Skip to content

Commit afa723f

Browse files
authored
Remove res.namedArgLoc attribute and store the location information directly into the label. (#7247)
* Experiment with storing the location of function named arguments in the AST. * Store label location in type argument instead of attribute. * Store the label loc directly in the label, for application for now. * restore some punning behaviour Preserve `isMobile={isMobile}` when it's like that in the source, instead of reformatting to `isMobile`. Do we want that? * Store location directly in the label on Ptyp_arrow instead of a separate field. * Store location directly in the label on `Pexp_fun`. * Put type without locs inside module Noloc. * Restore original variant names. * Update CHANGELOG.md
1 parent 7105216 commit afa723f

File tree

73 files changed

+677
-807
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+677
-807
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
- AST cleanup: represent concatenation (`++`) and (dis)equality operators (`==`, `===`, `!=`, `!==`) just like in the syntax. https://github.com/rescript-lang/rescript/pull/7248
3434
- AST cleanup: use inline record for `Ptyp_arrow`. https://github.com/rescript-lang/rescript/pull/7250
3535
- Playground: Bundle stdlib runtime so that the playground can execute functions from Core/Belt/Js. (#7255)
36+
- AST cleanup: Remove `res.namedArgLoc` attribute and store the location information directly into the label. https://github.com/rescript-lang/rescript/pull/7247
3637

3738
#### :nail_care: Polish
38-
3939
- Rewatch 1.0.10. https://github.com/rescript-lang/rescript/pull/7259
4040

4141
# 12.0.0-alpha.7

analysis/reanalyze/src/Arnold.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ module ExtendFunctionTable = struct
582582
Texp_apply {funct = {exp_desc = Texp_ident (path, {loc}, _)}; args};
583583
}
584584
when kindOpt <> None ->
585-
let checkArg ((argLabel : Asttypes.arg_label), _argOpt) =
585+
let checkArg ((argLabel : Asttypes.Noloc.arg_label), _argOpt) =
586586
match (argLabel, kindOpt) with
587587
| (Labelled l | Optional l), Some kind ->
588588
kind |> List.for_all (fun {Kind.label} -> label <> l)
@@ -624,7 +624,7 @@ module ExtendFunctionTable = struct
624624
when callee |> FunctionTable.isInFunctionInTable ~functionTable ->
625625
let functionName = Path.name callee in
626626
args
627-
|> List.iter (fun ((argLabel : Asttypes.arg_label), argOpt) ->
627+
|> List.iter (fun ((argLabel : Asttypes.Noloc.arg_label), argOpt) ->
628628
match (argLabel, argOpt |> extractLabelledArgument) with
629629
| Labelled label, Some (path, loc)
630630
when path |> FunctionTable.isInFunctionInTable ~functionTable
@@ -672,7 +672,7 @@ module CheckExpressionWellFormed = struct
672672
->
673673
let functionName = Path.name functionPath in
674674
args
675-
|> List.iter (fun ((argLabel : Asttypes.arg_label), argOpt) ->
675+
|> List.iter (fun ((argLabel : Asttypes.Noloc.arg_label), argOpt) ->
676676
match argOpt |> ExtendFunctionTable.extractLabelledArgument with
677677
| Some (path, loc) -> (
678678
match argLabel with
@@ -761,7 +761,7 @@ module Compile = struct
761761
let argsFromKind =
762762
innerFunctionDefinition.kind
763763
|> List.map (fun (entry : Kind.entry) ->
764-
( Asttypes.Labelled entry.label,
764+
( Asttypes.Noloc.Labelled entry.label,
765765
Some
766766
{
767767
expr with
@@ -785,7 +785,7 @@ module Compile = struct
785785
args
786786
|> List.find_opt (fun arg ->
787787
match arg with
788-
| Asttypes.Labelled s, Some _ -> s = label
788+
| Asttypes.Noloc.Labelled s, Some _ -> s = label
789789
| _ -> false)
790790
in
791791
let argOpt =

analysis/reanalyze/src/DeadValue.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ let processOptionalArgs ~expType ~(locFrom : Location.t) ~locTo ~path args =
104104
| None -> Some false
105105
in
106106
match lbl with
107-
| Asttypes.Optional s when not locFrom.loc_ghost ->
107+
| Asttypes.Noloc.Optional s when not locFrom.loc_ghost ->
108108
if argIsSupplied <> Some false then supplied := s :: !supplied;
109109
if argIsSupplied = None then suppliedMaybe := s :: !suppliedMaybe
110110
| _ -> ());

analysis/src/CompletionBackEnd.ml

+4-3
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
952952
(* compute the application of the first label, then the next ones *)
953953
let args = processApply args [label] in
954954
processApply args nextLabels
955-
| (Asttypes.Nolabel, _) :: nextArgs, [Asttypes.Nolabel] -> nextArgs
955+
| (Asttypes.Noloc.Nolabel, _) :: nextArgs, [Asttypes.Noloc.Nolabel] ->
956+
nextArgs
956957
| ((Labelled _, _) as arg) :: nextArgs, [Nolabel] ->
957958
arg :: processApply nextArgs labels
958959
| (Optional _, _) :: nextArgs, [Nolabel] -> processApply nextArgs labels
@@ -1007,9 +1008,9 @@ and getCompletionsForContextPath ~debug ~full ~opens ~rawOpens ~pos ~env ~exact
10071008
synthetic = true;
10081009
contextPath =
10091010
(match cp with
1010-
| CPApply (c, args) -> CPApply (c, args @ [Asttypes.Nolabel])
1011+
| CPApply (c, args) -> CPApply (c, args @ [Asttypes.Noloc.Nolabel])
10111012
| CPId _ when TypeUtils.isFunctionType ~env ~package typ ->
1012-
CPApply (cp, [Asttypes.Nolabel])
1013+
CPApply (cp, [Asttypes.Noloc.Nolabel])
10131014
| _ -> cp);
10141015
id = fieldName;
10151016
inJsx = false;

analysis/src/CompletionFrontEnd.ml

+6-3
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,10 @@ let rec exprToContextPathInner (e : Parsetree.expression) =
298298
| Pexp_apply {funct = e1; args} -> (
299299
match exprToContextPath e1 with
300300
| None -> None
301-
| Some contexPath -> Some (CPApply (contexPath, args |> List.map fst)))
301+
| Some contexPath ->
302+
Some
303+
(CPApply (contexPath, args |> List.map fst |> List.map Asttypes.to_noloc))
304+
)
302305
| Pexp_tuple exprs ->
303306
let exprsAsContextPaths = exprs |> List.filter_map exprToContextPath in
304307
if List.length exprs = List.length exprsAsContextPaths then
@@ -1446,8 +1449,8 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
14461449
(match lbl with
14471450
| Nolabel ->
14481451
Unlabelled {argumentPosition = currentUnlabelledCount}
1449-
| Optional name -> Optional name
1450-
| Labelled name -> Labelled name);
1452+
| Optional {txt = name} -> Optional name
1453+
| Labelled {txt = name} -> Labelled name);
14511454
})
14521455
in
14531456
(match defaultExpOpt with

analysis/src/CompletionJsx.ml

+6-7
Original file line numberDiff line numberDiff line change
@@ -465,20 +465,19 @@ let extractJsxProps ~(compName : Longident.t Location.loc) ~args =
465465
in
466466
let rec processProps ~acc args =
467467
match args with
468-
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
468+
| (Asttypes.Labelled {txt = "children"}, {Parsetree.pexp_loc}) :: _ ->
469469
{
470470
compName;
471471
props = List.rev acc;
472472
childrenStart =
473473
(if pexp_loc.loc_ghost then None else Some (Loc.start pexp_loc));
474474
}
475-
| ((Labelled s | Optional s), (eProp : Parsetree.expression)) :: rest -> (
476-
let namedArgLoc =
477-
eProp.pexp_attributes
478-
|> List.find_opt (fun ({Asttypes.txt}, _) -> txt = "res.namedArgLoc")
479-
in
475+
| ( (Labelled {txt = s; loc} | Optional {txt = s; loc}),
476+
(eProp : Parsetree.expression) )
477+
:: rest -> (
478+
let namedArgLoc = if loc = Location.none then None else Some loc in
480479
match namedArgLoc with
481-
| Some ({loc}, _) ->
480+
| Some loc ->
482481
processProps
483482
~acc:
484483
({

analysis/src/CreateInterface.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ let printSignature ~extractor ~signature =
170170
in
171171
let lblName = labelDecl.ld_id |> Ident.name in
172172
let lbl =
173-
if labelDecl.ld_optional then Asttypes.Optional lblName
173+
if labelDecl.ld_optional then Asttypes.Noloc.Optional lblName
174174
else Labelled lblName
175175
in
176176
{

analysis/src/DumpAst.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,8 @@ and printExprItem expr ~pos ~indentation =
219219
^ "arg: "
220220
^ (match arg with
221221
| Nolabel -> "Nolabel"
222-
| Labelled name -> "Labelled(" ^ name ^ ")"
223-
| Optional name -> "Optional(" ^ name ^ ")")
222+
| Labelled {txt = name} -> "Labelled(" ^ name ^ ")"
223+
| Optional {txt = name} -> "Optional(" ^ name ^ ")")
224224
^ ",\n"
225225
^ addIndentation (indentation + 2)
226226
^ "pattern: "

analysis/src/SemanticTokens.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ let command ~debug ~emitter ~path =
266266

267267
let posOfGreatherthanAfterProps =
268268
let rec loop = function
269-
| (Asttypes.Labelled "children", {Parsetree.pexp_loc}) :: _ ->
269+
| (Asttypes.Labelled {txt = "children"}, {Parsetree.pexp_loc}) :: _ ->
270270
Loc.start pexp_loc
271271
| _ :: args -> loop args
272272
| [] -> (* should not happen *) (-1, -1)

analysis/src/SharedTypes.ml

+8-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let ident l = l |> List.map str |> String.concat "."
44

55
type path = string list
66

7-
type typedFnArg = Asttypes.arg_label * Types.type_expr
7+
type typedFnArg = Asttypes.Noloc.arg_label * Types.type_expr
88

99
let pathToString (path : path) = path |> String.concat "."
1010

@@ -605,7 +605,7 @@ module Completable = struct
605605
| CPFloat
606606
| CPBool
607607
| CPOption of contextPath
608-
| CPApply of contextPath * Asttypes.arg_label list
608+
| CPApply of contextPath * Asttypes.Noloc.arg_label list
609609
| CPId of {
610610
path: string list;
611611
completionContext: completionContext;
@@ -692,7 +692,7 @@ module Completable = struct
692692
contextPathToString cp ^ "("
693693
^ (labels
694694
|> List.map (function
695-
| Asttypes.Nolabel -> "Nolabel"
695+
| Asttypes.Noloc.Nolabel -> "Nolabel"
696696
| Labelled s -> "~" ^ s
697697
| Optional s -> "?" ^ s)
698698
|> String.concat ", ")
@@ -898,14 +898,12 @@ type arg = {label: label; exp: Parsetree.expression}
898898
let extractExpApplyArgs ~args =
899899
let rec processArgs ~acc args =
900900
match args with
901-
| (((Asttypes.Labelled s | Optional s) as label), (e : Parsetree.expression))
901+
| ( ((Asttypes.Labelled {txt = s; loc} | Optional {txt = s; loc}) as label),
902+
(e : Parsetree.expression) )
902903
:: rest -> (
903-
let namedArgLoc =
904-
e.pexp_attributes
905-
|> List.find_opt (fun ({Asttypes.txt}, _) -> txt = "res.namedArgLoc")
906-
in
904+
let namedArgLoc = if loc = Location.none then None else Some loc in
907905
match namedArgLoc with
908-
| Some ({loc}, _) ->
906+
| Some loc ->
909907
let labelled =
910908
{
911909
name = s;
@@ -919,7 +917,7 @@ let extractExpApplyArgs ~args =
919917
in
920918
processArgs ~acc:({label = Some labelled; exp = e} :: acc) rest
921919
| None -> processArgs ~acc rest)
922-
| (Asttypes.Nolabel, (e : Parsetree.expression)) :: rest ->
920+
| (Nolabel, (e : Parsetree.expression)) :: rest ->
923921
if e.pexp_loc.loc_ghost then processArgs ~acc rest
924922
else processArgs ~acc:({label = None; exp = e} :: acc) rest
925923
| [] -> List.rev acc

analysis/src/SignatureHelp.ml

+6-5
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ let findActiveParameter ~argAtCursor ~args =
154154
(* If a function only has one, unlabelled argument, we can safely assume that's active whenever we're in the signature help for that function,
155155
even if we technically didn't find anything at the cursor (which we don't for empty expressions). *)
156156
match args with
157-
| [(Asttypes.Nolabel, _)] -> Some 0
157+
| [(Asttypes.Noloc.Nolabel, _)] -> Some 0
158158
| _ -> None)
159159
| Some (Unlabelled unlabelledArgumentIndex) ->
160160
let index = ref 0 in
161161
args
162162
|> List.find_map (fun (label, _) ->
163163
match label with
164-
| Asttypes.Nolabel when !index = unlabelledArgumentIndex ->
164+
| Asttypes.Noloc.Nolabel when !index = unlabelledArgumentIndex ->
165165
Some !index
166166
| _ ->
167167
index := !index + 1;
@@ -171,7 +171,7 @@ let findActiveParameter ~argAtCursor ~args =
171171
args
172172
|> List.find_map (fun (label, _) ->
173173
match label with
174-
| (Asttypes.Labelled labelName | Optional labelName)
174+
| (Asttypes.Noloc.Labelled labelName | Optional labelName)
175175
when labelName = name ->
176176
Some !index
177177
| _ ->
@@ -474,6 +474,7 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
474474
parameters =
475475
parameters
476476
|> List.map (fun (argLabel, start, end_) ->
477+
let argLabel = Asttypes.to_noloc argLabel in
477478
let paramArgCount = !paramUnlabelledArgCount in
478479
paramUnlabelledArgCount := paramArgCount + 1;
479480
let unlabelledArgCount = ref 0 in
@@ -486,8 +487,8 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
486487
let argCount = !unlabelledArgCount in
487488
unlabelledArgCount := argCount + 1;
488489
match (lbl, argLabel) with
489-
| ( Asttypes.Optional l1,
490-
Asttypes.Optional l2 )
490+
| ( Asttypes.Noloc.Optional l1,
491+
Asttypes.Noloc.Optional l2 )
491492
when l1 = l2 ->
492493
true
493494
| Labelled l1, Labelled l2

analysis/src/TypeUtils.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ let getFirstFnUnlabelledArgType ~env ~full t =
11231123
in
11241124
let rec findFirstUnlabelledArgType labels =
11251125
match labels with
1126-
| (Asttypes.Nolabel, t) :: _ -> Some t
1126+
| (Asttypes.Noloc.Nolabel, t) :: _ -> Some t
11271127
| _ :: rest -> findFirstUnlabelledArgType rest
11281128
| [] -> None
11291129
in

compiler/frontend/ast_compatible.ml

+13-6
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,29 @@ let apply_labels ?(loc = default_loc) ?(attrs = []) fn
117117
Pexp_apply
118118
{
119119
funct = fn;
120-
args = Ext_list.map args (fun (l, a) -> (Asttypes.Labelled l, a));
120+
args =
121+
Ext_list.map args (fun (l, a) ->
122+
(Asttypes.Labelled {txt = l; loc = Location.none}, a));
121123
partial = false;
122124
};
123125
}
124126

125-
let label_arrow ?(loc = default_loc) ?(attrs = []) ~arity s arg ret : core_type
126-
=
127+
let label_arrow ?(loc = default_loc) ?(attrs = []) ~arity txt arg ret :
128+
core_type =
127129
{
128-
ptyp_desc = Ptyp_arrow {lbl = Labelled s; arg; ret; arity};
130+
ptyp_desc =
131+
Ptyp_arrow
132+
{lbl = Asttypes.Labelled {txt; loc = default_loc}; arg; ret; arity};
129133
ptyp_loc = loc;
130134
ptyp_attributes = attrs;
131135
}
132136

133-
let opt_arrow ?(loc = default_loc) ?(attrs = []) ~arity s arg ret : core_type =
137+
let opt_arrow ?(loc = default_loc) ?(attrs = []) ~arity txt arg ret : core_type
138+
=
134139
{
135-
ptyp_desc = Ptyp_arrow {lbl = Asttypes.Optional s; arg; ret; arity};
140+
ptyp_desc =
141+
Ptyp_arrow
142+
{lbl = Asttypes.Optional {txt; loc = default_loc}; arg; ret; arity};
136143
ptyp_loc = loc;
137144
ptyp_attributes = attrs;
138145
}

compiler/frontend/ast_external_process.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
471471
| _ ->
472472
Location.raise_errorf ~loc
473473
"expect label, optional, or unit here")
474-
| Labelled label -> (
474+
| Labelled {txt = label} -> (
475475
let field_name =
476476
match
477477
Ast_attributes.iter_process_bs_string_as param_type.attr
@@ -530,7 +530,7 @@ let process_obj (loc : Location.t) (st : external_desc) (prim_name : string)
530530
| Unwrap ->
531531
Location.raise_errorf ~loc
532532
"%@obj label %s does not support %@unwrap arguments" label)
533-
| Optional label -> (
533+
| Optional {txt = label} -> (
534534
let field_name =
535535
match
536536
Ast_attributes.iter_process_bs_string_as param_type.attr
@@ -983,7 +983,7 @@ let handle_attributes (loc : Bs_loc.t) (type_annotation : Parsetree.core_type)
983983
arg_type,
984984
new_arg_types ) =
985985
match arg_label with
986-
| Optional s -> (
986+
| Optional {txt = s} -> (
987987
let arg_type = get_opt_arg_type ~nolabel:false ty in
988988
match arg_type with
989989
| Poly_var _ ->

compiler/gentype/TranslateCoreType.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
open GenTypeCommon
22
open! TranslateTypeExprFromTypes
33

4-
let remove_option ~(label : Asttypes.arg_label)
4+
let remove_option ~(label : Asttypes.Noloc.arg_label)
55
(core_type : Typedtree.core_type) =
66
match (core_type.ctyp_desc, label) with
77
| Ttyp_constr (Path.Pident id, _, [t]), Optional lbl

compiler/gentype/TranslateTypeExprFromTypes.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ open GenTypeCommon
22

33
type translation = {dependencies: dep list; type_: type_}
44

5-
let rec remove_option ~(label : Asttypes.arg_label)
5+
let rec remove_option ~(label : Asttypes.Noloc.arg_label)
66
(type_expr : Types.type_expr) =
77
match (type_expr.desc, label) with
88
| Tconstr (Path.Pident id, [t], _), Optional lbl when Ident.name id = "option"

compiler/ml/ast_helper.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ module Typ = struct
8282
| Ptyp_var x ->
8383
check_variable var_names t.ptyp_loc x;
8484
Ptyp_var x
85-
| Ptyp_arrow {lbl = label; arg; ret; arity = a} ->
86-
Ptyp_arrow {lbl = label; arg = loop arg; ret = loop ret; arity = a}
85+
| Ptyp_arrow ({arg; ret} as arr) ->
86+
Ptyp_arrow {arr with arg = loop arg; ret = loop ret}
8787
| Ptyp_tuple lst -> Ptyp_tuple (List.map loop lst)
8888
| Ptyp_constr ({txt = Longident.Lident s}, []) when List.mem s var_names
8989
->

compiler/ml/ast_mapper_from0.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ module T = struct
9999
| Ptyp_any -> any ~loc ~attrs ()
100100
| Ptyp_var s -> var ~loc ~attrs s
101101
| Ptyp_arrow (lab, t1, t2) ->
102+
let lab = Asttypes.to_arg_label lab in
102103
arrow ~loc ~attrs ~arity:None lab (sub.typ sub t1) (sub.typ sub t2)
103104
| Ptyp_tuple tyl -> tuple ~loc ~attrs (List.map (sub.typ sub) tyl)
104105
| Ptyp_constr (lid, tl) -> (
@@ -304,6 +305,7 @@ module E = struct
304305
| Pexp_let (r, vbs, e) ->
305306
let_ ~loc ~attrs r (List.map (sub.value_binding sub) vbs) (sub.expr sub e)
306307
| Pexp_fun (lab, def, p, e) ->
308+
let lab = Asttypes.to_arg_label lab in
307309
let async = Ext_list.exists attrs (fun ({txt}, _) -> txt = "res.async") in
308310
fun_ ~loc ~attrs ~async ~arity:None lab
309311
(map_opt (sub.expr sub) def)
@@ -349,7 +351,9 @@ module E = struct
349351
in
350352
let partial, attrs = process_partial_app_attribute attrs in
351353
apply ~loc ~attrs ~partial (sub.expr sub e)
352-
(List.map (map_snd (sub.expr sub)) l)
354+
(List.map
355+
(fun (lbl, e) -> (Asttypes.to_arg_label lbl, sub.expr sub e))
356+
l)
353357
| Pexp_match (e, pel) ->
354358
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
355359
| Pexp_try (e, pel) -> try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)

0 commit comments

Comments
 (0)