Skip to content

Commit cd876ad

Browse files
committed
fix inlined optional record pattern matching
1 parent c3f4545 commit cd876ad

File tree

6 files changed

+121
-39
lines changed

6 files changed

+121
-39
lines changed

jscomp/ml/typecore.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
11531153
let label_is_optional ld =
11541154
match ld.lbl_repres with
11551155
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
1156+
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
11561157
| _ -> false in
11571158
let process_optional_label (ld, pat) =
11581159
let exp_optional_attr =

jscomp/test/record_regression.js

Lines changed: 104 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -99,63 +99,136 @@ pm0 = ir0.TAG === /* V0 */0 ? [
9999

100100
var pm1;
101101

102-
pm1 = ir1.TAG === /* V0 */0 ? [
102+
if (ir1.TAG === /* V0 */0) {
103+
var x1 = "v1";
104+
var x0 = "v0";
105+
pm1 = x1 !== undefined ? [
106+
x0,
107+
x1,
108+
3
109+
] : [
110+
x0,
111+
"n/a",
112+
3
113+
];
114+
} else {
115+
pm1 = [
103116
"v0",
104-
"v1",
105-
3
106-
] : [
107-
"v0",
108-
undefined,
117+
"n/a",
109118
"v1"
110119
];
120+
}
111121

112122
var pm2;
113123

114-
pm2 = ir2.TAG === /* V0 */0 ? [
115-
"v0",
116-
"v1",
117-
2,
118-
3
119-
] : [
124+
if (ir2.TAG === /* V0 */0) {
125+
var x1$1 = "v1";
126+
var x0$1 = "v0";
127+
if (x1$1 !== undefined) {
128+
var x2 = 2;
129+
pm2 = x2 !== undefined ? [
130+
x0$1,
131+
x1$1,
132+
x2,
133+
3
134+
] : [
135+
x0$1,
136+
x1$1,
137+
0,
138+
3
139+
];
140+
} else {
141+
var x2$1 = 2;
142+
pm2 = x2$1 !== undefined ? [
143+
x0$1,
144+
"n/a",
145+
x2$1,
146+
3
147+
] : [
148+
x0$1,
149+
"n/a",
150+
0,
151+
3
152+
];
153+
}
154+
} else {
155+
pm2 = [
120156
"v0",
121-
undefined,
122-
undefined,
157+
"n/a",
158+
0,
123159
"v1"
124160
];
161+
}
125162

126163
function inlinedRecord(ir) {
127164
if (ir.TAG !== /* V0 */0) {
128165
return [
129166
ir.y0,
130167
"n/a",
131-
undefined,
168+
0,
132169
ir.y1
133170
];
134171
}
135-
var x3 = ir.x3;
136-
var x2 = ir.x2;
137172
var x1 = ir.x1;
138173
var x0 = ir.x0;
139-
if (Caml_obj.equal(x1, "x1")) {
140-
return [
141-
x0,
142-
"x1!",
143-
x2,
144-
x3
145-
];
146-
} else if (x1 !== undefined) {
174+
if (x1 !== undefined) {
175+
switch (x1) {
176+
case "x1" :
177+
var x2 = ir.x2;
178+
if (x2 !== undefined) {
179+
return [
180+
x0,
181+
"x1",
182+
x2,
183+
ir.x3
184+
];
185+
}
186+
break;
187+
case "xx1" :
188+
var x2$1 = ir.x2;
189+
if (x2$1 !== undefined) {
190+
return [
191+
x0,
192+
"xx1",
193+
x2$1,
194+
ir.x3
195+
];
196+
}
197+
break;
198+
default:
199+
200+
}
201+
var x2$2 = ir.x2;
202+
if (x2$2 !== undefined) {
203+
return [
204+
x0,
205+
x1,
206+
x2$2,
207+
ir.x3
208+
];
209+
} else {
210+
return [
211+
x0,
212+
x1,
213+
0,
214+
ir.x3
215+
];
216+
}
217+
}
218+
var x2$3 = ir.x2;
219+
if (x2$3 !== undefined) {
147220
return [
148221
x0,
149-
x1,
150-
x2,
151-
x3
222+
"n/a",
223+
x2$3,
224+
ir.x3
152225
];
153226
} else {
154227
return [
155228
x0,
156-
"not existed",
157-
x2,
158-
x3
229+
"n/a",
230+
0,
231+
ir.x3
159232
];
160233
}
161234
}

jscomp/test/record_regression.res

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,24 @@ let pm0 = switch ir0 {
123123
}
124124
let pm1 = switch ir1 {
125125
| V0({x0, x1, x3}) => (x0, x1, x3)
126-
| V1({y0, y1}) => (y0, None, y1)
126+
| V0({x0, x1: ?None, x3}) => (x0, "n/a", x3)
127+
| V1({y0, y1}) => (y0, "n/a", y1)
127128
}
128129
let pm2 = switch ir2 {
129130
| V0({x0, x1, x2, x3}) => (x0, x1, x2, x3)
130-
| V1({y0, y1}) => (y0, None, None, y1)
131+
| V0({x0, x1: ?None, x2, x3}) => (x0, "n/a", x2, x3)
132+
| V0({x0, x1, x2: ?None, x3}) => (x0, x1, 0, x3)
133+
| V0({x0, x1: ?None, x2: ?None, x3}) => (x0, "n/a", 0, x3)
134+
| V1({y0, y1}) => (y0, "n/a", 0, y1)
131135
}
132136
let inlinedRecord = ir => switch ir {
133-
| V0({x0, x1, x2, x3}) if x1 == Some("x1") => (x0, "x1!", x2, x3)
134-
| V0({x0, x1, x2, x3}) => switch x1 {
135-
| Some(x1) => (x0, x1, x2, x3)
136-
| None => (x0, "not existed", x2, x3)
137-
}
138-
| V1({y0, y1}) => (y0, "n/a", None, y1)
137+
| V0({x0, x1: ?Some("x1"), x2, x3}) => (x0, "x1", x2, x3)
138+
| V0({x0, x1: "xx1", x2, x3}) => (x0, "xx1", x2, x3)
139+
| V0({x0, x1, x2, x3}) => (x0, x1, x2, x3)
140+
| V0({x0, x1: ?None, x2, x3}) => (x0, "n/a", x2, x3)
141+
| V0({x0, x1, x2: ?None, x3}) => (x0, x1, 0, x3)
142+
| V0({x0, x1: ?None, x2: ?None, x3}) => (x0, "n/a", 0, x3)
143+
| V1({y0, y1}) => (y0, "n/a", 0, y1)
139144
}
140145
let pm3 = inlinedRecord(ir2)
141146
let pm4 = inlinedRecord(ir3)

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41882,6 +41882,7 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
4188241882
let label_is_optional ld =
4188341883
match ld.lbl_repres with
4188441884
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
41885+
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
4188541886
| _ -> false in
4188641887
let process_optional_label (ld, pat) =
4188741888
let exp_optional_attr =

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41882,6 +41882,7 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
4188241882
let label_is_optional ld =
4188341883
match ld.lbl_repres with
4188441884
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
41885+
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
4188541886
| _ -> false in
4188641887
let process_optional_label (ld, pat) =
4188741888
let exp_optional_attr =

lib/4.06.1/whole_compiler.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96880,6 +96880,7 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
9688096880
let label_is_optional ld =
9688196881
match ld.lbl_repres with
9688296882
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
96883+
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
9688396884
| _ -> false in
9688496885
let process_optional_label (ld, pat) =
9688596886
let exp_optional_attr =

0 commit comments

Comments
 (0)