소스 검색

[optimizer] don't try to const-optimize switches on `this`

closes #8781
Simon Krajewski 6 년 전
부모
커밋
5792f6a497
2개의 변경된 파일40개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 1
      src/optimization/optimizer.ml
  2. 39 0
      tests/unit/src/unit/issues/Issue8781.hx

+ 1 - 1
src/optimization/optimizer.ml

@@ -237,7 +237,7 @@ let reduce_control_flow ctx e = match e.eexpr with
 		| DoWhile -> e) (* we cant remove while since sub can contain continue/break *)
 	| TSwitch (e1,cases,def) ->
 		let e = match Texpr.skip e1 with
-			| {eexpr = TConst ct} as e1 ->
+			| {eexpr = TConst ct} as e1 when (match ct with TSuper | TThis -> false | _ -> true) ->
 				let rec loop cases = match cases with
 					| (el,e) :: cases ->
 						if List.exists (Texpr.equal e1) el then e

+ 39 - 0
tests/unit/src/unit/issues/Issue8781.hx

@@ -0,0 +1,39 @@
+package unit.issues;
+
+private class ComplexEnum
+{
+
+	public static final FIRST:ComplexEnum = new ComplexEnum("first", 0);
+	public static final SECOND:ComplexEnum = new ComplexEnum( "second", 1);
+	public static final THIRD:ComplexEnum = new ComplexEnum( "third", 2);
+
+	var name(null, default):String;
+	var value(null, default):Int;
+
+	public function new (name:String, value:Int )
+	{
+		this.name = name;
+		this.value = value;
+	}
+
+	public function getPrefix():String
+	{
+		switch (this)
+		{
+			case FIRST:
+				return "first_";
+			case SECOND:
+				return "second_";
+			case THIRD:
+				return "third_";
+			default: throw "Unknown";
+		}
+	}
+
+}
+
+class Issue8781 extends unit.Test {
+	function test() {
+		eq("value: second_", "value: "+ComplexEnum.SECOND.getPrefix());
+	}
+}