Repository navigation
Commit c3bf716
Resolve an external's FFI attributes in the type checker
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, with its location, and
`Typedecl` resolves the attributes, once, through
`Primitive.resolve_external`, a hook the frontend registers since the
resolver lives above `compiler/ml`. The resolver also reports the
external's errors and warnings, inside the declaration's `@warning`
scope. The generated JavaScript is unchanged.
AST modified (externals after the built-in PPX):
Example:
type t
@send external on: (t, @as("click") _, unit => unit) => unit = "addEventListener"
@inline let answer = 42
Before (expanded):
…
structure_item
Pstr_primitive
value_description "on"
core_type
Ptyp_arrow
arity = 2
Nolabel
core_type
Ptyp_constr "t"
[]
Nolabel
core_type
Ptyp_arrow
…
core_type
Ptyp_constr "unit"
[]
"addEventListener"
<ffi spec>
structure_item
Pstr_primitive
value_description "answer"
core_type
Ptyp_constr "int"
[]
<inline const>
After (expanded):
…
structure_item
Pstr_primitive
value_description "on"
attribute "send"
[]
core_type
Ptyp_arrow
arity = 3
Nolabel
core_type
Ptyp_constr "t"
[]
Nolabel
Parg_fixed
value
Pfixed_string (source="click", semantic="click")
Nolabel
core_type
Ptyp_arrow
…
core_type
Ptyp_constr "unit"
[]
"addEventListener"
structure_item
Pstr_primitive
value_description "answer"
attribute "inline"
[
structure_item
Pstr_eval
expression
Pexp_constant PConst_int (42,None)
]
core_type
Ptyp_constr "int"
[]
"#rescript-inline"
AST modified (an external with a relative path after the built-in PPX):
Example:
@module("./local") external relative: int => int = "relative"
Before (expanded):
structure_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
…
]
After (expanded):
structure_item
Pstr_primitive
value_description "relative"
attribute "module"
…
core_type
…
"relative"
Typedtree modified (an external with a relative path):
Example:
@module("./local") external relative: int => int = "relative"
Before (typed): the constraint the built-in PPX wrote
structure_item
Tstr_include
module_expr
Tmod_constraint
module_expr
Tmod_structure
[
structure_item
Tstr_primitive
value_description relative/1001
…
"relative"
]
module_type
Tmty_signature
[
signature_item
Tsig_value
value_description relative/1002
…
]
After (typed): the constraint Typemod adds, implicit
structure_item
Tstr_include
module_expr
module_expr
Tmod_structure
[
structure_item
Tstr_primitive
value_description relative/1001
…
"relative"
]
Typedtree modified (externals):
Example:
type t
@send external on: (t, @as("click") _, unit => unit) => unit = "addEventListener"
@inline let answer = 42
Before (typed):
…
structure_item
Tstr_primitive
value_description on/1002
core_type
Ttyp_arrow
…
"addEventListener"
<ffi spec>
structure_item
Tstr_primitive
value_description answer/1003
core_type
Ttyp_constr "int/1"
[]
<inline const>
After (typed):
…
structure_item
Tstr_primitive
value_description on/1002
core_type
Ttyp_arrow
…
"addEventListener"
structure_item
Tstr_primitive
value_description answer/1003
core_type
Ttyp_constr "int/1"
[]
"#rescript-inline"
Diagnostic modified (unused attribute on a relative @module external):
Example:
@module("./local") external relative: @as("v") int => unit = "relative"
Before (diagnostic):
Warning number 101
A.res:1:39-41
Unused attribute: @as
Warning number 101
A.res:1:39-41
Unused attribute: @as
After (diagnostic):
Warning number 101
A.res:1:39-41
Unused attribute: @as
Diagnostic removed (unused attribute under a @warning that disables it):
Example:
@warning("-101") @module("./local")
external silenced: @as("w") int => unit = "silenced"
Before (diagnostic):
Warning number 101
A.res:2:21-23
Unused attribute: @as
JS modified (@inline let with a bigint):
Example:
@inline let big = 10n
let use = () => big
Before (js):
function use() {
return 10n;
}
let big = 10n;
exports.big = big;
exports.use = use;
After (js):
function use() {
return 10n;
}
exports.use = use;
Diagnostic removed (@inline on a bigint let):
Example:
@inline let big = 10n
Before (diagnostic):
Warning number 53
the @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`).
Review: 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.
Review: 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.
Review: What does this cost? Each external is resolved once, in the
compile step, which reads it as written. The unused-attribute walk
on the resolved form is skipped when nothing in it can be reported,
which is the common case. Parse and compile steps together allocate
about as many words as before on Stdlib_Math.resi and up to 1% more
on Stdlib_Math.res (resolving in the frontend as well had made it
4.3%).
Review: 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.
Review: 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.
Review: 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.
Review: 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.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018oLiCQjGFSE2qwfYqCMF9e
Claude-Session: https://claude.ai/code/session_01Kbf3gLpf7RN1MiRcubPgRn
Claude-Session: https://claude.ai/code/session_01Kbf3gJFAva9685fjvkajub
Claude-Session: https://claude.ai/code/session_01Kbf3gzUzT5G
Signed-off-by: Christoph Knittel <ck@cca.io>1 parent 32116b5 commit c3bf716
65 files changed
Lines changed: 822 additions & 478 deletions
File tree
- changes
- compiler
- core
- frontend
- gentype
- ml
- syntax/src
- tests
- analysis_tests/tests/src
- expected
- build_tests/super_errors
- expected
- fixtures
- ounit_tests
- syntax_tests/data/ast-mapping
- expected
- tests/src
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
358 | 358 | | |
359 | 359 | | |
360 | 360 | | |
361 | | - | |
| 361 | + | |
362 | 362 | | |
363 | 363 | | |
364 | 364 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
83 | 76 | | |
84 | 77 | | |
85 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
36 | | - | |
| 35 | + | |
| 36 | + | |
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
76 | | - | |
| 75 | + | |
| 76 | + | |
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
41 | 34 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
0 commit comments