Browse Source

[matcher] fix logic with inexhaustive null-guards

closes #10291
Simon Krajewski 4 years ago
parent
commit
d264533d7b

+ 9 - 3
src/typing/matcher.ml

@@ -1608,9 +1608,15 @@ module TexprConverter = struct
 								report_not_exhaustive !v_lookup e [(ConConst TNull,dt.dt_pos),dt.dt_pos]
 						| Some e_then ->
 							let e_else = loop dt_rec params dt2 in
-							Option.map (fun e_else ->
-								mk (TIf(e_cond,e_then,Some e_else)) t_switch (punion e_then.epos e_else.epos)
-							) e_else
+							begin match e_else with
+							| None ->
+								if toplevel then
+									Some (mk (TIf(e_cond,e_then,None)) t_switch e_then.epos)
+								else
+									report_not_exhaustive !v_lookup e []
+							| Some e_else ->
+								Some (mk (TIf(e_cond,e_then,Some e_else)) t_switch (punion e_then.epos e_else.epos))
+							end
 						end
 					| Bind(bl,dt) ->
 						let el = List.map (fun (v,p,e) ->

+ 15 - 0
tests/misc/projects/Issue10291/Main.hx

@@ -0,0 +1,15 @@
+import haxe.ds.Option;
+
+class Main {
+	static function main() {
+		var o = Some({b: null});
+		switch (o) {
+			case Some({b: b}) if (b != null):
+				trace("Case 1: b=" + b);
+			case Some({b: null}):
+				trace("Case 2: b=null");
+			case None: // Change None to _ and it works
+				trace("Default");
+		}
+	}
+}

+ 1 - 0
tests/misc/projects/Issue10291/compile-fail.hxml

@@ -0,0 +1 @@
+--main Main

+ 1 - 0
tests/misc/projects/Issue10291/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:9: characters 14-23 : Unmatched patterns: Some(_)