Преглед изворни кода

give proper error when accessing non-static abstract field from static one (cloes #4448)

Simon Krajewski пре 9 година
родитељ
комит
bae5cdf5a7

+ 21 - 0
tests/misc/projects/Issue4448/Main1.hx

@@ -0,0 +1,21 @@
+abstract A(Int) {
+    function f() {}
+
+    var v(get,never):Int;
+    function get_v() return this;
+
+    var v2(get,never):Int;
+    inline function get_v2() return this;
+
+    static function e() {
+        f(); // Not enough arguments, expected this:Int - should be just unavailable
+        v; // generates _$Main_A_$Impl_$.get_v();
+        v2; // Invalid abstract implementation function
+    }
+}
+
+class Main {
+	static function main() {
+
+	}
+}

+ 2 - 0
tests/misc/projects/Issue4448/compile1-fail.hxml

@@ -0,0 +1,2 @@
+-main Main1
+--interp

+ 3 - 0
tests/misc/projects/Issue4448/compile1-fail.hxml.stderr

@@ -0,0 +1,3 @@
+Main1.hx:11: characters 8-9 : Cannot access non-static field f from static method
+Main1.hx:12: characters 8-9 : Cannot access non-static field v from static method
+Main1.hx:13: characters 8-10 : Cannot access non-static field v2 from static method

+ 2 - 0
typer.ml

@@ -1362,6 +1362,8 @@ let rec type_ident_raise ctx i p mode =
 	with Not_found -> try
 		(* static variable lookup *)
 		let f = PMap.find i ctx.curclass.cl_statics in
+		if Meta.has Meta.Impl f.cf_meta && not (Meta.has Meta.Impl ctx.curfield.cf_meta) then
+			error (Printf.sprintf "Cannot access non-static field %s from static method" f.cf_name) p;
 		let e = type_type ctx ctx.curclass.cl_path p in
 		(* check_locals_masking already done in type_type *)
 		field_access ctx mode f (FStatic (ctx.curclass,f)) (field_type ctx ctx.curclass [] f p) e p