Skip to content

Commit c3bf716

Browse files
cknittclaude
andcommitted
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

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+
- Report an unused attribute on a relative `@module` external once instead of twice.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- A `@warning` attribute on an external now applies to the warnings about its FFI attributes, such as an unused `@as`.

‎changes/inline-bigint-let.fix.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Inline `@inline let` bigint constants like other literals, instead of warning that `@inline` cannot appear there.
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. The type checker resolves each external once.

‎compiler/core/lam_compile_external_call.ml‎

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

‎compiler/frontend/ast_attributes.ml‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,10 @@ 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) =
73-
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) ->
77-
(* Not any attribute found *)
78-
prim_to_be_encoded name
79-
| _, Some (Prim_name name) ->
80-
Ext_list.exists_fst attrs (fun ({txt} : string Asttypes.loc) ->
81-
external_attrs |> Array.exists (fun (x : string) -> txt = x))
82-
|| prim_to_be_encoded name
72+
let is_ffi_external (attrs : t) (prim_name : string) =
73+
Ext_list.exists_fst attrs (fun ({txt} : string Asttypes.loc) ->
74+
external_attrs |> Array.exists (fun (x : string) -> txt = x))
75+
|| prim_to_be_encoded prim_name
8376

8477
let is_inline : attr -> bool = fun ({txt}, _) -> txt = "inline"
8578

‎compiler/frontend/ast_attributes.mli‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ 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 -> bool
54+
(** Whether an [external] with these attributes and primitive name is an FFI
55+
external, as opposed to a compiler intrinsic. *)
5456

5557
val is_gentype : attr -> bool
5658

‎compiler/frontend/ast_exp_handle_external.ml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ let raw_expression loc ~is_function (exp : Parsetree.expression) =
3232
{
3333
exp with
3434
pexp_desc =
35-
Ast_external_mk.local_external_apply loc
36-
~pval_prim:(Prim_name "#raw_expr") ~pval_type:(raw_type ()) [exp];
35+
Ast_external_mk.local_external_apply loc ~pval_prim:"#raw_expr"
36+
~pval_type:(raw_type ()) [exp];
3737
pexp_attributes =
3838
(match !is_function with
3939
| None -> exp.pexp_attributes
@@ -72,8 +72,8 @@ let raw_structure loc (exp : Parsetree.expression) =
7272
{
7373
exp with
7474
pexp_desc =
75-
Ast_external_mk.local_external_apply loc
76-
~pval_prim:(Prim_name "#raw_stmt") ~pval_type:(raw_type ()) [exp];
75+
Ast_external_mk.local_external_apply loc ~pval_prim:"#raw_stmt"
76+
~pval_type:(raw_type ()) [exp];
7777
}
7878

7979
let handle_raw loc payload =

‎compiler/frontend/ast_external.ml‎

Lines changed: 38 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -22,77 +22,42 @@
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 =
28-
let loc = prim.pval_loc in
29-
let pval_type = self.typ self prim.pval_type in
30-
let pval_attributes = self.attributes self prim.pval_attributes in
31-
match prim.pval_prim with
32-
| 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} ->
25+
(* The resolver behind [Primitive.resolve_external], which the type checker
26+
calls once per declaration: an [@inline] constant, an FFI external with its
27+
specification, or an intrinsic. An FFI external's errors and warnings are
28+
reported here, including unused attributes on its resolved form. *)
29+
let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
30+
Primitive.resolved_external =
31+
if prim_name = Ast_external_mk.inline_const_prim then
32+
match Ast_external_mk.inline_const_of_declaration value_desc with
33+
| Some c ->
4134
{
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-
})
53-
54-
let handle_external_in_stru (self : Ast_mapper.mapper)
55-
(prim : Parsetree.value_description) (str : Parsetree.structure_item) :
56-
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-
]))))
35+
resolved_type = value_desc.pval_type;
36+
resolved_attributes = [];
37+
resolved_name = "";
38+
resolved_kind = Kind_inline_const c;
39+
}
40+
| None ->
41+
Location.raise_errorf ~loc:value_desc.pval_loc
42+
"\"%s\" is reserved for %@inline constants" prim_name
43+
else if Ast_attributes.is_ffi_external value_desc.pval_attributes prim_name
44+
then (
45+
let {Ast_external_process.pval_type; spec; pval_attributes} =
46+
Ast_external_process.resolve value_desc.pval_loc value_desc.pval_type
47+
value_desc.pval_attributes prim_name
48+
in
49+
Bs_ast_invariant.emit_external_warnings_on_resolved_external value_desc
50+
~pval_type ~pval_attributes;
51+
{
52+
resolved_type = pval_type;
53+
resolved_attributes = pval_attributes;
54+
resolved_name = prim_name;
55+
resolved_kind = Kind_external spec;
56+
})
57+
else
58+
{
59+
resolved_type = value_desc.pval_type;
60+
resolved_attributes = value_desc.pval_attributes;
61+
resolved_name = prim_name;
62+
resolved_kind = Kind_intrinsic;
63+
}

‎compiler/frontend/ast_external.mli‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@
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 handle_external_in_sig :
26-
Ast_mapper.mapper ->
27-
Parsetree.value_description ->
28-
Parsetree.signature_item ->
29-
Parsetree.signature_item
30-
31-
val handle_external_in_stru :
32-
Ast_mapper.mapper ->
33-
Parsetree.value_description ->
34-
Parsetree.structure_item ->
35-
Parsetree.structure_item
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. Reports an FFI
29+
external's errors and warnings. *)

0 commit comments

Comments
 (0)