Resolve FFI externals during type checking
Why
The parsetree is what PPXs see and what -bs-ast serializes, and #8624 wants it to describe source only. After the built-in PPX had read an external's @send, @module, @as… attributes, it replaced the primitive string with the computed FFI specification (Prim_ffi, Prim_inline_const), so Parsetree depended on External_ffi_types and carried cases no PPX can observe. Now pval_prim is the string as written (string option), and Typedecl resolves the attributes through Primitive.resolve_external, a hook the frontend registers since the resolver lives above compiler/ml. The frontend no longer resolves them as well. The resolver also reports the external's errors and warnings, inside the declaration's @warning scope, and after any @@warning above it in the file. The generated JavaScript is unchanged, except for @inline let bigint constants, which are now inlined.
modifiedAST externals after the built-in PPX
type t@send external on: (t, @as("click") _, unit => unit) => unit = "addEventListener"@inline let answer = 42
expanded
modifiedAST an external with a relative path after the built-in PPX
@module("./local") external relative: int => int = "relative"expanded
modifiedTypedtree an external with a relative path
@module("./local") external relative: int => int = "relative"typed
modifiedTypedtree externals
type t@send external on: (t, @as("click") _, unit => unit) => unit = "addEventListener"@inline let answer = 42
typed
modifiedDiagnostic unused attribute on a relative @module external
@module("./local") external relative: @as("v") int => unit = "relative"diagnostic
removedDiagnostic unused attribute under a @warning that disables it
@warning("-101") @module("./local")external silenced: @as("w") int => unit = "silenced"
diagnostic
Warning number 101A.res:2:21-23Unused attribute: @as
removedDiagnostic unused attribute after a @@warning that disables it
@send external first: (string, @as("x") int) => unit = "first"@@warning("-101")@send external second: (string, @as("y") int) => unit = "second"
diagnostic
addedDiagnostic an external that names the inline-constant primitive
external answer: int = "#rescript-inline"diagnostic
"#rescript-inline" is reserved for @inline constantsmodifiedJS @inline let with a bigint
@inline let big = 10nlet use = () => big
js
removedDiagnostic @inline on a bigint let
@inline let big = 10ndiagnostic
Warning number 53the @inline attribute cannot appear in this context
Review
- How does an external with a relative path stay out of other modules? Its path is relative to the file that declares it, so other modules call the value its module exports. In a signature,
Typedeclgives itVal_reg. In a structure,Typemodtypes it asinclude (struct external … end : {let …}), the wrapper the built-in PPX used to write, whose coercion (Tcoerce_primitive) emits the exported function.Primitive.no_cross_module_inlinereads relativeness off the resolved spec (External_ffi_types.is_relative, split out ofcheck_decl). - Why a hook rather than passing the result along?
-bs-astwrites the parsetree after the built-in PPX and a laterbscprocess reads it back, so the specification has to be recomputable from the tree.Ppx_entry, which every compiler driver links, registers the resolver at module initialization, as theOprinthooks are. - What do diagnostics look like now? An external's errors and warning 101 come during type checking, in source order with the type errors, instead of in the parse step; an earlier type error hides them until it is fixed. The reprint path and
-bs-astalone no longer report them. - Is each external resolved once? Yes, except in a recursive module's signature, which the type checker types twice, the first time with warnings off, so its diagnostics come once.
- What does this cost? Each external is resolved in the compile step, which reads it as written, attributes and all. The unused-attribute walk on the resolved form is skipped unless it checks one of the attributes, which is the common case. Parse and compile steps together allocate 0.65% fewer words than before on Stdlib_Math.resi, 0.39% more on Stdlib_Math.res, and 1.4% more on Stdlib_Symbol.res, a small file of
@val @scope(...)constants. - What is
"#rescript-inline"? The primitive of an@inlineconstant's declaration, which keeps its@inline(<literal>)attribute; the resolver turns it intoKind_inline_const. A structural form is a separate #8624 item. - Do PPXs see a difference? No: they run before the built-in PPX, and the v0 bridge carries the string as before; its error branch for already-resolved externals is gone.
- What changes under
-bs-no-builtin-ppx? An external with a plain name is now resolved as an FFI external by the type checker; before, it was typed as an intrinsic of that name. - What else goes?
Typedtree.val_prim, a copy of the primitive string whose readers only need the value's resolved kind, andPrimitive.print:Outcometree.oval_primis thePrimitive.descriptionitself.