Explorar o código

deal with side-effect subjects in matcher null guards

closes #11738
Simon Krajewski hai 9 meses
pai
achega
1dffb2eb70

+ 9 - 3
src/typing/matcher/texprConverter.ml

@@ -349,9 +349,15 @@ let to_texpr ctx t_switch with_type dt =
 					let e_then = loop dt_rec params dt1 in
 					let e_then = loop dt_rec params dt1 in
 					begin match e_then with
 					begin match e_then with
 					| None ->
 					| None ->
-						if toplevel then
-							loop dt_rec params dt2
-						else if ignore_error ctx.com then
+						if toplevel then begin match loop dt_rec params dt2 with
+							| None ->
+								None
+							| Some e_else ->
+								(* In some cases like extractors, the original e expression might be significant for the
+								   output, so let's make sure it appears there (issue #11738). *)
+								let e = mk (TBlock [e;e_else]) e_else.etype e_else.epos in
+								Some e
+						end else if ignore_error ctx.com then
 							Some (mk (TConst TNull) (mk_mono()) dt2.dt_pos)
 							Some (mk (TConst TNull) (mk_mono()) dt2.dt_pos)
 						else
 						else
 							report_not_exhaustive !v_lookup e [(ConConst TNull,dt.dt_pos),dt.dt_pos]
 							report_not_exhaustive !v_lookup e [(ConConst TNull,dt.dt_pos),dt.dt_pos]

+ 21 - 0
tests/unit/src/unit/issues/Issue11738.hx

@@ -0,0 +1,21 @@
+package unit.issues;
+
+import unit.Test;
+
+class Issue11738 extends Test {
+	var value:String;
+
+	function test() {
+		switch ("abc") {
+			case _.charCodeAt(0) => 'a'.code:
+				doSomething("1");
+			case _.charCodeAt(1) => 'e'.code:
+				doSomething("2");
+		}
+		eq("1", value);
+	}
+
+	function doSomething(s:String) {
+		value = s;
+	}
+}