Skip to content

fix No narrowing for unions in match when the subject is a call #2858#3319

Open
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
asukaminato0721:2858
Open

fix No narrowing for unions in match when the subject is a call #2858#3319
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
asukaminato0721:2858

Conversation

@asukaminato0721
Copy link
Copy Markdown
Contributor

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

@meta-cla meta-cla Bot added the cla signed label May 6, 2026
@github-actions github-actions Bot added the size/l label May 6, 2026
@github-actions

This comment has been minimized.

@asukaminato0721 asukaminato0721 marked this pull request as ready for review May 6, 2026 19:12
Copilot AI review requested due to automatic review settings May 6, 2026 19:12
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::Anonymous and a PatternNarrowOps helper 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, _| {
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.
@github-actions github-actions Bot added size/l and removed size/l labels May 7, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 7, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No narrowing for unions in match when the subject is a call

2 participants