Explorar el Código

restrict member field inits to constant expressions

Simon Krajewski hace 13 años
padre
commit
98e108f813
Se han modificado 5 ficheros con 6 adiciones y 24 borrados
  1. 4 9
      codegen.ml
  2. 0 4
      tests/unit/MyClass.hx
  3. 0 3
      tests/unit/TestMisc.hx
  4. 0 5
      tests/unit/TestType.hx
  5. 2 3
      typeload.ml

+ 4 - 9
codegen.ml

@@ -465,15 +465,10 @@ 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,e.eexpr with
-		(* Flash8 and As3 can init a lot of things with some exceptions *)
-		| Flash8, TBlock _ -> false
-		| Flash8, _ -> true
-		| Flash,_ when Common.defined com "as3" -> true
-		(* Php can init literals when the field has no setter *)
-		| Php, TConst _ when (match cf.cf_kind with Var({v_write = AccCall _}) -> false | _ -> true) -> true
-		(* Flash9 should support some field inits as well, but they are currently not generated *)
-		| Flash, TConst _ -> false
+	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
 		| _ -> false
 	in
 	let inits = List.filter (fun cf ->

+ 0 - 4
tests/unit/MyClass.hx

@@ -99,16 +99,12 @@ class InitBase {
 	public var i = 2;
 	public var s = "foo";
 	public var b = true;
-	public var a = [true, false];
-	public var complex = { var i = 10; for (v in 0...15) i++; i; };
-	public var newInit = new MyClass(12);
 	public function new() { }
 }
 
 class InitChild extends InitBase { }
 
 class InitChildWithCtor extends InitBase {
-	public var t:Class<Dynamic> = String;
 	public function new(_) {
 		super();
 	}

+ 0 - 3
tests/unit/TestMisc.hx

@@ -326,15 +326,12 @@ class TestMisc extends Test {
 	}
 
 	static inline function foo(x) return x + 5
-	static inline var type = String;
 	
 	function testInline() {
 		// check that operations are correctly generated
 		var x = 3; // prevent optimization
 		eq( 2 * foo(x), 16 );
 		eq( -foo(x), -8 );
-		
-		t(Std.is(Type.createEmptyInstance(type), String));
 	}
 
 	function testEvalAccessOrder() {

+ 0 - 5
tests/unit/TestType.hx

@@ -359,10 +359,6 @@ class TestType extends Test {
 		eq(c.i, 2);
 		eq(c.s, "foo");
 		eq(c.b, true);
-		t(c.a[0]);
-		f(c.a[1]);
-		eq(c.complex, 25);
-		eq(c.newInit.get(), 12);
 		
 		var c = new InitChild();
 		eq(c.i, 2);
@@ -373,7 +369,6 @@ class TestType extends Test {
 		eq(c.i, 2);
 		eq(c.s, "foo");
 		eq(c.b, true);
-		eq(c.t, String);
 		
 		var c = Type.createInstance(InitWithoutCtor, []);
 		eq(c.i, 2);

+ 2 - 3
typeload.ml

@@ -947,17 +947,16 @@ 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_read = AccInline } ->
+					| Var v when not stat || v.v_read = AccInline ->
 						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 _ -> true
 							| _ -> false
 						in
-						if not (is_const e) then display_error ctx "Inline variable must be a constant value" p;
+						if not (is_const e) then display_error ctx "Variable initialization must be a constant value" p;
 						e
 					| _ ->
 						e