Przeglądaj źródła

improve pattern error on invalid field (closes #4954)

Simon Krajewski 9 lat temu
rodzic
commit
c6b1ca07b8

+ 10 - 2
matcher.ml

@@ -137,6 +137,8 @@ module Pattern = struct
 		mutable in_reification : bool;
 	}
 
+	exception Bad_pattern of string
+
 	let rec to_string pat = match fst pat with
 		| PatConstructor(con,patterns) -> Printf.sprintf "%s(%s)" (Constructor.to_string con) (String.concat ", " (List.map to_string patterns))
 		| PatVariable v -> Printf.sprintf "%s<%i>" v.v_name v.v_id
@@ -190,6 +192,8 @@ module Pattern = struct
 					PatConstructor(ConConst ct,[])
 				| TCast(e1,None) ->
 					loop e1
+				| TField _ ->
+					raise (Bad_pattern "Only inline or read-only (default, never) fields can be used as pattern")
 				| _ ->
 					raise Exit
 			in
@@ -308,8 +312,12 @@ module Pattern = struct
 						fail()
 				end
 			| EField _ ->
-				begin try try_typing e
-				with Exit -> fail() end
+				begin try
+					try_typing e
+				with
+					| Exit -> fail()
+					| Bad_pattern s -> error s p
+				end
 			| EArrayDecl el ->
 				begin match follow t with
 					| TFun(tl,tr) when tr == fake_tuple_type ->

+ 9 - 0
tests/misc/projects/Issue4954/Main.hx

@@ -0,0 +1,9 @@
+class Main {
+    static var VALUE = 0;
+
+    public static function main() {
+        switch (0) {
+            case Main.VALUE:
+        }
+    }
+}

+ 2 - 0
tests/misc/projects/Issue4954/compile-fail.hxml

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

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

@@ -0,0 +1 @@
+Main.hx:6: characters 17-27 : Only inline or read-only (default, never) fields can be used as pattern