Skip to content

Commit

Permalink
add implicit null checks to pattern matching (see #2580)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Apr 23, 2014
1 parent 0f43ad8 commit 4b08643
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion matcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,11 @@ let convert_switch mctx st cases loop =
| _ -> DTSwitch(e, List.map (fun (c,dt) -> convert_con ctx c, loop dt) cases, !def)
in
match !null with
| None -> dt
| None when is_explicit_null st.st_type ->
let econd = mk (TBinop(OpNotEq,e_st,mk (TConst TNull) (mk_mono()) p)) ctx.t.tbool p in
DTGuard(econd,dt,!def)
| None ->
dt
| Some dt_null ->
let econd = mk (TBinop(OpEq,e_st,mk (TConst TNull) (mk_mono()) p)) ctx.t.tbool p in
DTGuard(econd,dt_null,Some dt)
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/issues/Issue2580.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package unit.issues;

private enum T {
T1(x:Null<T>);
T2(x:Int);
}

class Issue2580 extends Test {
function test() {
function match(t) {
return switch (t) {
case T1(T1(_)): 0;
case T1(T2(_)): 1;
case T1(_): 2;
case T2(_): 3;
}
}
eq(0, match(T1(T1(null))));
eq(1, match(T1(T2(1))));
eq(2, match(T1(null)));
eq(3, match(T2(2)));
}
}

0 comments on commit 4b08643

Please sign in to comment.