Bladeren bron

better error for static access on an instance field (closes #9612)

Aleksandr Kuzmenko 5 jaren geleden
bovenliggende
commit
b62cfde72c

+ 5 - 1
src/typing/fields.ml

@@ -319,7 +319,11 @@ let rec type_field cfg ctx e i p mode =
 				(* the abstract field is not part of the field list, which is only true when it has no expression (issue #2344) *)
 				display_error ctx ("Field " ^ i ^ " cannot be called directly because it has no expression") pfield;
 			| _ ->
-				display_error ctx (StringError.string_error i (string_source t) (s_type (print_context()) t ^ " has no field " ^ i)) pfield;
+				match follow t with
+				| TAnon { a_status = { contents = Statics c } } when PMap.mem i c.cl_fields ->
+					display_error ctx ("Static access to instance field " ^ i ^ " is not allowed") pfield;
+				| _ ->
+					display_error ctx (StringError.string_error i (string_source t) (s_type (print_context()) t ^ " has no field " ^ i)) pfield;
 		end;
 		AKExpr (mk (TField (e,FDynamic i)) (mk_mono()) p)
 	in

+ 7 - 0
tests/misc/projects/Issue9612/Main.hx

@@ -0,0 +1,7 @@
+class Main {
+	static function main() {
+		Main.instanceMethod();
+	}
+
+	function instanceMethod() {}
+}

+ 1 - 0
tests/misc/projects/Issue9612/compile-fail.hxml

@@ -0,0 +1 @@
+--run Main

+ 1 - 0
tests/misc/projects/Issue9612/compile-fail.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:3: characters 8-22 : Static access to instance field instanceMethod is not allowed