Browse Source

bugfix in local functions

Nicolas Cannasse 17 years ago
parent
commit
f4d55d6324
4 changed files with 18 additions and 13 deletions
  1. 2 2
      Makefile.win
  2. 8 0
      tests/unit/TestLocals.hx
  3. 6 9
      typeload.ml
  4. 2 2
      typer.ml

+ 2 - 2
Makefile.win

@@ -20,7 +20,7 @@ haxe.exe: $(FILES)
 
 ../neko/libs/include/ocaml/binast.cmx: ../neko/libs/include/ocaml/nast.cmx
 
-genneko.cmx: type.cmx ../neko/libs/include/ocaml/nxml.cmx ../neko/libs/include/ocaml/nast.cmx lexer.cmx common.cmx ../neko/libs/include/ocaml/binast.cmx ast.cmx
+genneko.cmx: type.cmx codegen.cmx ../neko/libs/include/ocaml/nxml.cmx ../neko/libs/include/ocaml/nast.cmx lexer.cmx common.cmx ../neko/libs/include/ocaml/binast.cmx ast.cmx
 
 ../neko/libs/include/ocaml/nxml.cmx: ../neko/libs/include/ocaml/nast.cmx
 
@@ -34,7 +34,7 @@ genas3.cmx: type.cmx codegen.cmx ../../mtcvs/swflib/swfParser.cmx ../../mtcvs/sw
 
 genjs.cmx: type.cmx codegen.cmx lexer.cmx common.cmx ast.cmx
 
-genswf.cmx: type.cmx ../../mtcvs/swflib/swfZip.cmx ../../mtcvs/swflib/swfParser.cmx ../../mtcvs/swflib/swf.cmx genswf9.cmx genswf8.cmx common.cmx ast.cmx ../../mtcvs/swflib/as3hlparse.cmx ../../mtcvs/swflib/as3hl.cmi ../../mtcvs/swflib/as3.cmi
+genswf.cmx: type.cmx codegen.cmx ../../mtcvs/swflib/swfZip.cmx ../../mtcvs/swflib/swfParser.cmx ../../mtcvs/swflib/swf.cmx genswf9.cmx genswf8.cmx common.cmx ast.cmx ../../mtcvs/swflib/as3hlparse.cmx ../../mtcvs/swflib/as3hl.cmi ../../mtcvs/swflib/as3.cmi
 
 genswf8.cmx: type.cmx codegen.cmx ../../mtcvs/swflib/swf.cmx lexer.cmx common.cmx ast.cmx ../../mtcvs/swflib/actionScript.cmx
 

+ 8 - 0
tests/unit/TestLocals.hx

@@ -125,4 +125,12 @@ class TestLocals extends Test {
 		}
 	}
 
+	function testPossibleBug() {
+		var funs = new Array();
+		for( i in 0...5 )
+			funs.push(function(i) return i);
+		for( k in 0...5 )
+			eq( funs[k](55), 55 );
+	}
+
 }

+ 6 - 9
typeload.ml

@@ -427,19 +427,16 @@ let type_type_params ctx path p (n,flags) =
 		ctx.delays := [(fun () -> ignore(!r()))] :: !(ctx.delays);
 		n, TLazy r
 
-let type_function ctx t static constr f p =
+let type_function ctx args ret static constr f p =
 	let locals = save_locals ctx in
-	let fargs , r = (match t with
-		| TFun (args,r) -> List.map (fun (n,opt,t) -> add_local ctx n t, opt, t) args, r
-		| _ -> assert false
-	) in
+	let fargs = List.map (fun (n,c,t) -> add_local ctx n t, c, t) args in
 	let old_ret = ctx.ret in
 	let old_static = ctx.in_static in
 	let old_constr = ctx.in_constructor in
 	let old_opened = ctx.opened in
 	ctx.in_static <- static;
 	ctx.in_constructor <- constr;
-	ctx.ret <- r;
+	ctx.ret <- ret;
 	ctx.opened <- [];
 	let e = type_expr ctx f.f_expr false in
 	let rec loop e =
@@ -452,7 +449,7 @@ let type_function ctx t static constr f p =
 	if have_ret then
 		(try return_flow ctx e with Exit -> ())
 	else
-		unify ctx r ctx.api.tvoid p;
+		unify ctx ret ctx.api.tvoid p;
 	let rec loop e =
 		match e.eexpr with
 		| TCall ({ eexpr = TConst TSuper },_) -> raise Exit
@@ -603,9 +600,9 @@ let init_class ctx c p herits fields =
 			let r = exc_protect (fun r ->
 				r := (fun() -> t);
 				if ctx.com.verbose then print_endline ("Typing " ^ s_type_path c.cl_path ^ "." ^ name);
-				let e , fargs = type_function ctx t stat constr f p in
+				let e , fargs = type_function ctx args ret stat constr f p in
 				let f = {
-					tf_args = args;
+					tf_args = fargs;
 					tf_type = ret;
 					tf_expr = e;
 				} in

+ 2 - 2
typer.ml

@@ -1220,9 +1220,9 @@ and type_expr ctx ?(need_val=true) (e,p) =
 				) args args2;
 			| _ -> ());
 		let ft = TFun (fun_args args,rt) in
-		let e , fargs = Typeload.type_function ctx ft true false f p in
+		let e , fargs = Typeload.type_function ctx args rt true false f p in
 		let f = {
-			tf_args = args;
+			tf_args = fargs;
 			tf_type = rt;
 			tf_expr = e;
 		} in