Skip to content

Commit 43bf1a8

Browse files
authored
Add tests for "keyword as identifier" syntax errors (#14754)
## Summary This is related to #13778, more specifically #13778 (comment). This PR adds various test cases where a keyword is being where an identifier is expected. The tests are to make sure that red knot doesn't panic, raises the syntax error and the identifier is added to the symbol table. The final part allows editor related features like renaming the symbol.
1 parent fda8b1f commit 43bf1a8

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Syntax errors
2+
3+
Test cases to ensure that red knot does not panic if there are syntax errors in the source code.
4+
5+
## Keyword as identifiers
6+
7+
When keywords are used as identifiers, the parser recovers from this syntax error by emitting an
8+
error and including the text value of the keyword to create the `Identifier` node.
9+
10+
### Name expression
11+
12+
```py
13+
# error: [invalid-syntax]
14+
pass = 1
15+
16+
# error: [invalid-syntax]
17+
# error: [invalid-syntax]
18+
type pass = 1
19+
20+
# error: [invalid-syntax]
21+
# error: [invalid-syntax]
22+
# error: [invalid-syntax]
23+
# error: [invalid-syntax]
24+
# error: [invalid-syntax]
25+
def True(for):
26+
# error: [invalid-syntax]
27+
pass
28+
29+
# TODO: Why is there two diagnostics for the same error?
30+
31+
# error: [invalid-syntax]
32+
# error: [invalid-syntax]
33+
# error: [invalid-syntax]
34+
# error: [unresolved-reference] "Name `pass` used when not defined"
35+
# error: [unresolved-reference] "Name `pass` used when not defined"
36+
for while in pass:
37+
pass
38+
39+
# error: [invalid-syntax]
40+
# error: [unresolved-reference] "Name `in` used when not defined"
41+
while in:
42+
pass
43+
44+
# error: [invalid-syntax]
45+
# error: [invalid-syntax]
46+
# error: [unresolved-reference] "Name `match` used when not defined"
47+
match while:
48+
# error: [invalid-syntax]
49+
# error: [invalid-syntax]
50+
# error: [invalid-syntax]
51+
# error: [unresolved-reference] "Name `case` used when not defined"
52+
case in:
53+
# error: [invalid-syntax]
54+
# error: [invalid-syntax]
55+
pass
56+
```
57+
58+
### Attribute expression
59+
60+
```py
61+
# TODO: Why is there two diagnostics for the same error?
62+
# TODO: Check when support for attribute expressions is added
63+
64+
# error: [invalid-syntax]
65+
# error: [unresolved-reference] "Name `foo` used when not defined"
66+
# error: [unresolved-reference] "Name `foo` used when not defined"
67+
for x in foo.pass:
68+
pass
69+
```

0 commit comments

Comments
 (0)