Skip to content

Commit 17cfa2a

Browse files
committed
Better error for definition followed by keyword
Fixes #18750 [Cherry-picked dc10902][modified]
1 parent 90cfbb4 commit 17cfa2a

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+2
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,8 @@ object Parsers {
35693569
* | id {`,' id} `:' Type `=' `_' (deprecated in 3.x)
35703570
*/
35713571
def patDefOrDcl(start: Offset, mods: Modifiers): Tree = atSpan(start, nameStart) {
3572+
if in.token != USCORE && isKeyword(in.token) then
3573+
syntaxError(ExpectedTokenButFound(IDENTIFIER, in.token), Span(in.offset))
35723574
val first = pattern2()
35733575
var lhs = first match {
35743576
case id: Ident if in.token == COMMA =>

tests/neg/i18750.check

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
-- [E040] Syntax Error: tests/neg/i18750.scala:3:4 ---------------------------------------------------------------------
2+
3 |val do = 23 // error
3+
| ^
4+
| an identifier expected, but 'do' found
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
|
9+
| If you want to use 'do' as identifier, you may put it in backticks: `do`.
10+
---------------------------------------------------------------------------------------------------------------------
11+
-- [E040] Syntax Error: tests/neg/i18750.scala:4:4 ---------------------------------------------------------------------
12+
4 |val if = 23 // error
13+
| ^
14+
| an identifier expected, but 'if' found
15+
|---------------------------------------------------------------------------------------------------------------------
16+
| Explanation (enabled by `-explain`)
17+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
18+
|
19+
| If you want to use 'if' as identifier, you may put it in backticks: `if`.
20+
---------------------------------------------------------------------------------------------------------------------
21+
-- [E040] Syntax Error: tests/neg/i18750.scala:5:4 ---------------------------------------------------------------------
22+
5 |val val = 23 // error
23+
| ^
24+
| an identifier expected, but 'val' found
25+
|---------------------------------------------------------------------------------------------------------------------
26+
| Explanation (enabled by `-explain`)
27+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
28+
|
29+
| If you want to use 'val' as identifier, you may put it in backticks: `val`.
30+
---------------------------------------------------------------------------------------------------------------------
31+
-- [E040] Syntax Error: tests/neg/i18750.scala:6:7 ---------------------------------------------------------------------
32+
6 |val a, if = 23 // error
33+
| ^^
34+
| an identifier expected, but 'if' found
35+
|---------------------------------------------------------------------------------------------------------------------
36+
| Explanation (enabled by `-explain`)
37+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
38+
|
39+
| If you want to use 'if' as identifier, you may put it in backticks: `if`.
40+
---------------------------------------------------------------------------------------------------------------------
41+
-- [E040] Syntax Error: tests/neg/i18750.scala:8:4 ---------------------------------------------------------------------
42+
8 |def do; // error
43+
| ^^
44+
| an identifier expected, but 'do' found
45+
|---------------------------------------------------------------------------------------------------------------------
46+
| Explanation (enabled by `-explain`)
47+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48+
|
49+
| If you want to use 'do' as identifier, you may put it in backticks: `do`.
50+
---------------------------------------------------------------------------------------------------------------------
51+
-- [E040] Syntax Error: tests/neg/i18750.scala:9:4 ---------------------------------------------------------------------
52+
9 |var do; // error
53+
| ^
54+
| an identifier expected, but 'do' found
55+
|---------------------------------------------------------------------------------------------------------------------
56+
| Explanation (enabled by `-explain`)
57+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58+
|
59+
| If you want to use 'do' as identifier, you may put it in backticks: `do`.
60+
---------------------------------------------------------------------------------------------------------------------
61+
-- [E040] Syntax Error: tests/neg/i18750.scala:10:6 --------------------------------------------------------------------
62+
10 |class do; // error
63+
| ^^
64+
| an identifier expected, but 'do' found
65+
|--------------------------------------------------------------------------------------------------------------------
66+
| Explanation (enabled by `-explain`)
67+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68+
|
69+
| If you want to use 'do' as identifier, you may put it in backticks: `do`.
70+
--------------------------------------------------------------------------------------------------------------------
71+
-- [E040] Syntax Error: tests/neg/i18750.scala:11:7 --------------------------------------------------------------------
72+
11 |object if; // error // error
73+
| ^^
74+
| an identifier expected, but 'if' found
75+
|--------------------------------------------------------------------------------------------------------------------
76+
| Explanation (enabled by `-explain`)
77+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
78+
|
79+
| If you want to use 'if' as identifier, you may put it in backticks: `if`.
80+
--------------------------------------------------------------------------------------------------------------------
81+
-- [E040] Syntax Error: tests/neg/i18750.scala:12:6 --------------------------------------------------------------------
82+
12 |trait else; // error
83+
| ^^^^
84+
| an identifier expected, but 'else' found
85+
|--------------------------------------------------------------------------------------------------------------------
86+
| Explanation (enabled by `-explain`)
87+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
88+
|
89+
| If you want to use 'else' as identifier, you may put it in backticks: `else`.
90+
--------------------------------------------------------------------------------------------------------------------
91+
-- [E040] Syntax Error: tests/neg/i18750.scala:13:5 --------------------------------------------------------------------
92+
13 |type for; // error
93+
| ^^^
94+
| an identifier expected, but 'for' found
95+
|--------------------------------------------------------------------------------------------------------------------
96+
| Explanation (enabled by `-explain`)
97+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98+
|
99+
| If you want to use 'for' as identifier, you may put it in backticks: `for`.
100+
--------------------------------------------------------------------------------------------------------------------
101+
-- [E161] Naming Error: tests/neg/i18750.scala:11:0 --------------------------------------------------------------------
102+
11 |object if; // error // error
103+
|^
104+
|<error> clashes with class <error> in tests/neg/i18750.scala; the two must be defined together

tests/neg/i18750.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//> using options -explain
2+
3+
val do = 23 // error
4+
val if = 23 // error
5+
val val = 23 // error
6+
val a, if = 23 // error
7+
8+
def do; // error
9+
var do; // error
10+
class do; // error
11+
object if; // error // error
12+
trait else; // error
13+
type for; // error

0 commit comments

Comments
 (0)