Skip to content

Commit 0527e03

Browse files
cknittclaude
andcommitted
Resolve FFI externals in the type checker, not the parsetree
`pval_prim` is the primitive string as written, with its location. The frontend still checks each FFI external, but `Typedecl` gets its spec through `Primitive.resolve_external`, so the parsetree no longer depends on `External_ffi_types`. Review: Why does the frontend resolve each external at all? It reports the errors and warnings where they were reported before, and decides whether a relative `@module` keeps other modules from inlining the external. The type checker's second resolution runs without warnings. Review: How do `@inline` constants work without `Prim_inline_const`? The built-in PPX turns them into an external with the internal primitive `#rescript-inline` that keeps the `@inline(<literal>)` attribute. Review: Is the global `Primitive.resolve_external` safe with parallel requests? `Ast_external` sets it once during module initialization, like the `Oprint` hooks, and it is only read afterwards. Area: frontend typechecker syntax gentype tests Output: unchanged, generated JS of the test suites is byte-identical Formatting: unchanged Formats: ast cmt PPX: unchanged, an external's primitive string reaches v0 as before Editor: unchanged Errors: none, the new fixture follows the warning_<NN> naming convention Tests: +tests/syntax_tests/data/ast-mapping/Externals.res +tests/build_tests/super_errors/fixtures/warning_101_external_arg_attribute.res ~tests/ounit_tests/ounit_ast_mapper0_tests.ml ~tests/ounit_tests/ounit_string_literal_tests.ml PR: #8728 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018oLiCQjGFSE2qwfYqCMF9e Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 4e11b25 commit 0527e03

43 files changed

Lines changed: 480 additions & 402 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Keep resolved FFI specifications out of the parsetree, which now holds an external's primitive string as written. https://github.com/rescript-lang/rescript/pull/8728

‎compiler/core/lam_compile_external_call.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types ~(prim_name : string)
310310
E.new_ fn args)
311311
| ( (Decl_send _ | Decl_get _ | Decl_set _ | Decl_get_index | Decl_set_index),
312312
Some Module_itself ) ->
313-
assert false (* rejected during digestion *)
313+
assert false (* rejected during resolution *)
314314
| Decl_val {name}, _ when decl.effective_arity = 0 ->
315315
(* a global value *)
316316
let e = translate_scoped_module_val named_module name scopes in

‎compiler/ext/config.ml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ let cmi_magic_number = "Caml1999I037"
22

33
(* Magic numbers for marshaled values of the *current* parsetree, whose layout
44
changes across compiler versions. *)
5-
and ast_impl_magic_number = "ResImpl01316"
5+
and ast_impl_magic_number = "ResImpl01317"
66

7-
and ast_intf_magic_number = "ResIntf01316"
7+
and ast_intf_magic_number = "ResIntf01317"
88

99
(* Magic numbers of the frozen Parsetree0 (OCaml 4.06) layout used on the
1010
external-PPX wire. They must never be written in front of a
@@ -13,7 +13,7 @@ and ast0_impl_magic_number = "Caml1999M022"
1313

1414
and ast0_intf_magic_number = "Caml1999N022"
1515

16-
and cmt_magic_number = "Caml1999T044"
16+
and cmt_magic_number = "Caml1999T045"
1717

1818
let get_load_path () = (Compiler_request_state.current ()).load_path
1919
let set_load_path path = (Compiler_request_state.current ()).load_path <- path

‎compiler/frontend/ast_attributes.ml‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,13 @@ let prim_to_be_encoded (name : string) = not (first_char_special name)
6969
They are not considered externals, they are part of the language
7070
*)
7171

72-
let rs_externals (attrs : t) (pval_prim : Parsetree.primitive_repr option) =
72+
let is_ffi_external (attrs : t) (pval_prim : string Asttypes.loc option) =
7373
match (attrs, pval_prim) with
74-
| _, (None | Some (Prim_ffi _ | Prim_inline_const _)) -> false
75-
(* [None] is a [val]; an already-digested external is not processed again *)
76-
| [], Some (Prim_name name) ->
74+
| _, None -> false (* [None] is a [val] *)
75+
| [], Some {txt = name} ->
7776
(* Not any attribute found *)
7877
prim_to_be_encoded name
79-
| _, Some (Prim_name name) ->
78+
| _, Some {txt = name} ->
8079
Ext_list.exists_fst attrs (fun ({txt} : string Asttypes.loc) ->
8180
external_attrs |> Array.exists (fun (x : string) -> txt = x))
8281
|| prim_to_be_encoded name

‎compiler/frontend/ast_attributes.mli‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ val process_derive_type : t -> derive_attr * t
5050

5151
val internal_expansive : attr
5252

53-
val rs_externals : t -> Parsetree.primitive_repr option -> bool
53+
val is_ffi_external : t -> string Asttypes.loc option -> bool
5454

5555
val is_gentype : attr -> bool
5656

‎compiler/frontend/ast_exp_handle_external.ml‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
let handle_debugger loc (payload : Ast_payload.t) =
2626
match payload with
2727
| PStr [] ->
28-
Ast_external_mk.local_external_apply loc ~pval_prim:(Prim_name "%debugger")
28+
Ast_external_mk.local_external_apply loc ~pval_prim:"%debugger"
2929
~pval_type:
3030
(Ast_helper.Typ.arrow
3131
[Parg_type {attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
@@ -45,8 +45,7 @@ let handle_raw loc payload =
4545
{
4646
exp with
4747
pexp_desc =
48-
Ast_external_mk.local_external_apply loc
49-
~pval_prim:(Prim_name "#raw_expr")
48+
Ast_external_mk.local_external_apply loc ~pval_prim:"#raw_expr"
5049
~pval_type:
5150
(Ast_helper.Typ.arrow
5251
[
@@ -94,8 +93,7 @@ let handle_ffi ~loc ~payload =
9493
{
9594
exp with
9695
pexp_desc =
97-
Ast_external_mk.local_external_apply loc
98-
~pval_prim:(Prim_name "#raw_expr")
96+
Ast_external_mk.local_external_apply loc ~pval_prim:"#raw_expr"
9997
~pval_type:
10098
(Ast_helper.Typ.arrow
10199
[
@@ -117,8 +115,7 @@ let handle_raw_structure loc payload =
117115
{
118116
exp with
119117
pexp_desc =
120-
Ast_external_mk.local_external_apply loc
121-
~pval_prim:(Prim_name "#raw_stmt")
118+
Ast_external_mk.local_external_apply loc ~pval_prim:"#raw_stmt"
122119
~pval_type:
123120
(Ast_helper.Typ.arrow
124121
[

‎compiler/frontend/ast_external.ml‎

Lines changed: 93 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,77 +22,105 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
let handle_external_in_sig (self : Ast_mapper.mapper)
26-
(prim : Parsetree.value_description) (sigi : Parsetree.signature_item) :
27-
Parsetree.signature_item =
25+
(* Resolve an FFI external that [validate] accepted. That cannot fail, and
26+
its warnings were reported then, so they are not repeated. *)
27+
let resolve_validated (value_desc : Parsetree.value_description)
28+
(prim_name : string) =
29+
Warnings.without_warnings (fun () ->
30+
Ast_external_process.resolve value_desc.pval_loc value_desc.pval_type
31+
value_desc.pval_attributes prim_name)
32+
33+
let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
34+
Primitive.resolved_external =
35+
if prim_name = Ast_external_mk.inline_const_prim then
36+
match Ast_external_mk.inline_const_of_declaration value_desc with
37+
| Some c ->
38+
{
39+
resolved_type = value_desc.pval_type;
40+
resolved_attributes = [];
41+
resolved_name = "";
42+
resolved_kind = Kind_inline_const c;
43+
}
44+
| None ->
45+
Location.raise_errorf ~loc:value_desc.pval_loc
46+
"\"%s\" is reserved for %@inline constants" prim_name
47+
else if
48+
Ast_attributes.is_ffi_external value_desc.pval_attributes
49+
value_desc.pval_prim
50+
then
51+
let {Ast_external_process.pval_type; spec; pval_attributes} =
52+
resolve_validated value_desc prim_name
53+
in
54+
{
55+
resolved_type = pval_type;
56+
resolved_attributes = pval_attributes;
57+
resolved_name = prim_name;
58+
resolved_kind = Kind_external spec;
59+
}
60+
else
61+
{
62+
resolved_type = value_desc.pval_type;
63+
resolved_attributes = value_desc.pval_attributes;
64+
resolved_name = prim_name;
65+
resolved_kind = Kind_intrinsic;
66+
}
67+
68+
(* The built-in PPX, which every compilation runs before type checking, links
69+
this module, so the type checker always finds the resolver registered. *)
70+
let () = Primitive.resolve_external := resolve
71+
72+
let resolved_val (value_desc : Parsetree.value_description)
73+
({pval_type; pval_attributes} : Ast_external_process.resolution) :
74+
Parsetree.value_description =
75+
{value_desc with pval_type; pval_prim = None; pval_attributes}
76+
77+
let resolved_ffi_external (value_desc : Parsetree.value_description) =
78+
match value_desc.pval_prim with
79+
| None -> value_desc
80+
| Some {txt = prim_name} ->
81+
resolved_val value_desc (resolve_validated value_desc prim_name)
82+
83+
(* Resolve an FFI external now to report its errors and warnings, and keep it
84+
as written for the type checker, which resolves it again through
85+
[resolve]. Returns the external as written, and its resolved form as a
86+
[val] when a relative [@module] makes it unfit for cross-module inlining. *)
87+
let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description) =
2888
let loc = prim.pval_loc in
2989
let pval_type = self.typ self prim.pval_type in
3090
let pval_attributes = self.attributes self prim.pval_attributes in
3191
match prim.pval_prim with
3292
| None -> Location.raise_errorf ~loc "empty primitive string"
33-
| Some (Prim_ffi _ | Prim_inline_const _) ->
34-
Location.raise_errorf ~loc "external declaration already processed"
35-
| Some (Prim_name v) -> (
36-
match
37-
Ast_external_process.handle_attributes_as_prim loc pval_type
38-
pval_attributes v
39-
with
40-
| {pval_type; pval_prim; pval_attributes; no_inline_cross_module} ->
41-
{
42-
sigi with
43-
psig_desc =
44-
Psig_value
45-
{
46-
prim with
47-
pval_type;
48-
pval_prim =
49-
(if no_inline_cross_module then None else Some pval_prim);
50-
pval_attributes;
51-
};
52-
})
93+
| Some {txt = prim_name} ->
94+
let resolution =
95+
Ast_external_process.resolve loc pval_type pval_attributes prim_name
96+
in
97+
( {prim with pval_type; pval_attributes},
98+
if resolution.no_inline_cross_module then
99+
Some (resolved_val prim resolution)
100+
else None )
101+
102+
let handle_external_in_sig (self : Ast_mapper.mapper)
103+
(prim : Parsetree.value_description) (sigi : Parsetree.signature_item) :
104+
Parsetree.signature_item =
105+
let external_, not_inlined = validate self prim in
106+
{
107+
sigi with
108+
psig_desc = Psig_value (Option.value not_inlined ~default:external_);
109+
}
53110

54111
let handle_external_in_stru (self : Ast_mapper.mapper)
55112
(prim : Parsetree.value_description) (str : Parsetree.structure_item) :
56113
Parsetree.structure_item =
57-
let loc = prim.pval_loc in
58-
let pval_type = self.typ self prim.pval_type in
59-
let pval_attributes = self.attributes self prim.pval_attributes in
60-
match prim.pval_prim with
61-
| None -> Location.raise_errorf ~loc "empty primitive string"
62-
| Some (Prim_ffi _ | Prim_inline_const _) ->
63-
Location.raise_errorf ~loc "external declaration already processed"
64-
| Some (Prim_name v) -> (
65-
match
66-
Ast_external_process.handle_attributes_as_prim loc pval_type
67-
pval_attributes v
68-
with
69-
| {pval_type; pval_prim; pval_attributes; no_inline_cross_module} ->
70-
let external_result =
71-
{
72-
str with
73-
pstr_desc =
74-
Pstr_primitive
75-
{prim with pval_type; pval_prim = Some pval_prim; pval_attributes};
76-
}
77-
in
78-
if not no_inline_cross_module then external_result
79-
else
80-
let open Ast_helper in
81-
Str.include_ ~loc
82-
(Incl.mk ~loc
83-
(Mod.constraint_ ~loc
84-
(Mod.structure ~loc [external_result])
85-
(Mty.signature ~loc
86-
[
87-
{
88-
psig_desc =
89-
Psig_value
90-
{
91-
prim with
92-
pval_type;
93-
pval_prim = None;
94-
pval_attributes;
95-
};
96-
psig_loc = loc;
97-
};
98-
]))))
114+
let external_, not_inlined = validate self prim in
115+
let external_result = {str with pstr_desc = Pstr_primitive external_} in
116+
match not_inlined with
117+
| None -> external_result
118+
| Some resolved_val ->
119+
let loc = prim.pval_loc in
120+
let open Ast_helper in
121+
Str.include_ ~loc
122+
(Incl.mk ~loc
123+
(Mod.constraint_ ~loc
124+
(Mod.structure ~loc [external_result])
125+
(Mty.signature ~loc
126+
[{psig_desc = Psig_value resolved_val; psig_loc = loc}])))

‎compiler/frontend/ast_external.mli‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
val resolve :
26+
Parsetree.value_description -> string -> Primitive.resolved_external
27+
(** The resolver behind [Primitive.resolve_external]: an [@inline] constant,
28+
an FFI external with its specification, or an intrinsic. *)
29+
30+
val resolved_ffi_external :
31+
Parsetree.value_description -> Parsetree.value_description
32+
(** An FFI external that [handle_external_in_sig] or [handle_external_in_stru]
33+
accepted, as a [val] of its resolved type and remaining attributes *)
34+
2535
val handle_external_in_sig :
2636
Ast_mapper.mapper ->
2737
Parsetree.value_description ->

‎compiler/frontend/ast_external_mk.ml‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
let local_external_apply loc ?(pval_attributes = [])
26-
~(pval_prim : Parsetree.primitive_repr) ~(pval_type : Parsetree.core_type)
27-
?(local_module_name = "J") ?(local_fun_name = "unsafe_expr")
28-
(args : Parsetree.expression list) : Parsetree.expression_desc =
25+
let local_external_apply loc ?(pval_attributes = []) ~(pval_prim : string)
26+
~(pval_type : Parsetree.core_type) ?(local_module_name = "J")
27+
?(local_fun_name = "unsafe_expr") (args : Parsetree.expression list) :
28+
Parsetree.expression_desc =
2929
Pexp_letmodule
3030
( {txt = local_module_name; loc},
3131
Ast_helper.Mod.structure ~loc
@@ -35,7 +35,7 @@ let local_external_apply loc ?(pval_attributes = [])
3535
pval_name = {txt = local_fun_name; loc};
3636
pval_type;
3737
pval_loc = loc;
38-
pval_prim = Some pval_prim;
38+
pval_prim = Some {txt = pval_prim; loc};
3939
pval_attributes;
4040
};
4141
],
@@ -44,18 +44,41 @@ let local_external_apply loc ?(pval_attributes = [])
4444
{txt = Ldot (Lident local_module_name, local_fun_name); loc})
4545
(Ext_list.map args (fun x -> (Asttypes.Nolabel, x))) )
4646

47-
let inline_const (c : External_ffi_types.inline_const) :
48-
Parsetree.primitive_repr =
49-
Prim_inline_const c
47+
let inline_const_prim = "#rescript-inline"
5048

51-
let inline_string semantic = inline_const (Const_string semantic)
49+
let inline_const_of_expression (expression : Parsetree.expression) :
50+
External_ffi_types.inline_const option =
51+
match (Ast_payload.unwrap_braces expression).pexp_desc with
52+
| Pexp_constant (Pconst_string _)
53+
| Pexp_template {source_segments = [_]; values = []} -> (
54+
match Ast_payload.semantic_string_of_expression expression with
55+
| Some semantic -> Some (Const_string semantic)
56+
| None -> assert false)
57+
| Pexp_constant (Pconst_integer (s, None)) ->
58+
Some (Const_int (Int32.of_string s))
59+
| Pexp_constant (Pconst_integer (s, Some 'n')) ->
60+
let positive, digits = Bigint_utils.parse_bigint s in
61+
Some (Const_bigint {positive; digits})
62+
| Pexp_constant (Pconst_float (s, None)) -> Some (Const_float s)
63+
| Pexp_construct ({txt = Lident (("true" | "false") as txt)}, {txt = []}) ->
64+
Some (Const_bool (txt = "true"))
65+
| _ -> None
5266

53-
let inline_bool b = inline_const (Const_bool b)
67+
let inline_const_declaration ~(attr_loc : Location.t)
68+
(value_desc : Parsetree.value_description)
69+
(expression : Parsetree.expression) : Parsetree.value_description =
70+
{
71+
value_desc with
72+
pval_prim = Some {txt = inline_const_prim; loc = attr_loc};
73+
pval_attributes =
74+
[
75+
( {txt = "inline"; loc = attr_loc},
76+
PStr [Ast_helper.Str.eval ~loc:expression.pexp_loc expression] );
77+
];
78+
}
5479

55-
let inline_int i = inline_const (Const_int i)
56-
57-
let inline_bigint s =
58-
let positive, digits = Bigint_utils.parse_bigint s in
59-
inline_const (Const_bigint {positive; digits})
60-
61-
let inline_float s = inline_const (Const_float s)
80+
let inline_const_of_declaration (value_desc : Parsetree.value_description) =
81+
match value_desc.pval_attributes with
82+
| [({txt = "inline"}, PStr [{pstr_desc = Pstr_eval (expression, _)}])] ->
83+
inline_const_of_expression expression
84+
| _ -> None

0 commit comments

Comments
 (0)