Skip to content

Commit e2110a3

Browse files
cknittclaude
andcommitted
Simplify external resolution and check externals in source order
- `Ast_external_process.resolve` returns its record directly instead of wrapping a tuple. - Rename `rs_externals` to `is_ffi_external`. - `typedecl.transl_value_decl` resolves once, in a single match. - Run the unused-attribute and leftover-json checks on FFI externals in the post-mapping pass again, on each external's resolved form. Running them while mapping reported warnings in reverse order, since the built-in PPX maps the rest of a structure before its head. - Add fixtures for an unused attribute on an external argument and a json literal on an argument that resolution does not consume. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1534797 commit e2110a3

14 files changed

Lines changed: 176 additions & 108 deletions

‎compiler/frontend/ast_attributes.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ 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 : string Asttypes.loc option) =
72+
let is_ffi_external (attrs : t) (pval_prim : string Asttypes.loc option) =
7373
match (attrs, pval_prim) with
7474
| _, None -> false (* [None] is a [val] *)
7575
| [], Some {txt = 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 -> string Asttypes.loc option -> bool
53+
val is_ffi_external : t -> string Asttypes.loc option -> bool
5454

5555
val is_gentype : attr -> bool
5656

‎compiler/frontend/ast_external.ml‎

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
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+
(* 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+
2533
let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
2634
Primitive.resolved_external =
2735
if prim_name = Ast_external_mk.inline_const_prim then
@@ -37,15 +45,11 @@ let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
3745
Location.raise_errorf ~loc:value_desc.pval_loc
3846
"\"%s\" is reserved for %@inline constants" prim_name
3947
else if
40-
Ast_attributes.rs_externals value_desc.pval_attributes value_desc.pval_prim
48+
Ast_attributes.is_ffi_external value_desc.pval_attributes
49+
value_desc.pval_prim
4150
then
42-
(* [handle_external_in_sig] and [handle_external_in_stru] resolved this
43-
declaration already and reported its warnings; resolve it again for
44-
the specification without repeating them. *)
4551
let {Ast_external_process.pval_type; spec; pval_attributes} =
46-
Warnings.without_warnings (fun () ->
47-
Ast_external_process.resolve value_desc.pval_loc value_desc.pval_type
48-
value_desc.pval_attributes prim_name)
52+
resolve_validated value_desc prim_name
4953
in
5054
{
5155
resolved_type = pval_type;
@@ -65,13 +69,21 @@ let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
6569
this module, so the type checker always finds the resolver registered. *)
6670
let () = Primitive.resolve_external := resolve
6771

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+
6883
(* Resolve an FFI external now to report its errors and warnings, and keep it
6984
as written for the type checker, which resolves it again through
70-
[resolve]. Returns the external as written, its resolved form as a [val],
71-
and whether a relative [@module] makes it unfit for cross-module inlining.
72-
Such an external is exported as that [val], which the AST checks then see
73-
like any other; otherwise its resolved form is checked here, since the
74-
checks skip the unresolved one. *)
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. *)
7587
let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description) =
7688
let loc = prim.pval_loc in
7789
let pval_type = self.typ self prim.pval_type in
@@ -82,37 +94,28 @@ let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description) =
8294
let resolution =
8395
Ast_external_process.resolve loc pval_type pval_attributes prim_name
8496
in
85-
let resolved_val : Parsetree.value_description =
86-
{
87-
prim with
88-
pval_type = resolution.pval_type;
89-
pval_prim = None;
90-
pval_attributes = resolution.pval_attributes;
91-
}
92-
in
93-
if not resolution.no_inline_cross_module then
94-
Bs_ast_invariant.check_resolved_external resolved_val;
9597
( {prim with pval_type; pval_attributes},
96-
resolved_val,
97-
resolution.no_inline_cross_module )
98+
if resolution.no_inline_cross_module then
99+
Some (resolved_val prim resolution)
100+
else None )
98101

99102
let handle_external_in_sig (self : Ast_mapper.mapper)
100103
(prim : Parsetree.value_description) (sigi : Parsetree.signature_item) :
101104
Parsetree.signature_item =
102-
let external_, resolved_val, no_inline_cross_module = validate self prim in
105+
let external_, not_inlined = validate self prim in
103106
{
104107
sigi with
105-
psig_desc =
106-
Psig_value (if no_inline_cross_module then resolved_val else external_);
108+
psig_desc = Psig_value (Option.value not_inlined ~default:external_);
107109
}
108110

109111
let handle_external_in_stru (self : Ast_mapper.mapper)
110112
(prim : Parsetree.value_description) (str : Parsetree.structure_item) :
111113
Parsetree.structure_item =
112-
let external_, resolved_val, no_inline_cross_module = validate self prim in
114+
let external_, not_inlined = validate self prim in
113115
let external_result = {str with pstr_desc = Pstr_primitive external_} in
114-
if not no_inline_cross_module then external_result
115-
else
116+
match not_inlined with
117+
| None -> external_result
118+
| Some resolved_val ->
116119
let loc = prim.pval_loc in
117120
let open Ast_helper in
118121
Str.include_ ~loc

‎compiler/frontend/ast_external.mli‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ val resolve :
2727
(** The resolver behind [Primitive.resolve_external]: an [@inline] constant,
2828
an FFI external with its specification, or an intrinsic. *)
2929

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+
3035
val handle_external_in_sig :
3136
Ast_mapper.mapper ->
3237
Parsetree.value_description ->

‎compiler/frontend/ast_external_process.ml‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -881,10 +881,16 @@ let external_decl_of_non_obj (loc : Location.t) (st : external_desc)
881881
| {get_name = Some _} ->
882882
Location.raise_errorf ~loc "Attribute found that conflicts with %@get"
883883

884+
type resolution = {
885+
pval_type: Parsetree.core_type;
886+
spec: External_ffi_types.t;
887+
pval_attributes: Parsetree.attributes;
888+
no_inline_cross_module: bool;
889+
}
890+
884891
(** Note that the passed [type_annotation] is already processed by visitor pattern before*)
885-
let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
886-
(prim_attributes : Ast_attributes.t) (prim_name : string) :
887-
Parsetree.core_type * External_ffi_types.t * Parsetree.attributes * bool =
892+
let resolve (loc : Location.t) (type_annotation : Parsetree.core_type)
893+
(prim_attributes : Ast_attributes.t) (prim_name : string) : resolution =
888894
let prim_name_with_source = {name = prim_name; source = External} in
889895
let result_type, arg_types_ty =
890896
(* Note this assumes external type is syntatic (no abstraction)*)
@@ -900,7 +906,12 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
900906
let _arity, new_type, spec =
901907
process_obj loc external_desc prim_name arg_types_ty result_type
902908
in
903-
(new_type, spec, unused_attrs, false)
909+
{
910+
pval_type = new_type;
911+
spec;
912+
pval_attributes = unused_attrs;
913+
no_inline_cross_module = false;
914+
}
904915
else
905916
let splice = external_desc.splice in
906917
let arg_type_specs, args, arg_type_specs_length =
@@ -988,23 +999,12 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
988999
let return_wrapper =
9891000
check_return_wrapper loc external_desc.return_wrapper result_type
9901001
in
991-
( (match args with
992-
| [] -> result_type
993-
| _ -> Ast_helper.Typ.arrow ~loc args result_type),
994-
External_ffi_types.ffi_bs arg_type_specs return_wrapper decl,
995-
unused_attrs,
996-
relative )
997-
998-
type resolution = {
999-
pval_type: Parsetree.core_type;
1000-
spec: External_ffi_types.t;
1001-
pval_attributes: Parsetree.attributes;
1002-
no_inline_cross_module: bool;
1003-
}
1004-
1005-
let resolve (pval_loc : Location.t) (typ : Ast_core_type.t)
1006-
(attrs : Ast_attributes.t) (prim_name : string) : resolution =
1007-
let pval_type, spec, pval_attributes, no_inline_cross_module =
1008-
handle_attributes pval_loc typ attrs prim_name
1009-
in
1010-
{pval_type; spec; pval_attributes; no_inline_cross_module}
1002+
{
1003+
pval_type =
1004+
(match args with
1005+
| [] -> result_type
1006+
| _ -> Ast_helper.Typ.arrow ~loc args result_type);
1007+
spec = External_ffi_types.ffi_bs arg_type_specs return_wrapper decl;
1008+
pval_attributes = unused_attrs;
1009+
no_inline_cross_module = relative;
1010+
}

‎compiler/frontend/bs_ast_invariant.ml‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,9 @@ let check_constant loc (const : Parsetree.constant) =
5858
with _ -> Bs_warnings.warn_literal_overflow loc)
5959
| _ -> ()
6060

61-
(* An FFI external stays as written until the type checker resolves it, so its
62-
argument types still carry the attributes resolution consumes. The checks
63-
below skip it; [check_resolved_external] runs them on its resolved form. *)
64-
let is_unresolved_ffi_external (v : Parsetree.value_description) =
65-
Ast_attributes.rs_externals v.pval_attributes v.pval_prim
66-
6761
(* Note we only used Bs_ast_iterator here, we can reuse compiler-libs instead of
6862
rolling our own*)
69-
let emit_external_warnings : iterator =
63+
let emit_external_warnings ~resolved_ffi_external : iterator =
7064
{
7165
super with
7266
type_declaration =
@@ -118,7 +112,8 @@ let emit_external_warnings : iterator =
118112
value_description =
119113
(fun self v ->
120114
match v with
121-
| _ when is_unresolved_ffi_external v -> ()
115+
| _ when Ast_attributes.is_ffi_external v.pval_attributes v.pval_prim ->
116+
super.value_description self (resolved_ffi_external v)
122117
| {
123118
pval_loc;
124119
pval_prim =
@@ -157,23 +152,28 @@ let rec iter_warnings_on_sigi (stru : Parsetree.signature) =
157152
iter_warnings_on_sigi rest
158153
| _ -> ())
159154

160-
let emit_external_warnings_on_structure (stru : Parsetree.structure) =
161-
emit_external_warnings.structure emit_external_warnings stru
155+
let emit_external_warnings_on_structure ~resolved_ffi_external
156+
(stru : Parsetree.structure) =
157+
let it = emit_external_warnings ~resolved_ffi_external in
158+
it.structure it stru
162159

163-
let emit_external_warnings_on_signature (sigi : Parsetree.signature) =
164-
emit_external_warnings.signature emit_external_warnings sigi
160+
let emit_external_warnings_on_signature ~resolved_ffi_external
161+
(sigi : Parsetree.signature) =
162+
let it = emit_external_warnings ~resolved_ffi_external in
163+
it.signature it sigi
165164

166165
(* [json] payloads are syntax-level expressions until built-in FFI processing
167166
consumes valid [@as(json`...`)] occurrences. Reject anything left only
168167
after that processing, so generic attributes and ordinary expressions
169168
cannot reinterpret them as strings. *)
170-
let unconsumed_json_iterator : iterator =
169+
let unconsumed_json_iterator ~resolved_ffi_external : iterator =
171170
{
172171
super with
173172
value_description =
174173
(fun self v ->
175-
if not (is_unresolved_ffi_external v) then
176-
super.value_description self v);
174+
if Ast_attributes.is_ffi_external v.pval_attributes v.pval_prim then
175+
super.value_description self (resolved_ffi_external v)
176+
else super.value_description self v);
177177
expr =
178178
(fun self expression ->
179179
match expression.pexp_desc with
@@ -188,12 +188,12 @@ let unconsumed_json_iterator : iterator =
188188
| _ -> super.pat self pattern);
189189
}
190190

191-
let reject_unconsumed_json_on_structure (stru : Parsetree.structure) =
192-
unconsumed_json_iterator.structure unconsumed_json_iterator stru
193-
194-
let reject_unconsumed_json_on_signature (sigi : Parsetree.signature) =
195-
unconsumed_json_iterator.signature unconsumed_json_iterator sigi
191+
let reject_unconsumed_json_on_structure ~resolved_ffi_external
192+
(stru : Parsetree.structure) =
193+
let it = unconsumed_json_iterator ~resolved_ffi_external in
194+
it.structure it stru
196195

197-
let check_resolved_external (v : Parsetree.value_description) =
198-
super.value_description unconsumed_json_iterator v;
199-
super.value_description emit_external_warnings v
196+
let reject_unconsumed_json_on_signature ~resolved_ffi_external
197+
(sigi : Parsetree.signature) =
198+
let it = unconsumed_json_iterator ~resolved_ffi_external in
199+
it.signature it sigi

‎compiler/frontend/bs_ast_invariant.mli‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,31 @@ val iter_warnings_on_stru : Parsetree.structure -> unit
3434

3535
val iter_warnings_on_sigi : Parsetree.signature -> unit
3636

37-
val emit_external_warnings_on_structure : Parsetree.structure -> unit
37+
(** The checks below see an FFI external in its resolved form, given by
38+
[resolved_ffi_external]: the declaration stays as written until type
39+
checking, so its argument types still carry the attributes that
40+
resolution consumes. *)
3841

39-
val emit_external_warnings_on_signature : Parsetree.signature -> unit
42+
val emit_external_warnings_on_structure :
43+
resolved_ffi_external:
44+
(Parsetree.value_description -> Parsetree.value_description) ->
45+
Parsetree.structure ->
46+
unit
4047

41-
val reject_unconsumed_json_on_structure : Parsetree.structure -> unit
48+
val emit_external_warnings_on_signature :
49+
resolved_ffi_external:
50+
(Parsetree.value_description -> Parsetree.value_description) ->
51+
Parsetree.signature ->
52+
unit
4253

43-
val reject_unconsumed_json_on_signature : Parsetree.signature -> unit
54+
val reject_unconsumed_json_on_structure :
55+
resolved_ffi_external:
56+
(Parsetree.value_description -> Parsetree.value_description) ->
57+
Parsetree.structure ->
58+
unit
4459

45-
val check_resolved_external : Parsetree.value_description -> unit
46-
(** Runs the checks above on an FFI external's resolved form, as a [val].
47-
They skip the external itself, which stays unresolved until type
48-
checking. *)
60+
val reject_unconsumed_json_on_signature :
61+
resolved_ffi_external:
62+
(Parsetree.value_description -> Parsetree.value_description) ->
63+
Parsetree.signature ->
64+
unit

‎compiler/frontend/bs_builtin_ppx.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
472472
| Psig_type (rf, tdcls) -> Ast_tdcls.handle_tdcls_in_sigi self sigi rf tdcls
473473
| Psig_value ({pval_attributes; pval_prim} as value_desc) -> (
474474
let pval_attributes = self.attributes self pval_attributes in
475-
if Ast_attributes.rs_externals pval_attributes pval_prim then
475+
if Ast_attributes.is_ffi_external pval_attributes pval_prim then
476476
Ast_external.handle_external_in_sig self value_desc sigi
477477
else
478478
match Ast_attributes.has_inline_payload pval_attributes with
@@ -520,7 +520,7 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
520520
| Pstr_type (rf, tdcls) (* [ {ptype_attributes} as tdcl ] *) ->
521521
Ast_tdcls.handle_tdcls_in_stru self str rf tdcls
522522
| Pstr_primitive prim
523-
when Ast_attributes.rs_externals prim.pval_attributes prim.pval_prim ->
523+
when Ast_attributes.is_ffi_external prim.pval_attributes prim.pval_prim ->
524524
Ast_external.handle_external_in_stru self prim str
525525
| Pstr_value
526526
( Nonrecursive,

‎compiler/frontend/ppx_entry.ml‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
3939
if !Js_config.no_builtin_ppx then ast
4040
else
4141
let result = unsafe_mapper.signature unsafe_mapper ast in
42-
Bs_ast_invariant.reject_unconsumed_json_on_signature result;
42+
Bs_ast_invariant.reject_unconsumed_json_on_signature
43+
~resolved_ffi_external:Ast_external.resolved_ffi_external result;
4344
(* Keep this check, since the check is not inexpensive*)
44-
Bs_ast_invariant.emit_external_warnings_on_signature result;
45+
Bs_ast_invariant.emit_external_warnings_on_signature
46+
~resolved_ffi_external:Ast_external.resolved_ffi_external result;
4547
result
4648

4749
let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure =
@@ -59,7 +61,9 @@ let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure =
5961
if !Js_config.no_builtin_ppx then ast
6062
else
6163
let result = unsafe_mapper.structure unsafe_mapper ast in
62-
Bs_ast_invariant.reject_unconsumed_json_on_structure result;
64+
Bs_ast_invariant.reject_unconsumed_json_on_structure
65+
~resolved_ffi_external:Ast_external.resolved_ffi_external result;
6366
(* Keep this check since it is not inexpensive*)
64-
Bs_ast_invariant.emit_external_warnings_on_structure result;
67+
Bs_ast_invariant.emit_external_warnings_on_structure
68+
~resolved_ffi_external:Ast_external.resolved_ffi_external result;
6569
result

0 commit comments

Comments
 (0)