Implement autofilled source loc magic constants#8303
Conversation
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
cristianoc
left a comment
There was a problem hiding this comment.
Any special reason to have this under a command line option?
Seems unlikely one would hit it by change as it's based on the new predef types.
The main changes to type checker etc are run anyway so not sure turning it off reduces the affected surface.
| | Loc_SOURCE_LOC_VALUE_PATH -> | ||
| let value_path = | ||
| match current_value_path with | ||
| | [] -> module_path |
There was a problem hiding this comment.
The following case covers this one too right?
| "LICENSE", | ||
| "LICENSE.MIT", | ||
| "README.md", | ||
| "cli/bsc.exe", |
There was a problem hiding this comment.
guess .exe files should not pop up here
Just to allow some way of turning it on/off for various environments. So you don't accidentally put it in prod unless you want to. That was the intention. |
This PR adds an opt-in way to automatically capture call site information at runtime. It does so by introducing two new builtin abstract types:
sourceLocPosandsourceLocValuePath.The opt-in is explicit at the function definition site: if you want one of these arguments to be compiler-filled, you declare it with
%autofill, for example~pos: sourceLocPos=%autofill. If%autofillis not present, the argument is just a regular argument. When the feature is enabled and such an argument is omitted at the call site, the compiler automatically injects information about the call site into it.The two builtin types contain different kinds of call site information:
sourceLocPosgives a string like<file>;<startLine>;<startCol>;<endLine>;<endCol>sourceLocValuePathgives a string likeModule.Submodule.value.localBinding, pointing to the value path of the call in the fileBoth of these types are abstract. To extract meaningful information from them, this PR also adds a new stdlib module,
SourceLoc, with helpers for interacting withsourceLocPosandsourceLocValuePath. These helpers can turn the abstract values into their underlying strings, or decode them into richer structures.This feature is intended for things like:
Configuration
This should eventually become a proper configuration, but for now it is opt-in and enabled by passing this to
bsc:The two opt-in points are intentional:
%autofillmakes it obvious at a glance which arguments are intended to be compiler-filled-allow-autofill-source-locenables the actual call-site injectionIf
-allow-autofill-source-locis not enabled, omitted%autofillarguments fall back to an empty string representation instead of receiving injected call site information. So code using these values should handle the “off” case gracefully.Examples
What each type means: