浏览代码

[matcher] don't optimize single-case switch away if subject is nullable

closes #6198
Simon Krajewski 6 年之前
父节点
当前提交
2bd850037c
共有 2 个文件被更改,包括 26 次插入1 次删除
  1. 2 1
      src/typing/matcher.ml
  2. 24 0
      tests/optimization/src/issues/Issue6198.hx

+ 2 - 1
src/typing/matcher.ml

@@ -1454,13 +1454,14 @@ module TexprConverter = struct
 								| Some e -> Some (List.map (Constructor.to_texpr ctx match_debug) (List.sort Constructor.compare cons),e)
 								| Some e -> Some (List.map (Constructor.to_texpr ctx match_debug) (List.sort Constructor.compare cons),e)
 							end
 							end
 						) cases in
 						) cases in
+						let is_nullable_subject = is_explicit_null e_subject.etype in
 						let e_subject = match kind with
 						let e_subject = match kind with
 							| SKValue -> e_subject
 							| SKValue -> e_subject
 							| SKEnum -> if match_debug then mk_name_call e_subject else mk_index_call e_subject
 							| SKEnum -> if match_debug then mk_name_call e_subject else mk_index_call e_subject
 							| SKLength -> type_field_access ctx e_subject "length"
 							| SKLength -> type_field_access ctx e_subject "length"
 						in
 						in
 						begin match cases,e_default,with_type with
 						begin match cases,e_default,with_type with
-							| [_,e2],None,_ when (match finiteness with RunTimeFinite -> true | _ -> false) ->
+							| [_,e2],None,_ when (match finiteness with RunTimeFinite -> true | _ -> false) && not is_nullable_subject ->
 								{e2 with etype = t_switch}
 								{e2 with etype = t_switch}
 							| [[e1],e2],Some _,_
 							| [[e1],e2],Some _,_
 							| [[e1],e2],None,NoValue when ctx.com.platform <> Java (* TODO: problem with TestJava.hx:285 *) ->
 							| [[e1],e2],None,NoValue when ctx.com.platform <> Java (* TODO: problem with TestJava.hx:285 *) ->

+ 24 - 0
tests/optimization/src/issues/Issue6198.hx

@@ -0,0 +1,24 @@
+package issues;
+
+private enum E {
+    A;
+}
+class Issue6198 {
+	@:js('
+		if(issues_Issue6198.getNull()._hx_index == 0) {
+			issues_Issue6198.use("e = E.A");
+		}
+	')
+	static function test1() {
+        var e:Null<E> = getNull();
+        switch(e)
+        {
+            case E.A:
+                use("e = E.A");
+        }
+	}
+
+	static function getNull() { return null; }
+
+	@:pure(false) static function use(a) { }
+}