fix No narrowing for unions in match when the subject is a call #2858#3319
Open
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
Open
fix No narrowing for unions in match when the subject is a call #2858#3319asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
This PR fixes match-case fallthrough narrowing when the match subject does not produce a user-visible narrowing name (e.g. match f(x):), so later cases (including a final capture case) see the subject narrowed by the negation of previous cases. This addresses issue #2858, where assert_never in the final capture case incorrectly saw a non-Never type.
Changes:
- Introduces
MatchSubject::Anonymousand aPatternNarrowOpshelper to track both scope/name-based narrows and anonymous-subject narrows. - Carries anonymous-subject negated narrows across match cases and applies them to the already-evaluated subject for subsequent cases.
- Adds regression tests covering exhaustive union matching with a call subject and a non-exhaustive class-args case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
pyrefly/lib/binding/pattern.rs |
Implements anonymous-subject fallthrough narrowing across match cases via PatternNarrowOps and negated_prev_subject. |
pyrefly/lib/test/pattern_match.rs |
Adds regression tests for exhaustive match f(x) union narrowing and a non-exhaustive class-args scenario. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
728
to
+734
| // Only accumulate narrows for the match subject. Alias names | ||
| // from MatchAs were already copied to the subject via | ||
| // and_for_subject and would create spurious entries if they | ||
| // shadow outer variables. When there is no narrowing subject | ||
| // (e.g. `match make_color():`), drop all narrows so that alias | ||
| // names don't resolve against unrelated outer variables. | ||
| new_narrow_ops.0.retain(|name, _| { | ||
| new_narrow_ops.scope.0.retain(|name, _| { |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #2858
match subjects without a real narrowing name, like match f(x):, still carry direct fallthrough narrows across cases.
The final capture now forwards to the already-evaluated subject narrowed by previous cases, without synthesizing a user-visible name.
Test Plan
add test