浏览代码

- allow TTypeExpr for variable initialization (fixed issue #929)
- remove inline var restrictions for 2.10

Simon Krajewski 13 年之前
父节点
当前提交
a4eb906336
共有 5 个文件被更改,包括 13 次插入6 次删除
  1. 6 4
      codegen.ml
  2. 1 0
      tests/unit/MyClass.hx
  3. 3 0
      tests/unit/TestType.hx
  4. 2 1
      typeload.ml
  5. 1 1
      typer.ml

+ 6 - 4
codegen.ml

@@ -465,10 +465,12 @@ let on_inherit ctx c p h =
 	Adds member field initializations as assignments to the constructor
 *)
 let add_field_inits com c =
-	let rec can_init_inline cf e = match com.platform with
-		| Flash8 -> true
-		| Flash when Common.defined com "as3" -> true
-		| Php when (match cf.cf_kind with Var({v_write = AccCall _}) -> false | _ -> true) -> true
+	let rec can_init_inline cf e = match com.platform,e.eexpr with
+		| Flash8,_ -> true
+		| Flash,_ when Common.defined com "as3" -> true
+		| Php, TTypeExpr _ -> false
+		| Php,_ ->
+			(match cf.cf_kind with Var({v_write = AccCall _}) -> false | _ -> true)
 		| _ -> false
 	in
 	let inits = List.filter (fun cf ->

+ 1 - 0
tests/unit/MyClass.hx

@@ -99,6 +99,7 @@ class InitBase {
 	public var i = 2;
 	public var s = "foo";
 	public var b = true;
+	public var t = String;
 	public function new() { }
 }
 

+ 3 - 0
tests/unit/TestType.hx

@@ -347,16 +347,19 @@ class TestType extends Test {
 		eq(c.i, 2);
 		eq(c.s, "foo");
 		eq(c.b, true);
+		eq(c.t, untyped String);
 		
 		var c = new InitChild();
 		eq(c.i, 2);
 		eq(c.s, "foo");
 		eq(c.b, true);
+		eq(c.t, untyped String);
 		
 		var c = new InitChildWithCtor(null);
 		eq(c.i, 2);
 		eq(c.s, "foo");
 		eq(c.b, true);
+		eq(c.t, untyped String);
 		
 		var c = Type.createInstance(InitWithoutCtor, []);
 		eq(c.i, 2);

+ 2 - 1
typeload.ml

@@ -950,13 +950,14 @@ let init_class ctx c p herits fields =
 					if not inline then mark_used cf;
 					let e = type_var_field ctx t e stat p in
 					let e = (match cf.cf_kind with
-					| Var v when not stat || v.v_read = AccInline ->
+					| Var v when not stat ->
 						let e = ctx.g.do_optimize ctx e in
 						let rec is_const e =
 							match e.eexpr with
 							| TConst _ -> true
 							| TBinop ((OpAdd|OpSub|OpMult|OpDiv|OpMod),e1,e2) -> is_const e1 && is_const e2
 							| TParenthesis e -> is_const e
+							| TTypeExpr e -> true
 							| _ -> false
 						in
 						if not (is_const e) then display_error ctx "Variable initialization must be a constant value" p;

+ 1 - 1
typer.ml

@@ -576,7 +576,7 @@ let get_this ctx p =
 		if ctx.untyped then display_error ctx "Cannot access this in 'untyped' mode : use either '__this__' or var 'me = this' (transitional)" p;
 		let v = (match ctx.vthis with
 			| None ->
-				let v = add_local ctx "me" ctx.tthis in
+				let v = gen_local ctx ctx.tthis in
 				ctx.vthis <- Some v;
 				v
 			| Some v -> v