2
0
Эх сурвалжийг харах

check for local "this" in abstract closures (fixed issue #1476)

Simon Krajewski 12 жил өмнө
parent
commit
92ee68461c

+ 17 - 0
tests/unit/MyAbstract.hx

@@ -203,4 +203,21 @@ abstract MyReflect({}) from {} {
 		Reflect.setField(this, key, value);
 		return value;
 	}
+}
+
+abstract MyAbstractClosure(String){
+	public function new(value:String) {
+		this = value;
+	}
+	
+	public function test() {
+		var fn = function(){
+			return this;
+		}
+		return fn;
+	}
+	
+	public inline function setVal(v) {
+		this = v;
+	}
 }

+ 9 - 0
tests/unit/TestType.hx

@@ -842,4 +842,13 @@ class TestType extends Test {
 		mr["101"] = function n(x) return 9 + x;
 		eq(mr["101"](1), 10);
 	}
+	
+	function testAbstractClosure() {
+		var s = new unit.MyAbstract.MyAbstractClosure("foo");
+		var func1 = s.test();
+		eq(func1(), "foo");
+		s.setVal("bar");
+		eq(func1(), "foo");
+		eq(s.test()(), "bar");
+	}
 }

+ 2 - 1
typer.ml

@@ -794,7 +794,8 @@ let get_this ctx p =
 	| FunMemberLocal ->
 		let v = (match ctx.vthis with
 			| None ->
-				let v = gen_local ctx ctx.tthis in
+				(* we might be in a closure of an abstract member, so check for local "this" first *)
+				let v = try PMap.find "this" ctx.locals with Not_found -> gen_local ctx ctx.tthis in
 				ctx.vthis <- Some v;
 				v
 			| Some v ->