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

[parser] error nicer when keywords are used as var names

closes #10358
Simon Krajewski пре 4 година
родитељ
комит
005fdb937e

+ 1 - 0
src/syntax/grammar.mly

@@ -1184,6 +1184,7 @@ and parse_var_decl_head final s =
 	let meta = parse_meta s in
 	match s with parser
 	| [< name, p = dollar_ident; t = popt parse_type_hint >] -> (meta,name,final,t,p)
+	| [< >] -> no_keyword "variable name" s
 
 and parse_var_assignment = parser
 	| [< '(Binop OpAssign,p1); s >] ->

+ 6 - 1
src/syntax/parser.ml

@@ -420,4 +420,9 @@ let check_signature_mark e p1 p2 =
 	end
 
 let convert_abstract_flags flags =
-	ExtList.List.filter_map decl_flag_to_abstract_flag flags
+	ExtList.List.filter_map decl_flag_to_abstract_flag flags
+
+let no_keyword what s =
+	match Stream.peek s with
+	| Some (Kwd kwd,p) -> error (Custom ("Keyword " ^ (s_keyword kwd) ^ " cannot be used as " ^ what)) p
+	| _ -> raise Stream.Failure

+ 5 - 0
tests/misc/projects/Issue10358/Main.hx

@@ -0,0 +1,5 @@
+class Main {
+	static function main() {
+		var final;
+	}
+}

+ 5 - 0
tests/misc/projects/Issue10358/Main2.hx

@@ -0,0 +1,5 @@
+class Main {
+	static function main() {
+		var not, final;
+	}
+}

+ 5 - 0
tests/misc/projects/Issue10358/Main3.hx

@@ -0,0 +1,5 @@
+class Main {
+	static function main() {
+		var final = 0;
+	}
+}

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

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

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

@@ -0,0 +1 @@
+Main.hx:3: characters 7-12 : Keyword final cannot be used as variable name

+ 1 - 0
tests/misc/projects/Issue10358/compile2-fail.hxml

@@ -0,0 +1 @@
+--main Main2

+ 1 - 0
tests/misc/projects/Issue10358/compile2-fail.hxml.stderr

@@ -0,0 +1 @@
+Main2.hx:3: characters 12-17 : Keyword final cannot be used as variable name

+ 1 - 0
tests/misc/projects/Issue10358/compile3-fail.hxml

@@ -0,0 +1 @@
+--main Main3

+ 1 - 0
tests/misc/projects/Issue10358/compile3-fail.hxml.stderr

@@ -0,0 +1 @@
+Main3.hx:3: characters 7-12 : Keyword final cannot be used as variable name