Skip to content

Commit d69d438

Browse files
authored
Fix renamed field access in inline records (#6551)
* add remaining part of melange-re/melange#732 * call the right fld_record_ functions * update changelog
1 parent b2ae38a commit d69d438

10 files changed

+150
-115
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#### :bug: Bug Fix
1616

17+
- Renamed inline record fields : fix renamed field access in inline records https://github.com/rescript-lang/rescript-compiler/pull/6551
1718
- Fixed issue with coercions sometimes raising a `Not_found` instead of giving a proper error message. https://github.com/rescript-lang/rescript-compiler/pull/6574
1819
- Fix issue with recursive modules and uncurried. https://github.com/rescript-lang/rescript-compiler/pull/6575
1920

jscomp/core/bs_conditional_initial.ml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ let setup_env () =
4444
Builtin_attributes.check_bs_attributes_inclusion := Record_attributes_check.check_bs_attributes_inclusion;
4545
Builtin_attributes.check_duplicated_labels :=
4646
Record_attributes_check.check_duplicated_labels;
47-
Lambda.fld_record := Record_attributes_check.fld_record;
48-
Lambda.fld_record_set := Record_attributes_check.fld_record_set;
49-
Lambda.blk_record := Record_attributes_check.blk_record;
50-
Lambda.blk_record_inlined := Record_attributes_check.blk_record_inlined;
5147
Matching.names_from_construct_pattern :=
5248
Matching_polyfill.names_from_construct_pattern;
5349

jscomp/core/record_attributes_check.ml

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
type label = Types.label_description
2626

27-
let find_name = Matching.find_name
27+
let find_name = Lambda.find_name
2828

2929
let find_name_with_loc (attr : Parsetree.attribute) : string Asttypes.loc option
3030
=
@@ -40,34 +40,6 @@ let find_name_with_loc (attr : Parsetree.attribute) : string Asttypes.loc option
4040
Some { txt = s; loc }
4141
| _ -> None
4242

43-
let fld_record (lbl : label) =
44-
Lambda.Fld_record
45-
{
46-
name = Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name;
47-
mutable_flag = lbl.lbl_mut;
48-
}
49-
50-
let fld_record_set (lbl : label) =
51-
Lambda.Fld_record_set
52-
(Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
53-
54-
let blk_record (fields : (label * _) array) mut record_repr =
55-
let all_labels_info =
56-
Ext_array.map fields (fun (lbl, _) ->
57-
Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
58-
in
59-
Lambda.Blk_record
60-
{ fields = all_labels_info; mutable_flag = mut; record_repr }
61-
62-
let blk_record_inlined fields name num_nonconst optional_labels ~tag ~attrs mutable_flag =
63-
let fields =
64-
Array.map
65-
(fun ((lbl : label), _) ->
66-
Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
67-
fields
68-
in
69-
Lambda.Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag; optional_labels; attrs }
70-
7143
let check_bs_attributes_inclusion (attrs1 : Parsetree.attributes)
7244
(attrs2 : Parsetree.attributes) lbl_name =
7345
let a = Ext_list.find_def attrs1 find_name lbl_name in

jscomp/ml/lambda.ml

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,47 @@ let mutable_flag_of_tag_info (tag : tag_info) =
8686
| Blk_some
8787
-> Immutable
8888

89+
type label = Types.label_description
90+
91+
let find_name (attr : Parsetree.attribute) =
92+
match attr with
93+
| ( { txt = "bs.as" | "as" },
94+
PStr
95+
[
96+
{
97+
pstr_desc =
98+
Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (s, _)) }, _);
99+
};
100+
] ) ->
101+
Some s
102+
| _ -> None
103+
104+
let blk_record (fields : (label * _) array) mut record_repr =
105+
let all_labels_info =
106+
Ext_array.map fields (fun (lbl, _) ->
107+
Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
108+
in
109+
Blk_record
110+
{ fields = all_labels_info; mutable_flag = mut; record_repr }
89111

90-
let blk_record = ref (fun _ _ _ ->
91-
assert false
92-
)
93-
94-
95-
let blk_record_ext = ref (fun fields mutable_flag ->
96-
let all_labels_info = fields |> Array.map (fun (x,_) -> x.Types.lbl_name) in
97-
Blk_record_ext {fields = all_labels_info; mutable_flag }
98-
)
99112

100-
let blk_record_inlined = ref (fun fields name num_nonconst optional_labels ~tag ~attrs mutable_flag ->
101-
let fields = fields |> Array.map (fun (x,_) -> x.Types.lbl_name) in
113+
let blk_record_ext fields mutable_flag =
114+
let all_labels_info =
115+
Array.map
116+
(fun ((lbl : label), _) ->
117+
Ext_list.find_def lbl.Types.lbl_attributes find_name lbl.lbl_name)
118+
fields
119+
in
120+
Blk_record_ext {fields = all_labels_info; mutable_flag }
121+
122+
let blk_record_inlined fields name num_nonconst optional_labels ~tag ~attrs mutable_flag =
123+
let fields =
124+
Array.map
125+
(fun ((lbl : label), _) ->
126+
Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
127+
fields
128+
in
102129
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag; optional_labels; attrs }
103-
)
104130

105131
let ref_tag_info : tag_info =
106132
Blk_record {fields = [| "contents" |]; mutable_flag = Mutable; record_repr = Record_regular}
@@ -117,9 +143,17 @@ type field_dbg_info =
117143
| Fld_variant
118144
| Fld_cons
119145
| Fld_array
120-
121-
let fld_record = ref (fun (lbl : Types.label_description) ->
122-
Fld_record {name = lbl.lbl_name; mutable_flag = Mutable})
146+
147+
let fld_record (lbl : label) =
148+
Fld_record
149+
{
150+
name = Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name;
151+
mutable_flag = lbl.lbl_mut;
152+
}
153+
154+
let fld_record_extension (lbl : label) =
155+
Fld_record_extension
156+
{ name = Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name }
123157

124158
let ref_field_info : field_dbg_info =
125159
Fld_record { name = "contents"; mutable_flag = Mutable}
@@ -131,8 +165,21 @@ type set_field_dbg_info =
131165
| Fld_record_extension_set of string
132166

133167
let ref_field_set_info : set_field_dbg_info = Fld_record_set "contents"
134-
let fld_record_set = ref ( fun (lbl : Types.label_description) ->
135-
Fld_record_set lbl.lbl_name )
168+
let fld_record_set (lbl : label) =
169+
Fld_record_set
170+
(Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
171+
172+
let fld_record_inline (lbl : label) =
173+
Fld_record_inline
174+
{ name = Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name }
175+
176+
let fld_record_inline_set (lbl : label) =
177+
Fld_record_inline_set
178+
(Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
179+
180+
let fld_record_extension_set (lbl : label) =
181+
Fld_record_extension_set
182+
(Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name)
136183

137184
type immediate_or_pointer =
138185
| Immediate

jscomp/ml/lambda.mli

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,34 @@ type tag_info =
6363
| Blk_record_ext of {fields : string array; mutable_flag : mutable_flag}
6464
| Blk_lazy_general
6565

66+
val find_name :
67+
Parsetree.attribute -> Asttypes.label option
68+
6669
val tag_of_tag_info : tag_info -> int
6770
val mutable_flag_of_tag_info : tag_info -> mutable_flag
68-
val blk_record :
69-
(
70-
(Types.label_description* Typedtree.record_label_definition) array ->
71-
mutable_flag ->
72-
record_repr ->
73-
tag_info
74-
) ref
71+
val blk_record :
72+
(Types.label_description* Typedtree.record_label_definition) array ->
73+
mutable_flag ->
74+
record_repr ->
75+
tag_info
76+
7577

7678
val blk_record_ext :
77-
(
78-
(Types.label_description* Typedtree.record_label_definition) array ->
79-
mutable_flag ->
80-
tag_info
81-
) ref
79+
(Types.label_description* Typedtree.record_label_definition) array ->
80+
mutable_flag ->
81+
tag_info
82+
8283

8384
val blk_record_inlined :
84-
(
85-
(Types.label_description* Typedtree.record_label_definition) array ->
86-
string ->
87-
int ->
88-
string list ->
89-
tag:int ->
90-
attrs:Parsetree.attributes ->
91-
mutable_flag ->
92-
tag_info
93-
) ref
85+
(Types.label_description* Typedtree.record_label_definition) array ->
86+
string ->
87+
int ->
88+
string list ->
89+
tag:int ->
90+
attrs:Parsetree.attributes ->
91+
mutable_flag ->
92+
tag_info
93+
9494

9595

9696

@@ -110,8 +110,16 @@ type field_dbg_info =
110110
| Fld_array
111111

112112
val fld_record :
113-
(Types.label_description ->
114-
field_dbg_info) ref
113+
Types.label_description ->
114+
field_dbg_info
115+
116+
val fld_record_inline :
117+
Types.label_description ->
118+
field_dbg_info
119+
120+
val fld_record_extension :
121+
Types.label_description ->
122+
field_dbg_info
115123

116124
val ref_field_info : field_dbg_info
117125

@@ -125,8 +133,16 @@ type set_field_dbg_info =
125133
val ref_field_set_info : set_field_dbg_info
126134

127135
val fld_record_set :
128-
(Types.label_description ->
129-
set_field_dbg_info) ref
136+
Types.label_description ->
137+
set_field_dbg_info
138+
139+
val fld_record_inline_set :
140+
Types.label_description ->
141+
set_field_dbg_info
142+
143+
val fld_record_extension_set :
144+
Types.label_description ->
145+
set_field_dbg_info
130146

131147
type immediate_or_pointer =
132148
| Immediate

jscomp/ml/matching.ml

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ open Printf
2626

2727
let dbg = false
2828

29-
let find_name (attr : Parsetree.attribute) =
30-
match attr with
31-
| ( { txt = "bs.as" | "as" },
32-
PStr
33-
[
34-
{
35-
pstr_desc =
36-
Pstr_eval ({ pexp_desc = Pexp_constant (Pconst_string (s, _)) }, _);
37-
};
38-
] ) ->
39-
Some s
40-
| _ -> None
41-
4229
(* See Peyton-Jones, ``The Implementation of functional programming
4330
languages'', chapter 5. *)
4431
(*
@@ -1612,12 +1599,11 @@ let make_record_matching loc all_labels def = function
16121599
match lbl.lbl_repres with
16131600
| Record_float_unused -> assert false
16141601
| Record_regular | Record_optional_labels _ ->
1615-
Lprim (Pfield (lbl.lbl_pos, !Lambda.fld_record lbl), [arg], loc)
1602+
Lprim (Pfield (lbl.lbl_pos, Lambda.fld_record lbl), [arg], loc)
16161603
| Record_inlined _ ->
1617-
let name = Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name in
1618-
Lprim (Pfield (lbl.lbl_pos, Fld_record_inline {name}), [arg], loc)
1604+
Lprim (Pfield (lbl.lbl_pos, Lambda.fld_record_inline lbl), [arg], loc)
16191605
| Record_unboxed _ -> arg
1620-
| Record_extension -> Lprim (Pfield (lbl.lbl_pos + 1, Fld_record_extension {name = lbl.lbl_name}), [arg], loc)
1606+
| Record_extension -> Lprim (Pfield (lbl.lbl_pos + 1, Lambda.fld_record_extension lbl), [arg], loc)
16211607
in
16221608
let str =
16231609
match lbl.lbl_mut with

jscomp/ml/matching.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
open Typedtree
1919
open Lambda
2020

21-
val find_name :
22-
Parsetree.attribute -> Asttypes.label option
23-
2421
val call_switcher_variant_constant :
2522
(Location.t ->
2623
Lambda.lambda option ->

0 commit comments

Comments
 (0)