Skip to content

Refactor the ast for record expressions and pattern. #7528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Remove deprecated pipe last (`|>`) syntax. https://github.com/rescript-lang/rescript/pull/7512
- Improve error message for pipe (`->`) syntax. https://github.com/rescript-lang/rescript/pull/7520
- Improve a few error messages around various subtyping issues. https://github.com/rescript-lang/rescript/pull/7404
- Refactor the ast for record expressions and patterns. https://github.com/rescript-lang/rescript/pull/7528

# 12.0.0-alpha.13

Expand Down
30 changes: 14 additions & 16 deletions analysis/src/CompletionExpressions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,21 @@ let rec traverseExpr (exp : Parsetree.expression) ~exprPath ~pos
| Pexp_record (fields, _) -> (
let fieldWithCursor = ref None in
let fieldWithExprHole = ref None in
fields
|> List.iter (fun (fname, exp, _) ->
match
( fname.Location.txt,
exp.Parsetree.pexp_loc |> CursorPosition.classifyLoc ~pos )
with
| Longident.Lident fname, HasCursor ->
fieldWithCursor := Some (fname, exp)
| Lident fname, _ when isExprHole exp ->
fieldWithExprHole := Some (fname, exp)
| _ -> ());
Ext_list.iter fields (fun {lid = fname; x = exp} ->
match
( fname.Location.txt,
exp.Parsetree.pexp_loc |> CursorPosition.classifyLoc ~pos )
with
| Longident.Lident fname, HasCursor ->
fieldWithCursor := Some (fname, exp)
| Lident fname, _ when isExprHole exp ->
fieldWithExprHole := Some (fname, exp)
| _ -> ());
let seenFields =
fields
|> List.filter_map (fun (fieldName, _f, _) ->
match fieldName with
| {Location.txt = Longident.Lident fieldName} -> Some fieldName
| _ -> None)
Ext_list.filter_map fields (fun {lid = fieldName} ->
match fieldName with
| {Location.txt = Longident.Lident fieldName} -> Some fieldName
| _ -> None)
in
match (!fieldWithCursor, !fieldWithExprHole) with
| Some (fname, f), _ | None, Some (fname, f) -> (
Expand Down
21 changes: 10 additions & 11 deletions analysis/src/CompletionFrontEnd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -518,16 +518,15 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
:: patternPath)
?contextPath p
| Ppat_record (fields, _) ->
fields
|> List.iter (fun (fname, p, _) ->
match fname with
| {Location.txt = Longident.Lident fname} ->
scopePattern
~patternPath:
(Completable.NFollowRecordField {fieldName = fname}
:: patternPath)
?contextPath p
| _ -> ())
Ext_list.iter fields (fun {lid = fname; x = p} ->
match fname with
| {Location.txt = Longident.Lident fname} ->
scopePattern
~patternPath:
(Completable.NFollowRecordField {fieldName = fname}
:: patternPath)
?contextPath p
| _ -> ())
| Ppat_array pl ->
pl
|> List.iter
Expand Down Expand Up @@ -926,7 +925,7 @@ let completionWithParser1 ~currentFile ~debug ~offset ~path ~posCursor
( {
pexp_desc =
Pexp_record
(({txt = Lident "from"}, fromExpr, _) :: _, _);
({lid = {txt = Lident "from"}; x = fromExpr} :: _, _);
},
_ );
};
Expand Down
31 changes: 14 additions & 17 deletions analysis/src/CompletionPatterns.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,21 @@ and traversePattern (pat : Parsetree.pattern) ~patternPath ~locHasCursor
| Ppat_record (fields, _) -> (
let fieldWithCursor = ref None in
let fieldWithPatHole = ref None in
fields
|> List.iter (fun (fname, f, _) ->
match
( fname.Location.txt,
f.Parsetree.ppat_loc
|> CursorPosition.classifyLoc ~pos:posBeforeCursor )
with
| Longident.Lident fname, HasCursor ->
fieldWithCursor := Some (fname, f)
| Lident fname, _ when isPatternHole f ->
fieldWithPatHole := Some (fname, f)
| _ -> ());
Ext_list.iter fields (fun {lid = fname; x = f} ->
match
( fname.Location.txt,
f.Parsetree.ppat_loc
|> CursorPosition.classifyLoc ~pos:posBeforeCursor )
with
| Longident.Lident fname, HasCursor -> fieldWithCursor := Some (fname, f)
| Lident fname, _ when isPatternHole f ->
fieldWithPatHole := Some (fname, f)
| _ -> ());
let seenFields =
fields
|> List.filter_map (fun (fieldName, _f, _) ->
match fieldName with
| {Location.txt = Longident.Lident fieldName} -> Some fieldName
| _ -> None)
Ext_list.filter_map fields (fun {lid = fieldName} ->
match fieldName with
| {Location.txt = Longident.Lident fieldName} -> Some fieldName
| _ -> None)
in
match (!fieldWithCursor, !fieldWithPatHole) with
| Some (fname, f), _ | None, Some (fname, f) -> (
Expand Down
26 changes: 12 additions & 14 deletions analysis/src/DumpAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,12 @@ let rec printPattern pattern ~pos ~indentation =
"Ppat_record(\n"
^ addIndentation (indentation + 1)
^ "fields:\n"
^ (fields
|> List.map (fun ((Location.{txt} as loc), pat, _) ->
addIndentation (indentation + 2)
^ (loc |> printLocDenominatorLoc ~pos)
^ (Utils.flattenLongIdent txt |> ident |> str)
^ ": "
^ printPattern pat ~pos ~indentation:(indentation + 2))
^ (Ext_list.map fields (fun {lid; x = pat} ->
addIndentation (indentation + 2)
^ (lid |> printLocDenominatorLoc ~pos)
^ (Utils.flattenLongIdent lid.txt |> ident |> str)
^ ": "
^ printPattern pat ~pos ~indentation:(indentation + 2))
|> String.concat "\n")
^ "\n" ^ addIndentation indentation ^ ")"
| Ppat_tuple patterns ->
Expand Down Expand Up @@ -244,13 +243,12 @@ and printExprItem expr ~pos ~indentation =
"Pexp_record(\n"
^ addIndentation (indentation + 1)
^ "fields:\n"
^ (fields
|> List.map (fun ((Location.{txt} as loc), expr, _) ->
addIndentation (indentation + 2)
^ (loc |> printLocDenominatorLoc ~pos)
^ (Utils.flattenLongIdent txt |> ident |> str)
^ ": "
^ printExprItem expr ~pos ~indentation:(indentation + 2))
^ (Ext_list.map fields (fun {lid; x = expr} ->
addIndentation (indentation + 2)
^ (lid |> printLocDenominatorLoc ~pos)
^ (Utils.flattenLongIdent lid.txt |> ident |> str)
^ ": "
^ printExprItem expr ~pos ~indentation:(indentation + 2))
|> String.concat "\n")
^ "\n" ^ addIndentation indentation ^ ")"
| Pexp_tuple exprs ->
Expand Down
2 changes: 1 addition & 1 deletion analysis/src/Hint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ let inlay ~path ~pos ~maxLength ~debug =
match pat.ppat_desc with
| Ppat_tuple pl -> pl |> List.iter processPattern
| Ppat_record (fields, _) ->
fields |> List.iter (fun (_, p, _) -> processPattern p)
Ext_list.iter fields (fun {x = p} -> processPattern p)
| Ppat_array fields -> fields |> List.iter processPattern
| Ppat_var {loc} -> push loc Type
| _ -> ()
Expand Down
16 changes: 7 additions & 9 deletions analysis/src/SemanticTokens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,8 @@ let command ~debug ~emitter ~path =
(* Don't emit true or false *)
Ast_iterator.default_iterator.pat iterator p
| Ppat_record (cases, _) ->
cases
|> List.iter (fun (label, _, _) ->
emitter |> emitRecordLabel ~label ~debug);
Ext_list.iter cases (fun {lid = label} ->
emitter |> emitRecordLabel ~label ~debug);
Ast_iterator.default_iterator.pat iterator p
| Ppat_construct (name, _) ->
emitter |> emitVariant ~name ~debug;
Expand Down Expand Up @@ -320,12 +319,11 @@ let command ~debug ~emitter ~path =
emitter |> emitFromLoc ~loc ~type_:Operator;
Ast_iterator.default_iterator.expr iterator e
| Pexp_record (cases, _) ->
cases
|> List.filter_map (fun ((label : Longident.t Location.loc), _, _) ->
match label.txt with
| Longident.Lident s when not (Utils.isFirstCharUppercase s) ->
Some label
| _ -> None)
Ext_list.filter_map cases (fun {lid} ->
match lid.txt with
| Longident.Lident s when not (Utils.isFirstCharUppercase s) ->
Some lid
| _ -> None)
|> List.iter (fun label -> emitter |> emitRecordLabel ~label ~debug);
Ast_iterator.default_iterator.expr iterator e
| Pexp_field (_, label) | Pexp_setfield (_, label, _) ->
Expand Down
10 changes: 4 additions & 6 deletions analysis/src/SignatureHelp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,8 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
fields
|> List.find_map
(fun
(({loc; txt}, expr, _) :
Longident.t Location.loc
* Parsetree.expression
* bool)
({lid = {loc; txt}; x = expr} :
Parsetree.expression Parsetree.record_element)
->
if
posBeforeCursor >= Pos.ofLexing loc.loc_start
Expand Down Expand Up @@ -673,8 +671,8 @@ let signatureHelp ~path ~pos ~currentFile ~debug ~allowForConstructorPayloads =
fields
|> List.find_map
(fun
(({loc; txt}, pat, _) :
Longident.t Location.loc * Parsetree.pattern * bool)
({lid = {loc; txt}; x = pat} :
Parsetree.pattern Parsetree.record_element)
->
if
posBeforeCursor >= Pos.ofLexing loc.loc_start
Expand Down
4 changes: 2 additions & 2 deletions analysis/src/Xform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ module IfThenElse = struct
| None -> None
| Some patList -> Some (mkPat (Ppat_tuple patList)))
| Pexp_record (items, None) -> (
let itemToPat (x, e, o) =
let itemToPat {Parsetree.lid; x = e; opt} =
match expToPat e with
| None -> None
| Some p -> Some (x, p, o)
| Some p -> Some {Parsetree.lid; x = p; opt}
in
match listToPat ~itemToPat items with
| None -> None
Expand Down
2 changes: 1 addition & 1 deletion compiler/common/pattern_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let untype typed =
let fields =
List.map
(fun (_, lbl, p, opt) ->
(mknoloc (Longident.Lident lbl.lbl_name), loop p, opt))
{lid = mknoloc (Longident.Lident lbl.lbl_name); x = loop p; opt})
subpatterns
in
mkpat (Ppat_record (fields, closed_flag))
Expand Down
30 changes: 20 additions & 10 deletions compiler/frontend/ast_derive_js_mapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ let handle_config (config : Parsetree.expression option) =
match config.pexp_desc with
| Pexp_record
( [
( {txt = Lident "newType"},
{
pexp_desc =
( Pexp_construct
({txt = Lident (("true" | "false") as x)}, None)
| Pexp_ident {txt = Lident ("newType" as x)} );
},
_ );
{
lid = {txt = Lident "newType"};
x =
{
pexp_desc =
( Pexp_construct
({txt = Lident (("true" | "false") as x)}, None)
| Pexp_ident {txt = Lident ("newType" as x)} );
};
};
],
None ) ->
not (x = "false")
Expand Down Expand Up @@ -196,7 +198,11 @@ let init () =
txt = Longident.Lident txt;
}
in
(label, Exp.field exp_param label, false)))
{
Parsetree.lid = label;
x = Exp.field exp_param label;
opt = false;
}))
None);
] ))
in
Expand All @@ -208,7 +214,11 @@ let init () =
let label =
{Asttypes.loc; txt = Longident.Lident txt}
in
(label, js_field exp_param label, false)))
{
Parsetree.lid = label;
x = js_field exp_param label;
opt = false;
}))
None
in
let from_js =
Expand Down
52 changes: 20 additions & 32 deletions compiler/frontend/ast_external_process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,14 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
] -> (
let from_name = ref None in
let with_ = ref None in
fields
|> List.iter
(fun
((l, exp, _) :
Longident.t Location.loc * Parsetree.expression * bool)
->
match (l, exp.pexp_desc) with
| ( {txt = Lident "from"},
Pexp_constant (Pconst_string (s, _)) ) ->
from_name := Some s
| {txt = Lident "with"}, Pexp_record (fields, _) ->
with_ := Some fields
| _ -> ());
Ext_list.iter fields (fun {lid = l; x = exp} ->
match (l, exp.pexp_desc) with
| {txt = Lident "from"}, Pexp_constant (Pconst_string (s, _))
->
from_name := Some s
| {txt = Lident "with"}, Pexp_record (fields, _) ->
with_ := Some fields
| _ -> ());
match (!from_name, !with_) with
| None, _ ->
Location.raise_errorf ~loc:pexp_loc
Expand All @@ -287,25 +282,18 @@ let parse_external_attributes (no_arguments : bool) (prim_name_check : string)
the import attributes you want applied to the import."
| Some from_name, Some with_fields ->
let import_attributes_from_record =
with_fields
|> List.filter_map
(fun
((l, exp, _) :
Longident.t Location.loc
* Parsetree.expression
* bool)
->
match exp.pexp_desc with
| Pexp_constant (Pconst_string (s, _)) -> (
match l.txt with
| Longident.Lident "type_" -> Some ("type", s)
| Longident.Lident txt -> Some (txt, s)
| _ ->
Location.raise_errorf ~loc:exp.pexp_loc
"Field must be a regular key.")
| _ ->
Location.raise_errorf ~loc:exp.pexp_loc
"Only string values are allowed here.")
Ext_list.filter_map with_fields (fun {lid = l; x = exp} ->
match exp.pexp_desc with
| Pexp_constant (Pconst_string (s, _)) -> (
match l.txt with
| Longident.Lident "type_" -> Some ("type", s)
| Longident.Lident txt -> Some (txt, s)
| _ ->
Location.raise_errorf ~loc:exp.pexp_loc
"Field must be a regular key.")
| _ ->
Location.raise_errorf ~loc:exp.pexp_loc
"Only string values are allowed here.")
in
let import_attributes =
Hashtbl.create (List.length import_attributes_from_record)
Expand Down
2 changes: 1 addition & 1 deletion compiler/frontend/ast_tuple_pattern_flatten.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ let flattern_tuple_pattern_vb (self : Bs_ast_mapper.mapper)
:: acc)
| _ -> {pvb_pat; pvb_expr; pvb_loc = vb.pvb_loc; pvb_attributes} :: acc)
| Ppat_record (lid_pats, _), Pexp_pack {pmod_desc = Pmod_ident id} ->
Ext_list.map_append lid_pats acc (fun (lid, pat, _) ->
Ext_list.map_append lid_pats acc (fun {lid; x = pat} ->
match lid.txt with
| Lident s ->
{
Expand Down
8 changes: 5 additions & 3 deletions compiler/frontend/ast_uncurry_gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ let to_method_callback loc (self : Bs_ast_mapper.mapper) label
Exp.constraint_ ~loc
(Exp.record ~loc
[
( {loc; txt = Ast_literal.Lid.hidden_field arity_s},
body,
false );
{
lid = {loc; txt = Ast_literal.Lid.hidden_field arity_s};
x = body;
opt = false;
};
]
None)
(Typ.constr ~loc
Expand Down
Loading