-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
fda8b1f
commit 43bf1a8
Showing
1 changed file
with
69 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
crates/red_knot_python_semantic/resources/mdtest/invalid_syntax.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Syntax errors | ||
|
||
Test cases to ensure that red knot does not panic if there are syntax errors in the source code. | ||
|
||
## Keyword as identifiers | ||
|
||
When keywords are used as identifiers, the parser recovers from this syntax error by emitting an | ||
error and including the text value of the keyword to create the `Identifier` node. | ||
|
||
### Name expression | ||
|
||
```py | ||
# error: [invalid-syntax] | ||
pass = 1 | ||
|
||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
type pass = 1 | ||
|
||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
def True(for): | ||
# error: [invalid-syntax] | ||
pass | ||
|
||
# TODO: Why is there two diagnostics for the same error? | ||
|
||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
# error: [unresolved-reference] "Name `pass` used when not defined" | ||
# error: [unresolved-reference] "Name `pass` used when not defined" | ||
for while in pass: | ||
pass | ||
|
||
# error: [invalid-syntax] | ||
# error: [unresolved-reference] "Name `in` used when not defined" | ||
while in: | ||
pass | ||
|
||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
# error: [unresolved-reference] "Name `match` used when not defined" | ||
match while: | ||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
# error: [unresolved-reference] "Name `case` used when not defined" | ||
case in: | ||
# error: [invalid-syntax] | ||
# error: [invalid-syntax] | ||
pass | ||
``` | ||
|
||
### Attribute expression | ||
|
||
```py | ||
# TODO: Why is there two diagnostics for the same error? | ||
# TODO: Check when support for attribute expressions is added | ||
|
||
# error: [invalid-syntax] | ||
# error: [unresolved-reference] "Name `foo` used when not defined" | ||
# error: [unresolved-reference] "Name `foo` used when not defined" | ||
for x in foo.pass: | ||
pass | ||
``` |