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

put abstract binop assign code in right place (closes #3345)

Simon Krajewski 11 жил өмнө
parent
commit
115b08249b

+ 38 - 0
tests/unit/issues/Issue3345.hx

@@ -0,0 +1,38 @@
+package unit.issues;
+
+private abstract Meters(Float) from Float {
+	inline public static var One:Meters = 1;
+	inline public static var Zero:Meters = 0;
+
+	@:extern inline public function new(f:Float) {
+		this = f;
+	}
+
+	@:extern @:op(-A) public static function neg(s:Meters):Meters;
+	@:extern @:op(A+B) public static function add(lhs:Meters, rhs:Meters):Meters;
+	@:extern @:op(A-B) public static function sub(lhs:Meters, rhs:Meters):Meters;
+	@:extern @:op(A>B) public static function gt(lhs:Meters, rhs:Meters):Bool;
+	@:extern @:op(A>=B) public static function gte(lhs:Meters, rhs:Meters):Bool;
+	@:extern @:op(A<B) public static function lt(lhs:Meters, rhs:Meters):Bool;
+	@:extern @:op(A<=B) public static function lte(lhs:Meters, rhs:Meters):Bool;
+	@:extern @:op(A==B) public static function eq(lhs:Meters, rhs:Meters):Bool;
+	@:extern inline public function float() return this;
+
+	@:to inline public function toString() {
+		return '$this(m)';
+	}
+}
+
+class Issue3345 extends Test {
+	function test() {
+		var acc:Meters = .0;
+		for (i in 0...10)
+			acc += 10;
+		eq("100(m)", acc);
+
+		var acc:Meters = .0;
+		for (i in 0...10)
+			acc -= 10;
+		eq("-100(m)", acc);
+	}
+}

+ 5 - 3
typer.ml

@@ -2176,7 +2176,7 @@ let rec type_binop ctx op e1 e2 is_assign_op with_type p =
 								Codegen.AbstractCast.cast_or_unify_raise ctx t1 e1 p,e2
 								Codegen.AbstractCast.cast_or_unify_raise ctx t1 e1 p,e2
 							end in
 							end in
 							check_constraints ctx "" cf.cf_params monos (apply_params a.a_params tl) false cf.cf_pos;
 							check_constraints ctx "" cf.cf_params monos (apply_params a.a_params tl) false cf.cf_pos;
-							if not swapped then
+							let e = if not swapped then
 								make e1 e2
 								make e1 e2
 							else
 							else
 								let v1,v2 = gen_local ctx t1, gen_local ctx t2 in
 								let v1,v2 = gen_local ctx t1, gen_local ctx t2 in
@@ -2188,8 +2188,10 @@ let rec type_binop ctx op e1 e2 is_assign_op with_type p =
 									ev1;
 									ev1;
 									e
 									e
 								]) e.etype e.epos in
 								]) e.etype e.epos in
-								if is_assign_op && op_cf = op then (mk (TMeta((Meta.RequiresAssign,[],p),e)) e.etype e.epos)
-								else e
+								e
+							in
+							if is_assign_op && op_cf = op || true then (mk (TMeta((Meta.RequiresAssign,[],p),e)) e.etype e.epos)
+							else e
 						in
 						in
 						begin try
 						begin try
 							check e1 e2 false
 							check e1 e2 false