From dc1090290812d30975ee1ea2d7abb117136119e3 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 24 Oct 2023 13:28:38 +0200 Subject: [PATCH] Better error for definition followed by keyword Fixes #18750 --- .../dotty/tools/dotc/parsing/Parsers.scala | 2 + tests/neg/i18750.check | 104 ++++++++++++++++++ tests/neg/i18750.scala | 13 +++ 3 files changed, 119 insertions(+) create mode 100644 tests/neg/i18750.check create mode 100644 tests/neg/i18750.scala diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 1da8b0e6b0d3..2c923f71382e 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -3598,6 +3598,8 @@ object Parsers { * | id {`,' id} `:' Type `=' `_' (deprecated in 3.x) */ def patDefOrDcl(start: Offset, mods: Modifiers): Tree = atSpan(start, nameStart) { + if in.token != USCORE && isKeyword(in.token) then + syntaxError(ExpectedTokenButFound(IDENTIFIER, in.token), Span(in.offset)) val first = pattern2(Location.InPattern) var lhs = first match { case id: Ident if in.token == COMMA => diff --git a/tests/neg/i18750.check b/tests/neg/i18750.check new file mode 100644 index 000000000000..e0ef5c00988c --- /dev/null +++ b/tests/neg/i18750.check @@ -0,0 +1,104 @@ +-- [E040] Syntax Error: tests/neg/i18750.scala:3:4 --------------------------------------------------------------------- +3 |val do = 23 // error + | ^ + | an identifier expected, but 'do' found + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'do' as identifier, you may put it in backticks: `do`. + --------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:4:4 --------------------------------------------------------------------- +4 |val if = 23 // error + | ^ + | an identifier expected, but 'if' found + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'if' as identifier, you may put it in backticks: `if`. + --------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:5:4 --------------------------------------------------------------------- +5 |val val = 23 // error + | ^ + | an identifier expected, but 'val' found + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'val' as identifier, you may put it in backticks: `val`. + --------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:6:7 --------------------------------------------------------------------- +6 |val a, if = 23 // error + | ^^ + | an identifier expected, but 'if' found + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'if' as identifier, you may put it in backticks: `if`. + --------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:8:4 --------------------------------------------------------------------- +8 |def do; // error + | ^^ + | an identifier expected, but 'do' found + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'do' as identifier, you may put it in backticks: `do`. + --------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:9:4 --------------------------------------------------------------------- +9 |var do; // error + | ^ + | an identifier expected, but 'do' found + |--------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'do' as identifier, you may put it in backticks: `do`. + --------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:10:6 -------------------------------------------------------------------- +10 |class do; // error + | ^^ + | an identifier expected, but 'do' found + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'do' as identifier, you may put it in backticks: `do`. + -------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:11:7 -------------------------------------------------------------------- +11 |object if; // error // error + | ^^ + | an identifier expected, but 'if' found + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'if' as identifier, you may put it in backticks: `if`. + -------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:12:6 -------------------------------------------------------------------- +12 |trait else; // error + | ^^^^ + | an identifier expected, but 'else' found + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'else' as identifier, you may put it in backticks: `else`. + -------------------------------------------------------------------------------------------------------------------- +-- [E040] Syntax Error: tests/neg/i18750.scala:13:5 -------------------------------------------------------------------- +13 |type for; // error + | ^^^ + | an identifier expected, but 'for' found + |-------------------------------------------------------------------------------------------------------------------- + | Explanation (enabled by `-explain`) + |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + | + | If you want to use 'for' as identifier, you may put it in backticks: `for`. + -------------------------------------------------------------------------------------------------------------------- +-- [E161] Naming Error: tests/neg/i18750.scala:11:0 -------------------------------------------------------------------- +11 |object if; // error // error + |^ + | clashes with class in tests/neg/i18750.scala; the two must be defined together diff --git a/tests/neg/i18750.scala b/tests/neg/i18750.scala new file mode 100644 index 000000000000..6c083205cd52 --- /dev/null +++ b/tests/neg/i18750.scala @@ -0,0 +1,13 @@ +//> using options -explain + +val do = 23 // error +val if = 23 // error +val val = 23 // error +val a, if = 23 // error + +def do; // error +var do; // error +class do; // error +object if; // error // error +trait else; // error +type for; // error