Skip to content

Commit d7f09dd

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 59a5808 commit d7f09dd

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
@@ -44,9 +44,7 @@ let resolve (value_desc : Parsetree.value_description) (prim_name : string) :
4444
| None ->
4545
Location.raise_errorf ~loc:value_desc.pval_loc
4646
"\"%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
47+
else if Ast_attributes.is_ffi_external value_desc.pval_attributes prim_name
5048
then
5149
let {Ast_external_process.pval_type; spec; pval_attributes} =
5250
resolve_validated value_desc prim_name
@@ -79,34 +77,32 @@ let resolved_val (value_desc : Parsetree.value_description)
7977
checker, which resolves it again through [resolve]. Returns the external as
8078
written, and its resolved form as a [val] when a relative [@module] makes it
8179
unfit for cross-module inlining. *)
82-
let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description) =
80+
let validate (self : Ast_mapper.mapper) (prim : Parsetree.value_description)
81+
~prim_name =
8382
let loc = prim.pval_loc in
8483
let pval_type = self.typ self prim.pval_type in
8584
let pval_attributes = self.attributes self prim.pval_attributes in
86-
match prim.pval_prim with
87-
| None -> Location.raise_errorf ~loc "empty primitive string"
88-
| Some {txt = prim_name} ->
89-
let resolution =
90-
Ast_external_process.resolve loc pval_type pval_attributes prim_name
91-
in
92-
let resolved = resolved_val prim resolution in
93-
Bs_ast_invariant.emit_external_warnings_on_value_description resolved;
94-
( {prim with pval_type; pval_attributes},
95-
if resolution.no_inline_cross_module then Some resolved else None )
85+
let resolution =
86+
Ast_external_process.resolve loc pval_type pval_attributes prim_name
87+
in
88+
let resolved = resolved_val prim resolution in
89+
Bs_ast_invariant.emit_external_warnings_on_value_description resolved;
90+
( {prim with pval_type; pval_attributes},
91+
if resolution.no_inline_cross_module then Some resolved else None )
9692

9793
let handle_external_in_sig (self : Ast_mapper.mapper)
98-
(prim : Parsetree.value_description) (sigi : Parsetree.signature_item) :
99-
Parsetree.signature_item =
100-
let external_, not_inlined = validate self prim in
94+
(prim : Parsetree.value_description) ~prim_name
95+
(sigi : Parsetree.signature_item) : Parsetree.signature_item =
96+
let external_, not_inlined = validate self prim ~prim_name in
10197
{
10298
sigi with
10399
psig_desc = Psig_value (Option.value not_inlined ~default:external_);
104100
}
105101

106102
let handle_external_in_stru (self : Ast_mapper.mapper)
107-
(prim : Parsetree.value_description) (str : Parsetree.structure_item) :
108-
Parsetree.structure_item =
109-
let external_, not_inlined = validate self prim in
103+
(prim : Parsetree.value_description) ~prim_name
104+
(str : Parsetree.structure_item) : Parsetree.structure_item =
105+
let external_, not_inlined = validate self prim ~prim_name in
110106
let external_result = {str with pstr_desc = Pstr_primitive external_} in
111107
match not_inlined with
112108
| None -> external_result

‎compiler/frontend/ast_external.mli‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ val resolve :
3030
val handle_external_in_sig :
3131
Ast_mapper.mapper ->
3232
Parsetree.value_description ->
33+
prim_name:string ->
3334
Parsetree.signature_item ->
3435
Parsetree.signature_item
3536

3637
val handle_external_in_stru :
3738
Ast_mapper.mapper ->
3839
Parsetree.value_description ->
40+
prim_name:string ->
3941
Parsetree.structure_item ->
4042
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)