Skip to content

Commit 282e6dd

Browse files
cknittclaude
andcommitted
Pass an FFI external's primitive name to the code that resolves it
`Ast_attributes.is_ffi_external` takes the primitive name rather than `pval_prim`, so each caller matches `Some {txt = prim_name}` once and hands the name on, and `Ast_external.validate` loses an "empty primitive string" error it could not reach. `Primitive.resolve_external` keeps its string argument for the same reason: `Typedecl` has already matched it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gJFAva9685fjvkajub Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent a53ab03 commit 282e6dd

6 files changed

Lines changed: 36 additions & 39 deletions

File tree

‎compiler/frontend/ast_attributes.ml‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +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 is_ffi_external (attrs : t) (pval_prim : string Asttypes.loc option) =
73-
match (attrs, pval_prim) with
74-
| _, None -> false (* [None] is a [val] *)
75-
| [], Some {txt = name} ->
76-
(* Not any attribute found *)
77-
prim_to_be_encoded name
78-
| _, Some {txt = name} ->
79-
Ext_list.exists_fst attrs (fun ({txt} : string Asttypes.loc) ->
80-
external_attrs |> Array.exists (fun (x : string) -> txt = x))
81-
|| 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
8276

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

‎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 is_ffi_external : t -> string Asttypes.loc 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_external.ml‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
4646
| None ->
4747
Location.raise_errorf ~loc:value_desc.pval_loc
4848
"\"%s\" is reserved for %@inline constants" prim_name
49-
else if
50-
Ast_attributes.is_ffi_external value_desc.pval_attributes
51-
value_desc.pval_prim
49+
else if Ast_attributes.is_ffi_external value_desc.pval_attributes prim_name
5250
then
5351
let {Ast_external_process.pval_type; spec; pval_attributes} =
5452
resolve_validated value_desc prim_name
@@ -81,34 +79,32 @@ let resolved_val (value_desc : Parsetree.value_description)
8179
checker, which resolves it again through [resolve]. Returns the external as
8280
written, and its resolved form as a [val] when a relative [@module] makes it
8381
unfit for cross-module inlining. *)
84-
let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description) =
82+
let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description)
83+
~prim_name =
8584
let loc = prim.pval_loc in
8685
let pval_type = self.typ self prim.pval_type in
8786
let pval_attributes = self.attributes self prim.pval_attributes in
88-
match prim.pval_prim with
89-
| None -> Location.raise_errorf ~loc "empty primitive string"
90-
| Some {txt = prim_name} ->
91-
let resolution =
92-
Ast_external_process.resolve loc pval_type pval_attributes prim_name
93-
in
94-
let resolved = resolved_val prim resolution in
95-
Bs_ast_invariant.emit_external_warnings_on_value_description resolved;
96-
( {prim with pval_type; pval_attributes},
97-
if resolution.no_inline_cross_module then Some resolved else None )
87+
let resolution =
88+
Ast_external_process.resolve loc pval_type pval_attributes prim_name
89+
in
90+
let resolved = resolved_val prim resolution in
91+
Bs_ast_invariant.emit_external_warnings_on_value_description resolved;
92+
( {prim with pval_type; pval_attributes},
93+
if resolution.no_inline_cross_module then Some resolved else None )
9894

9995
let handle_external_in_sig (self : Ast_mapper.mapper)
100-
(prim : Parsetree.value_description) (sigi : Parsetree.signature_item) :
101-
Parsetree.signature_item =
102-
let external_, not_inlined = validate self prim in
96+
(prim : Parsetree.value_description) ~prim_name
97+
(sigi : Parsetree.signature_item) : Parsetree.signature_item =
98+
let external_, not_inlined = validate self prim ~prim_name in
10399
{
104100
sigi with
105101
psig_desc = Psig_value (Option.value not_inlined ~default:external_);
106102
}
107103

108104
let handle_external_in_stru (self : Ast_mapper.mapper)
109-
(prim : Parsetree.value_description) (str : Parsetree.structure_item) :
110-
Parsetree.structure_item =
111-
let external_, not_inlined = validate self prim in
105+
(prim : Parsetree.value_description) ~prim_name
106+
(str : Parsetree.structure_item) : Parsetree.structure_item =
107+
let external_, not_inlined = validate self prim ~prim_name in
112108
let external_result = {str with pstr_desc = Pstr_primitive external_} in
113109
match not_inlined with
114110
| None -> external_result

‎compiler/frontend/ast_external.mli‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
val handle_external_in_sig :
2626
Ast_mapper.mapper ->
2727
Parsetree.value_description ->
28+
prim_name:string ->
2829
Parsetree.signature_item ->
2930
Parsetree.signature_item
3031

3132
val handle_external_in_stru :
3233
Ast_mapper.mapper ->
3334
Parsetree.value_description ->
35+
prim_name:string ->
3436
Parsetree.structure_item ->
3537
Parsetree.structure_item

‎compiler/frontend/bs_ast_invariant.ml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ let emit_external_warnings : iterator =
139139
value_description =
140140
(fun self v ->
141141
match v with
142-
| _ when Ast_attributes.is_ffi_external v.pval_attributes v.pval_prim ->
142+
| {pval_prim = Some {txt = prim_name}}
143+
when Ast_attributes.is_ffi_external v.pval_attributes prim_name ->
143144
(* Checked in its resolved form by [Ast_external.validate], with
144145
[emit_external_warnings_on_value_description]. *)
145146
()

‎compiler/frontend/bs_builtin_ppx.ml‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,11 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
457457
| Psig_type (rf, tdcls) -> Ast_tdcls.handle_tdcls_in_sigi self sigi rf tdcls
458458
| Psig_value ({pval_attributes; pval_prim} as value_desc) -> (
459459
let pval_attributes = self.attributes self pval_attributes in
460-
if Ast_attributes.is_ffi_external pval_attributes pval_prim then
461-
Ast_external.handle_external_in_sig self value_desc sigi
462-
else
460+
match pval_prim with
461+
| Some {txt = prim_name}
462+
when Ast_attributes.is_ffi_external pval_attributes prim_name ->
463+
Ast_external.handle_external_in_sig self value_desc ~prim_name sigi
464+
| _ -> (
463465
match Ast_attributes.has_inline_payload pval_attributes with
464466
| Some
465467
(({loc = attr_loc}, PStr [{pstr_desc = Pstr_eval (expression, _)}]) as
@@ -475,7 +477,7 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) :
475477
expression);
476478
}
477479
| None -> default_mapper.signature_item self sigi)
478-
| Some _ | None -> default_mapper.signature_item self sigi)
480+
| Some _ | None -> default_mapper.signature_item self sigi))
479481
| _ -> default_mapper.signature_item self sigi
480482

481483
let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
@@ -501,9 +503,9 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) :
501503
(LetUnwrap_not_supported_in_position `Toplevel)
502504
| Pstr_type (rf, tdcls) (* [ {ptype_attributes} as tdcl ] *) ->
503505
Ast_tdcls.handle_tdcls_in_stru self str rf tdcls
504-
| Pstr_primitive prim
505-
when Ast_attributes.is_ffi_external prim.pval_attributes prim.pval_prim ->
506-
Ast_external.handle_external_in_stru self prim str
506+
| Pstr_primitive ({pval_prim = Some {txt = prim_name}} as prim)
507+
when Ast_attributes.is_ffi_external prim.pval_attributes prim_name ->
508+
Ast_external.handle_external_in_stru self prim ~prim_name str
507509
| Pstr_value
508510
( Nonrecursive,
509511
[

0 commit comments

Comments
 (0)