Browse Source

make sure the raising version of `cast_or_unify` is used when trying to find array access (closes #3432)

Simon Krajewski 11 years ago
parent
commit
0979a9a16c
2 changed files with 43 additions and 5 deletions
  1. 5 5
      codegen.ml
  2. 38 0
      tests/unit/issues/Issue3432.hx

+ 5 - 5
codegen.ml

@@ -725,20 +725,20 @@ module AbstractCast = struct
 				| TFun([(_,_,tab);(_,_,ta1);(_,_,ta2)],r) as tf when is_set ->
 					begin try
 						Type.unify tab ta;
-						let e1 = cast_or_unify ctx ta1 e1 p in
-						let e2o = match e2o with None -> None | Some e2 -> Some (cast_or_unify ctx ta2 e2 p) in
+						let e1 = cast_or_unify_raise ctx ta1 e1 p in
+						let e2o = match e2o with None -> None | Some e2 -> Some (cast_or_unify_raise ctx ta2 e2 p) in
 						check_constraints();
 						cf,tf,r,e1,e2o
-					with Unify_error _ ->
+					with Unify_error _ | Error (Unify _,_) ->
 						loop cfl
 					end
 				| TFun([(_,_,tab);(_,_,ta1)],r) as tf when not is_set ->
 					begin try
 						Type.unify tab ta;
-						let e1 = cast_or_unify ctx ta1 e1 p in
+						let e1 = cast_or_unify_raise ctx ta1 e1 p in
 						check_constraints();
 						cf,tf,r,e1,None
-					with Unify_error _ ->
+					with Unify_error _ | Error (Unify _,_) ->
 						loop cfl
 					end
 				| _ -> loop cfl

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

@@ -0,0 +1,38 @@
+package unit.issues;
+
+private abstract A(String) {
+
+	public function new(s) {
+		this = s;
+	}
+
+	@:arrayAccess function getString(index:String) {
+		return this + index;
+	}
+
+	@:arrayAccess function getInt(index:Int) {
+		return this + Std.string(index);
+	}
+
+	@:arrayAccess inline function setString(index:String, value:String) {
+		this += index + value;
+	}
+
+	@:arrayAccess inline function setInt(index:Int, value:String) {
+		this += Std.string(index) + value;
+	}
+}
+
+class Issue3432 extends Test {
+	function test() {
+		var a = new A("foo");
+		eq("foobar", a["bar"]);
+		eq("foo12", a[12]);
+		a["bar"] = "baz";
+		eq("foobarbazbaz", a["baz"]);
+		eq("foobarbaz12", a[12]);
+		a[12] = "foo";
+		eq("foobarbaz12foofoo", a["foo"]);
+		eq("foobarbaz12foo12", a[12]);
+	}
+}