Skip to content

Commit bfa24b1

Browse files
committed
Fix compilation of unknown.
1 parent 8b314d8 commit bfa24b1

7 files changed

+60
-21
lines changed

jscomp/core/js_exp_make.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,9 @@ let rec is_a_literal_case ~(literal_cases : Lambda.literal list) ~block_cases (e
783783
(* We don't know the type of unknown, so we need to express:
784784
this is not one of the literals *)
785785
(match literal_cases with
786-
| [] -> { expression_desc = Bool true; comment=None}
786+
| [] ->
787+
(* this should not happen *)
788+
assert false
787789
| l1 :: others ->
788790
let is_literal_1 = is_literal_case l1 in
789791
Ext_list.fold_right others is_literal_1 (fun literal_n acc ->
@@ -797,7 +799,7 @@ let rec is_a_literal_case ~(literal_cases : Lambda.literal list) ~block_cases (e
797799
bin And (is_block_case c1) (is_a_literal_case ~literal_cases ~block_cases:rest e)
798800
| [] -> assert false
799801

800-
let is_tag ?(has_null_undefined_other=(false, false, false)) (e : t) : t =
802+
let is_int_tag ?(has_null_undefined_other=(false, false, false)) (e : t) : t =
801803
let (has_null, has_undefined, has_other) = has_null_undefined_other in
802804
if has_null && (has_undefined = false) && (has_other = false) then (* null *)
803805
{ expression_desc = Bin (EqEqEq, e, nil); comment=None }

jscomp/core/js_exp_make.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ val neq_null_undefined_boolean : ?comment:string -> t -> t -> t
202202

203203
val is_type_number : ?comment:string -> t -> t
204204

205-
val is_tag : ?has_null_undefined_other:(bool * bool * bool) -> t -> t
205+
val is_int_tag : ?has_null_undefined_other:(bool * bool * bool) -> t -> t
206206

207207
val is_a_literal_case : literal_cases:Lambda.literal list -> block_cases:Lambda.block_type list -> t -> t
208208

jscomp/core/lam_compile.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ let get_literal_cases (sw_names : Lambda.switch_names option) =
172172
| Some { consts } ->
173173
Ext_array.iter consts (function
174174
| {literal = Some literal} -> res := literal :: !res
175-
| {literal = None} -> ()
175+
| {name; literal = None} -> res := String name :: !res
176176
)
177177
);
178178
!res
@@ -691,14 +691,14 @@ and compile_switch (switch_arg : Lam.t) (sw : Lam.lambda_switch)
691691
else
692692
(* [e] will be used twice *)
693693
let dispatch e =
694-
let is_tag =
694+
let is_a_literal_case =
695695
if block_cases <> []
696-
then E.is_a_literal_case ~literal_cases:(get_literal_cases sw_names) ~block_cases e
696+
then
697+
E.is_a_literal_case ~literal_cases:(get_literal_cases sw_names) ~block_cases e
697698
else
698-
E.is_tag ~has_null_undefined_other:(has_null_undefined_other sw_names) e in
699-
S.if_ is_tag
699+
E.is_int_tag ~has_null_undefined_other:(has_null_undefined_other sw_names) e in
700+
S.if_ is_a_literal_case
700701
(compile_cases cxt e sw_consts sw_num_default get_const_name)
701-
(* default still needed, could simplified*)
702702
~else_:
703703
(compile_cases ~untagged cxt (if untagged then e else E.tag ~name:tag_name e) sw_blocks sw_blocks_default
704704
get_block_name)

jscomp/ml/matching.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,11 +1329,10 @@ let make_constr_matching p def ctx = function
13291329
[] -> fatal_error "Matching.make_constr_matching"
13301330
| ((arg, _mut) :: argl) ->
13311331
let cstr = pat_as_constr p in
1332+
let untagged =
1333+
Ext_list.exists cstr.cstr_attributes (function ({txt}, _) -> txt = "unboxed") in
13321334
let newargs =
1333-
if cstr.cstr_inlined <> None ||
1334-
Ext_list.exists cstr.cstr_attributes (function
1335-
| ({txt="unboxed"}, _) -> true
1336-
| _ -> false) then
1335+
if cstr.cstr_inlined <> None || (untagged && cstr.cstr_args <> []) then
13371336
(arg, Alias) :: argl
13381337
else match cstr.cstr_tag with
13391338
| Cstr_block _ when

jscomp/test/UntaggedVariants.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,15 @@ var TwoObjects = {
9292
};
9393

9494
function classify$2(x) {
95-
if (x === "A") {
96-
return "a";
97-
} else {
98-
return "b";
95+
if (x === "A" || x === "B") {
96+
if (x === "A") {
97+
return "a";
98+
} else {
99+
return "b";
100+
}
99101
}
102+
console.log(x);
103+
return "Unknown";
100104
}
101105

102106
var Unknown = {
@@ -211,6 +215,26 @@ var Json = {
211215
classify: classify$6
212216
};
213217

218+
function check(s, y) {
219+
if (s === "B") {
220+
return 42;
221+
}
222+
var x = s[0];
223+
if (x === "B") {
224+
return 42;
225+
}
226+
var tmp = s[1];
227+
if (tmp === "B" && x !== y) {
228+
return 41;
229+
} else {
230+
return 42;
231+
}
232+
}
233+
234+
var TrickyNested = {
235+
check: check
236+
};
237+
214238
var i = 42;
215239

216240
var i2 = 42.5;
@@ -244,4 +268,5 @@ exports.MultipleBlocks = MultipleBlocks;
244268
exports.OnlyBlocks = OnlyBlocks;
245269
exports.WithArray = WithArray;
246270
exports.Json = Json;
271+
exports.TrickyNested = TrickyNested;
247272
/* l2 Not a pure module */

jscomp/test/UntaggedVariants.res

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,16 @@ let classify (x : t) : tagged_t =
183183
JSONObject (Obj.magic x)
184184
*/
185185
}
186+
187+
module TrickyNested = {
188+
@unboxed
189+
type rec t =
190+
| A((t, t))
191+
| B
192+
193+
let check = (s, y) =>
194+
switch s {
195+
| A((A(x), B)) if x !== y => 41
196+
| _ => 42
197+
}
198+
}

jscomp/test/variantsMatching.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ function isWhyNot(x) {
278278
}
279279

280280
function plus$3(x, y) {
281-
if (x === null || x === undefined) {
281+
if (x === undefined || x === null || x === "WhyNotAnotherOne") {
282282
switch (x) {
283283
case null :
284284
case undefined :
@@ -287,13 +287,13 @@ function plus$3(x, y) {
287287
break;
288288

289289
}
290-
} else if (!(y === null || y === undefined)) {
290+
} else if (!(y === undefined || y === null || y === "WhyNotAnotherOne")) {
291291
return {
292292
x: x.x + y.x,
293293
y: x.y + y.y
294294
};
295295
}
296-
if (!(y === null || y === undefined)) {
296+
if (!(y === undefined || y === null || y === "WhyNotAnotherOne")) {
297297
return "WhyNotAnotherOne";
298298
}
299299
switch (y) {
@@ -307,7 +307,7 @@ function plus$3(x, y) {
307307
}
308308

309309
function kind$1(x) {
310-
if (!(x === null || x === undefined)) {
310+
if (!(x === undefined || x === null || x === "WhyNotAnotherOne")) {
311311
return "present";
312312
}
313313
switch (x) {

0 commit comments

Comments
 (0)