-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix syntax error false positive on nested alternative patterns #21104
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
crates/ruff_python_parser/resources/inline/ok/nested_alternative_patterns.py
This file contains hidden or 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,7 @@ | ||
| match ruff: | ||
| case {"lint": {"select": x} | {"extend-select": x}} | {"select": x}: | ||
| ... | ||
| match 42: | ||
| case [[x] | [x]] | x: ... | ||
| match 42: | ||
| case [[x | x] | [x]] | x: ... |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a good understanding of this part. So please disregard if not relevant or I'm just wrong ;)
Would it be better to return the union of all
visitor.nameshere instead of just the last one in case we encountered aDifferentMatchPatternBindingserror? Or is there a risk that this introduces other false positives? If so, should we return the intersection of all names instead?(I suspect that either approach can lead to false positives depending on how the pattern are nested, so it might not be a case where there's no "better way" of doing this, it's just trade offs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I think that's a good question. (This part was not intuitive to me at all, I was working on a more complicated fix when I realized this solved the issue)
I guess this could come into play with cases like this:
We currently emit two diagnostics for the first and only one for the second:
Hmm, we actually get the same answer here with the union, but we get two errors in both cases with the intersection.
Maybe that makes the intersection the best option for consistency's sake?
I think I'd consider these all true positives at least, but they may be a bit redundant. CPython only emits the innermost diagnostic, so we could also consider that.
This is also pretty hard to hit. Because of the
breakafter emitting aDifferentMatchPatternBindingsdiagnostic, you need at least two inner patterns with the same name, followed by one with a different name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, the intersection breaks the tests, I guess because
self.namesis initially empty. So I guess we should either stick with replacing or the union.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just not intersect until we visited at least one pattern? Like use an
Option<FxHashSet>and only intersect when it's some?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be misunderstanding the suggestion, but I tried a patch like the one below, and it caused some other tests to fail (and without changing the new diagnostics from this PR). It's not just when
self.namesis initially empty as I said before, this also affects cases like:where one of the branches is genuinely empty.
I initially included the .snap.new files in this patch, but they were way too big. We lost the diagnostics in the cases above.
I'm leaning toward sticking with the simple existing code unless I'm just missing something.
Patch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not what I meant, I think?
I'd created a
Option<FxHashSet>right outside thefor pattern in patterns {rather than globally in the visitorBut i also think that what we have is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have one of those too:
ruff/crates/ruff_python_parser/src/semantic_errors.rs
Lines 1809 to 1811 in d38a529
We need an outer one in the visitor (or at least somewhere else) for recursive cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll go ahead and land this for now then since it should resolve the bug!