Semantic Diff

#8728 Resolve FFI externals during type checking

1 commit

02a99b770f7e87d00f189e41c9212081d4132f83..pr-8728

0f975d2ae · cknitt + Claude · Nov 10, 2026, 19:24 UTC

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
Before
After
……structure_itemstructure_item├─ Pstr_primitive├─ Pstr_primitive└─ value_description "on"└─ value_description "on"    ├─ attribute "send"   │  └─ []   ├─ core_type   ├─ core_type   │  ├─ Ptyp_arrow   │  ├─ Ptyp_arrow   │  ├─ arity = 2   │  ├─ arity = 3   │  ├─ Nolabel   │  ├─ Nolabel   │  ├─ core_type   │  ├─ core_type   │  │  ├─ Ptyp_constr "t"   │  │  ├─ Ptyp_constr "t"   │  │  └─ []   │  │  └─ []   │  ├─ Nolabel   │  ├─ Nolabel    │  ├─ Parg_fixed   │  │  └─ value   │  │     └─ Pfixed_string (source="click", semantic="click")   │  ├─ Nolabel   │  ├─ core_type   │  ├─ core_type   │  │  ├─ Ptyp_arrow   │  │  ├─ Ptyp_arrow   │  │  └─ …   │  │  └─ …   │  └─ core_type   │  └─ core_type   │     ├─ Ptyp_constr "unit"   │     ├─ Ptyp_constr "unit"   │     └─ []   │     └─ []   ├─ "addEventListener"   └─ "addEventListener"   └─ <ffi spec> structure_itemstructure_item├─ Pstr_primitive├─ Pstr_primitive└─ value_description "answer"└─ value_description "answer"    ├─ attribute "inline"   │     └─ structure_item   │        ├─ Pstr_eval   │        └─ expression   │           └─ Pexp_constant PConst_int (42,None)   ├─ core_type   ├─ core_type   │  ├─ Ptyp_constr "int"   │  ├─ Ptyp_constr "int"   │  └─ []   │  └─ []   └─ <inline const>   └─ "#rescript-inline"

modifiedAST an external with a relative path after the built-in PPX

@module("./local") external relative: int => int = "relative"
expanded
Before
After
structure_itemstructure_item├─ Pstr_include└─ module_expr   ├─ Pmod_constraint   ├─ module_expr   │  └─ Pmod_structure   │     └─ structure_item   │        ├─ Pstr_primitive   │        └─ value_description "relative"   │           ├─ attribute "module"   │           │  └─ …   │           ├─ core_type   │           │  └─ …   │           └─ "relative"   └─ module_type      └─ Pmty_signature         └─ signature_item            ├─ Psig_value            └─ value_description "relative"               └─ core_type                  └─ …├─ Pstr_primitive└─ value_description "relative"   ├─ attribute "module"   │  └─ …   ├─ core_type   │  └─ …   └─ "relative"

modifiedTypedtree an external with a relative path

@module("./local") external relative: int => int = "relative"
typed
Before the constraint the built-in PPX wrote
After the constraint Typemod adds, implicit
structure_itemstructure_item├─ Tstr_include├─ Tstr_include└─ module_expr└─ module_expr   ├─ Tmod_constraint    ├─ module_expr   └─ module_expr   │  └─ Tmod_structure      └─ Tmod_structure   │     └─ structure_item         └─ structure_item   │        ├─ Tstr_primitive            ├─ Tstr_primitive   │        └─ value_description relative/1001            └─ value_description relative/1001   │           ├─ …               ├─ …   │           └─ "relative"               └─ "relative"   └─ module_type      └─ Tmty_signature         └─ signature_item            ├─ Tsig_value            └─ value_description relative/1002               └─ …

modifiedTypedtree externals

type t@send external on: (t, @as("click") _, unit => unit) => unit = "addEventListener"@inline let answer = 42
typed
Before
After
……structure_itemstructure_item├─ Tstr_primitive├─ Tstr_primitive└─ value_description on/1002└─ value_description on/1002   ├─ core_type   ├─ core_type   │  ├─ Ttyp_arrow   │  ├─ Ttyp_arrow   │  └─ …   │  └─ …   ├─ "addEventListener"   └─ "addEventListener"   └─ <ffi spec> structure_itemstructure_item├─ Tstr_primitive├─ Tstr_primitive└─ value_description answer/1003└─ value_description answer/1003   ├─ core_type   ├─ core_type   │  ├─ Ttyp_constr "int/1"   │  ├─ Ttyp_constr "int/1"   │  └─ []   │  └─ []   └─ <inline const>   └─ "#rescript-inline"

modifiedDiagnostic unused attribute on a relative @module external

@module("./local") external relative: @as("v") int => unit = "relative"
diagnostic
Before
After
Warning number 101Warning number 101A.res:1:39-41A.res:1:39-41Unused attribute: @asUnused attribute: @as Warning number 101A.res:1:39-41Unused attribute: @as

removedDiagnostic unused attribute under a @warning that disables it

@warning("-101") @module("./local")external silenced: @as("w") int => unit = "silenced"
diagnostic
Before
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
Before
After
Warning number 101Warning number 101A.res:1:32-34A.res:1:32-34Unused attribute: @asUnused attribute: @as Warning number 101A.res:3:33-35Unused attribute: @as

addedDiagnostic an external that names the inline-constant primitive

external answer: int = "#rescript-inline"
diagnostic
After
"#rescript-inline" is reserved for @inline constants

modifiedJS @inline let with a bigint

@inline let big = 10nlet use = () => big
js
Before
After
function use() {function use() { return 10n; return 10n;}}let big = 10n;exports.big = big; exports.use = use;exports.use = use;

removedDiagnostic @inline on a bigint let

@inline let big = 10n
diagnostic
Before
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, Typedecl gives it Val_reg. In a structure, Typemod types it as include (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_inline reads relativeness off the resolved spec (External_ffi_types.is_relative, split out of check_decl).
  • Why a hook rather than passing the result along? -bs-ast writes the parsetree after the built-in PPX and a later bsc process 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 the Oprint hooks 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-ast alone 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 @inline constant's declaration, which keeps its @inline(<literal>) attribute; the resolver turns it into Kind_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, and Primitive.print: Outcometree.oval_prim is the Primitive.description itself.