From 40b0b67dd911c64bbe4ac7c197758e210234fc91 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Fri, 6 Dec 2024 08:11:33 +0530 Subject: [PATCH] [red-knot] Separate invalid syntax code snippets (#14803) Ref: https://github.com/astral-sh/ruff/pull/14788#discussion_r1872242283 This PR: * Separates code snippets as individual tests for the invalid syntax cases * Adds a general comment explaining why the parser could emit more syntax errors than expected --- .../resources/mdtest/invalid_syntax.md | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/crates/red_knot_python_semantic/resources/mdtest/invalid_syntax.md b/crates/red_knot_python_semantic/resources/mdtest/invalid_syntax.md index e33b7cb819..435e818515 100644 --- a/crates/red_knot_python_semantic/resources/mdtest/invalid_syntax.md +++ b/crates/red_knot_python_semantic/resources/mdtest/invalid_syntax.md @@ -2,6 +2,13 @@ Test cases to ensure that red knot does not panic if there are syntax errors in the source code. +The parser cannot recover from certain syntax errors completely which is why the number of syntax +errors could be more than expected in the following examples. For instance, if there's a keyword +(like `for`) in the middle of another statement (like function definition), then it's more likely +that the rest of the tokens are going to be part of the `for` statement and not the function +definition. But, it's not necessary that the remaining tokens are valid in the context of a `for` +statement. + ## Keyword as identifiers When keywords are used as identifiers, the parser recovers from this syntax error by emitting an @@ -9,35 +16,57 @@ error and including the text value of the keyword to create the `Identifier` nod ### Name expression +#### Assignment + ```py # error: [invalid-syntax] pass = 1 +``` +#### Type alias + +```py # error: [invalid-syntax] # error: [invalid-syntax] type pass = 1 +``` +#### Function definition + +```py # error: [invalid-syntax] # error: [invalid-syntax] # error: [invalid-syntax] # error: [invalid-syntax] # error: [invalid-syntax] def True(for): + # error: [invalid-syntax] # error: [invalid-syntax] pass +``` -# error: [invalid-syntax] +#### For + +```py # error: [invalid-syntax] # error: [invalid-syntax] # error: [unresolved-reference] "Name `pass` used when not defined" for while in pass: pass +``` +#### While + +```py # error: [invalid-syntax] # error: [unresolved-reference] "Name `in` used when not defined" while in: pass +``` +#### Match + +```py # error: [invalid-syntax] # error: [invalid-syntax] # error: [unresolved-reference] "Name `match` used when not defined"