Browse Source

[typer] require init or type-hint for static vars too

closes #6440
Simon Krajewski 7 years ago
parent
commit
0142be1c0e

+ 1 - 0
extra/CHANGES.txt

@@ -3,6 +3,7 @@ XXXX-XX-XX:
 	General improvements and optimizations:
 
 	all : made `final` on structure fields invariant (#7039)
+	all : [breaking] disallowed static variables that have no type-hint and expression (#6440)
 	php : Optimized haxe.ds.Vector (VectorData is not Array anymore)
 	php : Optimized `Map.copy()` and `Array.copy()`
 	php : Support native PHP generators. See `php.Syntax.yield()` and `php.Generator`

+ 3 - 3
src/typing/typeloadFields.ml

@@ -770,8 +770,8 @@ let create_variable (ctx,cctx,fctx) c f t eo p =
 		else cctx.uninitialized_final <- Some f.cff_pos;
 	end;
 	let t = (match t with
-		| None when not fctx.is_static && eo = None ->
-			error ("Type required for member variable " ^ fst f.cff_name) p;
+		| None when eo = None ->
+			error ("Variable requires type-hint or initialization") (pos f.cff_name);
 		| None ->
 			mk_mono()
 		| Some t ->
@@ -1080,7 +1080,7 @@ let create_property (ctx,cctx,fctx) c f (get,set,t,eo) p =
 	let name = fst f.cff_name in
 	(* TODO is_lib: lazify load_complex_type *)
 	let ret = (match t, eo with
-		| None, None -> error (name ^ ": Property must either define a type or a default value") p;
+		| None, None -> error (name ^ ": Property requires type-hint or initialization") p;
 		| None, _ -> mk_mono()
 		| Some t, _ -> lazy_display_type ctx (fun () -> load_type_hint ctx p (Some t))
 	) in

+ 1 - 1
std/lua/Boot.hx

@@ -29,7 +29,7 @@ import haxe.Constraints.Function;
 class Boot {
 
 	// Used temporarily for bind()
-	static var _;
+	static var _:Dynamic;
 	static var _fid = 0;
 
 	// A max stack size to respect for unpack operations

+ 1 - 1
std/neko/_std/Math.hx

@@ -24,7 +24,7 @@ import neko.Lib;
 @:native("Math")
 @:keep
 private class MathImpl {
-	static var __rnd;
+	static var __rnd:Dynamic;
 	static var _rand_float = Lib.load("std","random_float",1);
 	static var _rand_int = Lib.load("std","random_int",2);
 

+ 1 - 1
std/neko/_std/String.hx

@@ -21,7 +21,7 @@
  */
 @:coreApi @:final class String {
 
-	static var __is_String;
+	static var __is_String = true;
 	private static var __split : Dynamic = neko.Lib.load("std","string_split",2);
 
 	static function __init__() : Void {

+ 4 - 4
tests/display/src/cases/Toplevel.hx

@@ -6,7 +6,7 @@ using Lambda;
 class Toplevel extends DisplayTestCase {
 	/**
 	class Main {
-		static var myField;
+		static var myField:String;
 		static function main() {{-1-}
 			{-2-}
 	**/
@@ -17,7 +17,7 @@ class Toplevel extends DisplayTestCase {
 
 	/**
 	class Main {
-		static var myField;
+		static var myField:String;
 		static function main() {
 			{-1-}
 			var a = "foo";
@@ -34,7 +34,7 @@ class Toplevel extends DisplayTestCase {
 
 	/**
 	class Main {
-		static var myField;
+		static var myField:String;
 		static function main() {
 			var a:{-1-}
 	**/
@@ -148,7 +148,7 @@ class Toplevel extends DisplayTestCase {
 
 	/**
 	class Main<ClassT> {
-		static var myField;
+		static var myField:String;
 		static function main<FieldT>() {
 			{-1-}
 		}

+ 2 - 2
tests/optimization/src/Macro.hx

@@ -6,8 +6,8 @@ using StringTools;
 
 class Macro {
 	static var classes = [];
-	static var output;
-	static var lines;
+	static var output:String;
+	static var lines:Array<String>;
 	static var tests = 0;
 	static var failures = 0;
 

+ 1 - 1
tests/optimization/src/issues/Issue6715.hx

@@ -46,7 +46,7 @@ class Issue6715 {
 		insanity4(function() var x = 1);
 	}
 
-	static var x;
+	static var x:Void->Void;
 
 	// Mixed: inline calls, reference reads
 	static inline function insanity(f:Void -> Void)