Skip to content

Commit db17fe1

Browse files
authored
Pass and extend current_type_path in missing place (#7781)
* pass and extend current_type_path in missing place, fixing issue with nested record fields + attributes * changelog
1 parent 09903f9 commit db17fe1

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#### :bug: Bug fix
2525

2626
- Preserve `@as(...)` decorator on record fields when creating interface. https://github.com/rescript-lang/rescript/pull/7779
27+
- Fix parse error with nested record types and attributes on the field name that has the nested record type. https://github.com/rescript-lang/rescript/pull/7781
2728

2829
#### :memo: Documentation
2930

compiler/syntax/src/res_core.ml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ type inline_types_context = {
1616
params: (Parsetree.core_type * Asttypes.variance) list;
1717
}
1818

19+
let extend_current_type_name_path current_type_name_path field_name =
20+
match current_type_name_path with
21+
| None -> None
22+
| Some path -> Some (path @ [field_name])
23+
1924
module Recover = struct
2025
let default_expr () =
2126
let id = Location.mknoloc "rescript.exprhole" in
@@ -4667,7 +4672,7 @@ and parse_string_field_declaration p =
46674672
(* field-decl ::=
46684673
* | [mutable] field-name : poly-typexpr
46694674
* | attributes field-decl *)
4670-
and parse_field_declaration p =
4675+
and parse_field_declaration ?current_type_name_path ?inline_types_context p =
46714676
let start_pos = p.Parser.start_pos in
46724677
let attrs = parse_attributes p in
46734678
let mut =
@@ -4684,7 +4689,10 @@ and parse_field_declaration p =
46844689
match p.Parser.token with
46854690
| Colon ->
46864691
Parser.next p;
4687-
parse_poly_type_expr p
4692+
let current_type_name_path =
4693+
extend_current_type_name_path current_type_name_path name.txt
4694+
in
4695+
parse_poly_type_expr ?current_type_name_path ?inline_types_context p
46884696
| _ ->
46894697
Ast_helper.Typ.constr ~loc:name.loc {name with txt = Lident name.txt} []
46904698
in
@@ -4718,9 +4726,7 @@ and parse_field_declaration_region ?current_type_name_path ?inline_types_context
47184726
let lident, loc = parse_lident p in
47194727
let name = Location.mkloc lident loc in
47204728
let current_type_name_path =
4721-
match current_type_name_path with
4722-
| None -> None
4723-
| Some current_type_name_path -> Some (current_type_name_path @ [name.txt])
4729+
extend_current_type_name_path current_type_name_path name.txt
47244730
in
47254731
let optional = parse_optional_label p in
47264732
let typ =
@@ -5379,7 +5385,10 @@ and parse_record_or_object_decl ?current_type_name_path ?inline_types_context p
53795385
p
53805386
| attr :: _ as attrs ->
53815387
let first =
5382-
let field = parse_field_declaration p in
5388+
let field =
5389+
parse_field_declaration ?current_type_name_path
5390+
?inline_types_context p
5391+
in
53835392
Parser.optional p Comma |> ignore;
53845393
{
53855394
field with

tests/tests/src/nested_records.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ let options = {
1616
}
1717
};
1818

19+
let location = {
20+
location_area: {
21+
name: "test",
22+
url: "test"
23+
}
24+
};
25+
1926
export {
2027
options,
28+
location,
2129
}
2230
/* No side effect */

tests/tests/src/nested_records.res

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,18 @@ let options = {
1515
otherExtra: Some({test: true, anotherInlined: {record: true}}),
1616
},
1717
}
18+
19+
type location = {
20+
@as("location_area")
21+
locationArea: {
22+
name: string,
23+
url: string,
24+
},
25+
}
26+
27+
let location = {
28+
locationArea: {
29+
name: "test",
30+
url: "test",
31+
},
32+
}

0 commit comments

Comments
 (0)