Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Detect several simple syntax errors in the parser #16308

Closed
wants to merge 37 commits into from

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Feb 21, 2025

Summary

Currently stacked on #16090, this PR uses the framework there to detect several more simple version-related syntax errors in the parser:

  • Assignment expressions before 3.8
  • except* before 3.11
  • type statements before 3.12
  • type parameter lists before 3.12
  • type parameter defaults before 3.13

Test Plan

New CLI tests

ntBre and others added 30 commits February 19, 2025 11:16
I initially tried passing the target version to all of the parser functions, but
this required a huge number of changes. The downside of the current approach is
that we're likely to accumulate quite a large Vec<SyntaxError> in the parser,
only to filter it down later. The upside is that we don't have to fix every
single call site of every parser function right now
could be Copy as well, currently because Mode and PythonVersion are
This reverts commit 3f43c82.

This didn't work because the fuzzing only runs with cfg(test) not cfg(fuzzing)
@ntBre ntBre force-pushed the brent/simple-syntax-errors branch from ce9ab64 to f5a607c Compare February 21, 2025 19:51
Copy link
Contributor

github-actions bot commented Feb 21, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser
Copy link
Member

MichaReiser commented Feb 22, 2025

This might have been your plan all along but I think I'd prefer a PR for each specific check vs one large PR that implements all of them. It should make them easier to review and I don't think it's necessary to ship them all at once

@ntBre
Copy link
Contributor Author

ntBre commented Feb 22, 2025

Sounds good, I'll close this and split it up once #16257 and #16090 are merged since it's waiting on those anyway.

@ntBre ntBre force-pushed the brent/syntax-errors-parser branch from b06e1b1 to cd90966 Compare February 24, 2025 14:55
ntBre added a commit that referenced this pull request Feb 24, 2025
I also remove this in #16308, so even with additional variants we don't need the
`match`. It was needed in an earlier draft of this prototype to emit a different
syntax error as a ruff rule diagnostic
@ntBre
Copy link
Contributor Author

ntBre commented Feb 24, 2025

I'll go ahead and close this. I absorbed some of the changes into #16090, and I'll add each of the new errors in its own PR.

@ntBre ntBre closed this Feb 24, 2025
ntBre added a commit that referenced this pull request Feb 25, 2025
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
ntBre added a commit that referenced this pull request Feb 26, 2025
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
ntBre added a commit that referenced this pull request Feb 26, 2025
## Summary

This PR builds on the changes in #16220 to pass a target Python version
to the parser. It also adds the `Parser::unsupported_syntax_errors` field, which
collects version-related syntax errors while parsing. These syntax
errors are then turned into `Message`s in ruff (in preview mode).

This PR only detects one syntax error (`match` statement before Python
3.10), but it has been pretty quick to extend to several other simple
errors (see #16308 for example).

## Test Plan

The current tests are CLI tests in the linter crate, but these could be
supplemented with inline parser tests after #16357.

I also tested the display of these syntax errors in VS Code:


![image](https://github.com/user-attachments/assets/062b4441-740e-46c3-887c-a954049ef26e)

![image](https://github.com/user-attachments/assets/101f55b8-146c-4d59-b6b0-922f19bcd0fa)

---------

Co-authored-by: Alex Waygood <[email protected]>
ntBre added a commit that referenced this pull request Feb 26, 2025
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
ntBre added a commit that referenced this pull request Feb 27, 2025
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
ntBre added a commit that referenced this pull request Feb 28, 2025
## Summary
This PR is the first in a series derived from
#16308, each of which add support
for detecting one version-related syntax error from
#6591. This one should be
the largest because it also includes the addition of the 
`Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax
error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

## Test Plan
As noted above, there are new inline parser tests, as well as new ruff
CLI
tests. Once #16379 is resolved,
there should also be new mdtests for red-knot,
but this PR does not currently include those.
ntBre added a commit that referenced this pull request Feb 28, 2025
This PR is the first in a series derived from #16308, each of which add support
for detecting one version-related syntax error from #6591. This one should be
the largest because it also includes a couple of additional changes:
1. the `syntax_errors!` macro, which makes adding more variants a bit easier
2. the `Parser::add_unsupported_syntax_error` method

Otherwise I think the general structure will be the same for each syntax error:
* Detecting the error in the parser
* Inline parser tests for the new error
* New ruff CLI tests for the new error

Because of the second point here, this PR is currently stacked on #16357.

As noted above, there are new inline parser tests, as well as new ruff CLI
tests. Once #16379 is resolved, there should also be new mdtests for red-knot,
but this PR does not currently include those.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants