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

[cs/java] do the same sorting/reversing of captured vars as done in handle_anon_func (closes #6304)

Dan Korostelev 8 жил өмнө
parent
commit
8fda02db50

+ 6 - 1
src/generators/gencommon/closuresToClass.ml

@@ -544,6 +544,11 @@ let configure gen ft =
 		)
 		~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
@@ -572,7 +577,7 @@ let configure gen ft =
 					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();
+	}
+}