Browse Source

apply @:this to temp vars created for abstract instance methods
closes #9295

Aleksandr Kuzmenko 5 years ago
parent
commit
9875100413

+ 4 - 0
src/core/meta.ml

@@ -75,3 +75,7 @@ let get_all () =
 		else []
 		else []
 	in
 	in
 	loop 0
 	loop 0
+
+let copy_from_to m src dst =
+	try (get m src) :: dst
+	with Not_found -> dst

+ 4 - 0
src/typing/typer.ml

@@ -633,6 +633,10 @@ let rec type_binop ctx op e1 e2 is_assign_op with_type p =
 				| TLocal v when not (Meta.has Meta.This v.v_meta) -> v,[],None
 				| TLocal v when not (Meta.has Meta.This v.v_meta) -> v,[],None
 				| _ ->
 				| _ ->
 					let v = gen_local ctx ta ef.epos in
 					let v = gen_local ctx ta ef.epos in
+					(match et.eexpr with
+					| TLocal { v_meta = m } -> v.v_meta <- Meta.copy_from_to Meta.This m v.v_meta
+					| _ -> ()
+					);
 					let decl_v e = mk (TVar (v,Some e)) ctx.t.tvoid p in
 					let decl_v e = mk (TVar (v,Some e)) ctx.t.tvoid p in
 					let rec needs_temp_var e =
 					let rec needs_temp_var e =
 						match e.eexpr with
 						match e.eexpr with

+ 15 - 0
tests/misc/projects/Issue9295/Main.hx

@@ -0,0 +1,15 @@
+abstract A(Int) from Int {
+	public var x(get,set):Int;
+
+	function get_x() return this;
+
+	inline function set_x(value) return this = value;
+
+	public function modify() {
+		x += 3;
+	}
+}
+
+class Main {
+	static function main() {}
+}

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

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

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

@@ -0,0 +1 @@
+Main.hx:9: characters 3-9 : Abstract 'this' value can only be modified inside an inline function. 'set_x' modifies 'this'