Browse Source

[matcher] don't allow null-only switches if we need a value

closes #11366
Simon Krajewski 1 year ago
parent
commit
e6442b23b9

+ 2 - 2
src/optimization/analyzerTexprTransformer.ml

@@ -102,7 +102,7 @@ let rec func ctx bb tf t p =
 			block_element bb e,e1
 			block_element bb e,e1
 		| TBlock [e1] ->
 		| TBlock [e1] ->
 			value bb e1
 			value bb e1
-		| TBlock _ | TIf _ | TSwitch _ | TTry _ ->
+		| TBlock _ | TIf(_,_,Some _) | TSwitch _ | TTry _ ->
 			bind_to_temp bb e
 			bind_to_temp bb e
 		| TCall({eexpr = TIdent s},el) when is_really_unbound s ->
 		| TCall({eexpr = TIdent s},el) when is_really_unbound s ->
 			check_unbound_call s el;
 			check_unbound_call s el;
@@ -178,7 +178,7 @@ let rec func ctx bb tf t p =
 		| TThrow _ | TReturn _ | TBreak | TContinue ->
 		| TThrow _ | TReturn _ | TBreak | TContinue ->
 			let bb = block_element bb e in
 			let bb = block_element bb e in
 			bb,mk (TConst TNull) t_dynamic e.epos
 			bb,mk (TConst TNull) t_dynamic e.epos
-		| TVar _ | TFor _ | TWhile _ ->
+		| TVar _ | TFor _ | TWhile _ | TIf _ ->
 			Error.raise_typing_error "Cannot use this expression as value" e.epos
 			Error.raise_typing_error "Cannot use this expression as value" e.epos
 	and value bb e =
 	and value bb e =
 		let bb,e = value' bb e in
 		let bb,e = value' bb e in

+ 1 - 1
src/typing/matcher/texprConverter.ml

@@ -359,7 +359,7 @@ let to_texpr ctx t_switch with_type dt =
 						let e_else = loop dt_rec params dt2 in
 						let e_else = loop dt_rec params dt2 in
 						begin match e_else with
 						begin match e_else with
 						| None ->
 						| None ->
-							if toplevel then
+							if toplevel && with_type = NoValue then
 								Some (mk (TIf(e_cond,e_then,None)) t_switch e_then.epos)
 								Some (mk (TIf(e_cond,e_then,None)) t_switch e_then.epos)
 							else
 							else
 								report_not_exhaustive !v_lookup e []
 								report_not_exhaustive !v_lookup e []

+ 6 - 0
tests/misc/projects/Issue11366/Main.hx

@@ -0,0 +1,6 @@
+function main() {
+	var x = switch [][0] {
+		case null:
+			'x';
+	}
+}

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

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

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

@@ -0,0 +1 @@
+Main.hx:2: characters 17-22 : Unmatched patterns: _

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

@@ -1 +1 @@
-Main.hx:7: characters 13-25 : Cannot use Void as value
+Main.hx:7: characters 13-25 : Cannot use this expression as value