Browse Source

[cs/java] do the same sorting/reversing of captured vars as done in handle_anon_func (see #6304, cherry-picked by hand)

Dan Korostelev 8 years ago
parent
commit
7e7b640fe0
2 changed files with 22 additions and 1 deletions
  1. 6 1
      src/generators/gencommon.ml
  2. 16 0
      tests/unit/src/unit/issues/Issue6304.hx

+ 6 - 1
src/generators/gencommon.ml

@@ -3128,6 +3128,11 @@ struct
 			)
 			~tparam_anon_acc:(fun v e -> try
 				let cls, captured = Hashtbl.find tvar_to_cdecl v.v_id in
+				let captured = List.sort (fun e1 e2 -> match e1, e2 with
+					| { eexpr = TLocal v1 }, { eexpr = TLocal v2 } ->
+						compare v1.v_name v2.v_name
+					| _ -> assert false) captured
+				in
 				let types = match v.v_extra with
 					| Some(t,_) -> t
 					| _ -> assert false
@@ -3156,7 +3161,7 @@ struct
 						snd (List.find (fun (t2,_) -> same_cl t t2) passoc)
 					with | Not_found -> t) cls.cl_params
 				in
-				{ e with eexpr = TNew(cls, cltparams, captured) }
+				{ e with eexpr = TNew(cls, cltparams, List.rev captured) }
 			with
 				| Not_found ->
 				gen.gcon.warning "This expression may be invalid" e.epos;

+ 16 - 0
tests/unit/src/unit/issues/Issue6304.hx

@@ -0,0 +1,16 @@
+package unit.issues;
+
+class Issue6304 extends unit.Test {
+	function test() {
+		eq(2, main1([], 1));
+	}
+
+	static function main1 (arr:Array<{}>, multiplier:Int) {
+		function doSomething <T>() {
+			var mul:Int =  multiplier;
+			arr.push({});
+			return arr.length + mul;
+		};
+		return doSomething();
+	}
+}