Skip to content

Commit 19a5a20

Browse files
cknittclaude
andcommitted
Represent fixed external arguments in the parsetree
`@as(value) _` arguments of externals become `Parg_fixed`, holding the value as written. The syntax, the v0 PPX encoding and the output are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NP47qMfjMbjUTKZDsLthDo Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent dcc7e35 commit 19a5a20

28 files changed

Lines changed: 572 additions & 136 deletions

‎analysis/src/signature_help.ml‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -114,33 +114,38 @@ let extract_parameters ~signature ~type_str_for_parser ~label_prefix_len =
114114
Psig_value {pval_type = {ptyp_desc = Ptyp_arrow {params = args}}};
115115
};
116116
] ->
117-
List.map
118-
(fun (arg : Parsetree.arg) ->
119-
let start_loc =
120-
(* For a labeled argument the label precedes the type. *)
121-
match arg.lbl with
122-
| Asttypes.Labelled {loc} | Optional {loc} -> loc |> Loc.start
123-
| Nolabel -> arg.typ.ptyp_loc |> Loc.start
124-
in
125-
let start_offset =
126-
start_loc |> Pos.position_to_offset type_str_for_parser |> Option.get
127-
in
128-
let end_offset =
129-
arg.typ.ptyp_loc |> Loc.end_
130-
|> Pos.position_to_offset type_str_for_parser
131-
|> Option.get
132-
in
133-
(* The AST locations does not account for "=?" of optional arguments, so add that to the offset here if needed. *)
134-
let end_offset =
135-
match arg.lbl with
136-
| Asttypes.Optional _ -> end_offset + 2
137-
| _ -> end_offset
138-
in
139-
( arg.lbl,
140-
(* Remove the label prefix offset here, since we're not
141-
showing that to the end user. *)
142-
start_offset - label_prefix_len,
143-
end_offset - label_prefix_len ))
117+
List.filter_map
118+
(function
119+
| Parsetree.Parg_fixed _ -> None
120+
| Parsetree.Parg_type {lbl; typ; _} ->
121+
let start_loc =
122+
(* For a labeled argument the label precedes the type. *)
123+
match lbl with
124+
| Asttypes.Labelled {loc} | Optional {loc} -> loc |> Loc.start
125+
| Nolabel -> typ.ptyp_loc |> Loc.start
126+
in
127+
let start_offset =
128+
start_loc
129+
|> Pos.position_to_offset type_str_for_parser
130+
|> Option.get
131+
in
132+
let end_offset =
133+
typ.ptyp_loc |> Loc.end_
134+
|> Pos.position_to_offset type_str_for_parser
135+
|> Option.get
136+
in
137+
(* The AST locations does not account for "=?" of optional arguments, so add that to the offset here if needed. *)
138+
let end_offset =
139+
match lbl with
140+
| Asttypes.Optional _ -> end_offset + 2
141+
| _ -> end_offset
142+
in
143+
Some
144+
( lbl,
145+
(* Remove the label prefix offset here, since we're not
146+
showing that to the end user. *)
147+
start_offset - label_prefix_len,
148+
end_offset - label_prefix_len ))
144149
args
145150
| _ -> []
146151

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Represent fixed external arguments, written `@as(value) _`, with a dedicated parsetree node. https://github.com/rescript-lang/rescript/pull/8875

‎compiler/frontend/ast_derive_projector.ml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ let init () =
132132
Ext_list.map label_declarations (fun {pld_name; pld_type} ->
133133
Ast_comb.single_non_rec_val ?attrs:gentype_attrs pld_name
134134
(Ast_helper.Typ.arrow
135-
[{attrs = []; lbl = Nolabel; typ = core_type}]
135+
[
136+
Parg_type {attrs = []; lbl = Nolabel; typ = core_type};
137+
]
136138
pld_type
137139
(*arity will alwys be 1 since these are single param functions*)))
138140
| Ptype_variant constructor_declarations ->
@@ -160,8 +162,8 @@ let init () =
160162
{loc; txt = Ext_string.uncapitalize_ascii con_name}
161163
(match
162164
Ext_list.map pcd_args (fun x ->
163-
({attrs = []; lbl = Nolabel; typ = x}
164-
: Parsetree.arg))
165+
Parsetree.Parg_type
166+
{attrs = []; lbl = Nolabel; typ = x})
165167
with
166168
| [] ->
167169
(* zero-argument constructor: the accessor is a value *)

‎compiler/frontend/ast_exp_handle_external.ml‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let handle_debugger loc (payload : Ast_payload.t) =
2828
Ast_external_mk.local_external_apply loc ~pval_prim:(Prim_name "%debugger")
2929
~pval_type:
3030
(Ast_helper.Typ.arrow
31-
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
31+
[Parg_type {attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
3232
(Ast_literal.type_unit ()))
3333
[Ast_literal.val_unit ~loc ()]
3434
| _ ->
@@ -49,7 +49,10 @@ let handle_raw loc payload =
4949
~pval_prim:(Prim_name "#raw_expr")
5050
~pval_type:
5151
(Ast_helper.Typ.arrow
52-
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
52+
[
53+
Parg_type
54+
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
55+
]
5356
(Ast_helper.Typ.any ()))
5457
[exp];
5558
pexp_attributes =
@@ -79,7 +82,7 @@ let handle_ffi ~loc ~payload =
7982
let effective_arity = if arity = 0 then 1 else arity in
8083
let args =
8184
Ext_list.init effective_arity (fun _ ->
82-
({attrs = []; lbl = Nolabel; typ = any} : Parsetree.arg))
85+
Parsetree.Parg_type {attrs = []; lbl = Nolabel; typ = any})
8386
in
8487
Ast_helper.Typ.arrow ~loc args any
8588
in
@@ -95,7 +98,10 @@ let handle_ffi ~loc ~payload =
9598
~pval_prim:(Prim_name "#raw_expr")
9699
~pval_type:
97100
(Ast_helper.Typ.arrow
98-
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
101+
[
102+
Parg_type
103+
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
104+
]
99105
(Ast_helper.Typ.any ()))
100106
[exp];
101107
pexp_attributes =
@@ -115,7 +121,10 @@ let handle_raw_structure loc payload =
115121
~pval_prim:(Prim_name "#raw_stmt")
116122
~pval_type:
117123
(Ast_helper.Typ.arrow
118-
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
124+
[
125+
Parg_type
126+
{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
127+
]
119128
(Ast_helper.Typ.any ()))
120129
[exp];
121130
}

‎compiler/frontend/ast_external_process.ml‎

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,31 @@ let spec_of_ptyp (nolabel : bool) (ptyp : Parsetree.core_type) :
6666
if nolabel then Extern_unit else Nothing
6767
| _ -> Nothing)
6868

69+
let arg_cst_of_as_const :
70+
Ast_attributes.as_const_payload -> External_arg_spec.attr = function
71+
| Int i ->
72+
(* This type is used in obj only to construct obj type*)
73+
Arg_cst (External_arg_spec.cst_int i)
74+
| Str s -> Arg_cst (External_arg_spec.cst_string s)
75+
| Json s -> Arg_cst (External_arg_spec.cst_json s)
76+
6977
(* is_optional = false
7078
*)
7179
let refine_arg_type ~(nolabel : bool) (ptyp : Ast_core_type.t) :
7280
External_arg_spec.attr =
73-
if ptyp.ptyp_desc = Ptyp_any then
81+
if ptyp.ptyp_desc = Ptyp_any then (
7482
let ptyp_attrs = ptyp.ptyp_attributes in
7583
let payload = Ast_attributes.as_const ptyp_attrs in
7684
match payload with
7785
| None -> spec_of_ptyp nolabel ptyp
78-
| Some cst -> (
86+
| Some cst ->
7987
(* (_[@as ])*)
8088
(* when ppx start dropping attributes
8189
we should warn, there is a trade off whether
8290
we should warn dropped non bs attribute or not
8391
*)
8492
Bs_ast_invariant.warn_discarded_unused_attributes ptyp_attrs;
85-
match cst with
86-
| Int i ->
87-
(* This type is used in obj only to construct obj type*)
88-
Arg_cst (External_arg_spec.cst_int i)
89-
| Str s -> Arg_cst (External_arg_spec.cst_string s)
90-
| Json s -> Arg_cst (External_arg_spec.cst_json s))
93+
arg_cst_of_as_const cst)
9194
else (* ([`a|`b] [@string]) *)
9295
spec_of_ptyp nolabel ptyp
9396

@@ -103,14 +106,7 @@ let refine_obj_arg_type ~(nolabel : bool) (ptyp : Ast_core_type.t) :
103106
Bs_ast_invariant.warn_discarded_unused_attributes ptyp_attrs;
104107
match payload with
105108
| None -> Bs_syntaxerr.err ptyp.ptyp_loc Invalid_underscore_type_in_external
106-
| Some (Int i) ->
107-
(* @as(24) *)
108-
(* This type is used in obj only to construct obj type *)
109-
Arg_cst (External_arg_spec.cst_int i)
110-
| Some (Str s) ->
111-
(* @as("foo") *)
112-
Arg_cst (External_arg_spec.cst_string s)
113-
| Some (Json s) -> Arg_cst (External_arg_spec.cst_json s))
109+
| Some cst -> arg_cst_of_as_const cst)
114110
else (* ([`a|`b] [@string]) *)
115111
spec_of_ptyp nolabel ptyp
116112

@@ -403,6 +399,26 @@ type response = {
403399
no_inline_cross_module: bool;
404400
}
405401

402+
(* A fixed argument means what [@as(value) _] means, so decode it with the
403+
same reader. *)
404+
let fixed_arg_type
405+
((value, loc) : Parsetree.fixed_value Location.loc * Location.t) =
406+
match Ast_attributes.as_const [Ast_fixed_arg.to_attribute ~loc value] with
407+
| Some cst -> arg_cst_of_as_const cst
408+
| None -> Bs_syntaxerr.err loc Expect_int_or_string_or_json_literal
409+
410+
(* The label and type of an argument. A fixed argument is typed as its [_], so
411+
the checks for [@as(value) _] apply to it unchanged. Its location ends with
412+
that [_]. *)
413+
let arg_parts (arg : Parsetree.arg) =
414+
match arg with
415+
| Parg_type {attrs; lbl; typ} -> (lbl, attrs, typ, None)
416+
| Parg_fixed {attrs; lbl; value; loc} ->
417+
( lbl,
418+
attrs,
419+
Ast_helper.Typ.any ~loc:(Ast_fixed_arg.underscore_loc loc) (),
420+
Some (value, loc) )
421+
406422
let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
407423
(arg_types_ty : Parsetree.arg list) (result_type : Ast_core_type.t) :
408424
int * Parsetree.core_type * External_ffi_types.t =
@@ -434,8 +450,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
434450
param_type
435451
(arg_labels, (arg_types : Parsetree.arg list), result_types)
436452
->
437-
let arg_label = param_type.lbl in
438-
let ty = param_type.typ in
453+
let arg_label, param_attrs, ty, fixed = arg_parts param_type in
439454
let new_arg_label, new_arg_types, output_tys =
440455
match arg_label with
441456
| Nolabel -> (
@@ -449,11 +464,15 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
449464
"expect label, optional, or unit here")
450465
| Labelled {txt = label} -> (
451466
let field_name =
452-
match Ast_attributes.as_string param_type.attrs with
467+
match Ast_attributes.as_string param_attrs with
453468
| Some alias -> alias
454469
| None -> label
455470
in
456-
let obj_arg_type = refine_obj_arg_type ~nolabel:false ty in
471+
let obj_arg_type =
472+
match fixed with
473+
| Some fixed -> fixed_arg_type fixed
474+
| None -> refine_obj_arg_type ~nolabel:false ty
475+
in
457476
match obj_arg_type with
458477
| Ignore ->
459478
( External_arg_spec.empty_kind obj_arg_type,
@@ -506,7 +525,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
506525
"%@obj label %s does not support %@unwrap arguments" label)
507526
| Optional {txt = label} -> (
508527
let field_name =
509-
match Ast_attributes.as_string param_type.attrs with
528+
match Ast_attributes.as_string param_attrs with
510529
| Some alias -> alias
511530
| None -> label
512531
in
@@ -914,8 +933,7 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
914933
Ext_list.fold_right arg_types_ty
915934
(([], [], 0) : External_arg_spec.params * Parsetree.arg list * int)
916935
(fun param_type (arg_type_specs, arg_types, i) ->
917-
let arg_label = param_type.lbl in
918-
let ty = param_type.typ in
936+
let arg_label, _, ty, fixed = arg_parts param_type in
919937
(if i = 0 && splice then
920938
match arg_label with
921939
| Optional _ ->
@@ -948,14 +966,22 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
948966
s
949967
| _ -> (Arg_optional, arg_type, param_type :: arg_types))
950968
| Labelled _ -> (
951-
let arg_type = refine_arg_type ~nolabel:false ty in
969+
let arg_type =
970+
match fixed with
971+
| Some fixed -> fixed_arg_type fixed
972+
| None -> refine_arg_type ~nolabel:false ty
973+
in
952974
( Arg_label,
953975
arg_type,
954976
match arg_type with
955977
| Arg_cst _ -> arg_types
956978
| _ -> param_type :: arg_types ))
957979
| Nolabel -> (
958-
let arg_type = refine_arg_type ~nolabel:true ty in
980+
let arg_type =
981+
match fixed with
982+
| Some fixed -> fixed_arg_type fixed
983+
| None -> refine_arg_type ~nolabel:true ty
984+
in
959985
( Arg_empty,
960986
arg_type,
961987
match arg_type with
@@ -977,7 +1003,9 @@ let handle_attributes (loc : Location.t) (type_annotation : Parsetree.core_type)
9771003
(Location.mkloc (Longident.Lident "unit") loc)
9781004
[]
9791005
in
980-
let unit_arg = {Parsetree.attrs = []; lbl = Nolabel; typ = unit_type} in
1006+
let unit_arg =
1007+
Parsetree.Parg_type {attrs = []; lbl = Nolabel; typ = unit_type}
1008+
in
9811009
( [unit_arg],
9821010
arg_type_specs
9831011
@ [{External_arg_spec.arg_label = Arg_empty; arg_type = Extern_unit}]

‎compiler/frontend/bs_builtin_ppx.ml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
142142
~pval_prim:(Prim_name "#raw_expr")
143143
~pval_type:
144144
(Ast_helper.Typ.arrow
145-
[{attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()}]
145+
[
146+
Parg_type {attrs = []; lbl = Nolabel; typ = Ast_helper.Typ.any ()};
147+
]
146148
(Ast_helper.Typ.any ()))
147149
[Ast_helper.Exp.constant ~loc (Pconst_raw_source source)]
148150
in

‎compiler/ml/ast_fixed_arg.ml‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
open Parsetree
2+
3+
let of_payload (payload : payload) : fixed_value Location.loc option =
4+
match payload with
5+
| PStr [{pstr_desc = Pstr_eval ({pexp_desc; pexp_loc = loc}, _); _}] -> (
6+
let fixed txt = Some {Location.txt; loc} in
7+
match pexp_desc with
8+
| Pexp_constant (Pconst_integer (source, None)) -> fixed (Pfixed_int source)
9+
| Pexp_constant (Pconst_string payload) -> fixed (Pfixed_string payload)
10+
| Pexp_template {source_segments = [{txt = source}]; values = []} ->
11+
fixed (Pfixed_template source)
12+
| Pexp_constant (Pconst_json source) -> fixed (Pfixed_json source)
13+
| _ -> None)
14+
| _ -> None
15+
16+
let to_expression ?(loc = Location.none) (value : fixed_value) : expression =
17+
match value with
18+
| Pfixed_int source ->
19+
Ast_helper.Exp.constant ~loc (Pconst_integer (source, None))
20+
| Pfixed_string payload ->
21+
Ast_helper.Exp.constant ~loc (Pconst_string payload)
22+
| Pfixed_template source ->
23+
Ast_helper.Exp.template ~loc [Location.mkloc source loc] []
24+
| Pfixed_json source -> Ast_helper.Exp.constant ~loc (Pconst_json source)
25+
26+
let to_attribute ?(loc = Location.none) (value : fixed_value Location.loc) :
27+
attribute =
28+
( Location.mkloc "as" loc,
29+
PStr
30+
[
31+
Ast_helper.Str.eval ~loc:value.loc
32+
(to_expression ~loc:value.loc value.txt);
33+
] )
34+
35+
(* [@as(value) _] on a direct argument becomes [Parg_fixed]. An argument with
36+
an invalid payload stays as written, so external processing reports it as
37+
before. So do the rare arguments that [Parg_fixed] could not print back as
38+
written: a [_] with other attributes too, and a braced payload such as
39+
[@as({1})]. *)
40+
let normalize_arg (arg : arg) : arg =
41+
match arg with
42+
| Parg_type
43+
{attrs; lbl; typ = {ptyp_desc = Ptyp_any; ptyp_loc; ptyp_attributes}} -> (
44+
let rec split before = function
45+
| [] -> None
46+
| (({txt = "as"; loc}, payload) : attribute) :: after ->
47+
Some (List.rev before, loc, payload, after)
48+
| attr :: after -> split (attr :: before) after
49+
in
50+
match split [] ptyp_attributes with
51+
| Some ([], as_loc, payload, []) -> (
52+
match of_payload payload with
53+
| None -> arg
54+
| Some value ->
55+
let loc =
56+
if as_loc = Location.none then ptyp_loc
57+
else {as_loc with loc_end = ptyp_loc.loc_end}
58+
in
59+
Parg_fixed {attrs; lbl; value; loc})
60+
| Some _ | None -> arg)
61+
| Parg_type _ | Parg_fixed _ -> arg
62+
63+
let underscore_loc (loc : Location.t) =
64+
if loc.loc_ghost then loc
65+
else
66+
{
67+
loc with
68+
loc_start = {loc.loc_end with pos_cnum = loc.loc_end.pos_cnum - 1};
69+
}
70+
71+
let normalize_external_type (typ : core_type) : core_type =
72+
match typ.ptyp_desc with
73+
| Ptyp_arrow {params; ret} ->
74+
{
75+
typ with
76+
ptyp_desc = Ptyp_arrow {params = List.map normalize_arg params; ret};
77+
}
78+
| _ -> typ

0 commit comments

Comments
 (0)