Skip to content

fix Sequence pattern in match/case does not narrow tuple out of a union #3147#3165

Open
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:3147
Open

fix Sequence pattern in match/case does not narrow tuple out of a union #3147#3165
asukaminato0721 wants to merge 1 commit intofacebook:mainfrom
asukaminato0721:3147

Conversation

@asukaminato0721
Copy link
Copy Markdown
Contributor

@asukaminato0721 asukaminato0721 commented Apr 16, 2026

Summary

Fixes #3147

Sequence patterns like (_, _) were picking up Placeholder narrows from wildcard and other irrefutable subpatterns, and those placeholders intentionally block negative narrowing.

In a later case, that prevented the fallback arm from collapsing to Never.

now strip those placeholders when every sequence subpattern is irrefutable, so the carried negation stays precise.

Test Plan

add test

@github-actions
Copy link
Copy Markdown

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

@asukaminato0721 asukaminato0721 marked this pull request as ready for review April 16, 2026 15:58
Copilot AI review requested due to automatic review settings April 16, 2026 15:58
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

Fixes pyrefly’s match/case narrowing so that an irrefutable sequence pattern (e.g. (_, _)) properly participates in negative narrowing across subsequent case arms, allowing union exhaustiveness to be recognized (Issue #3147).

Changes:

  • Add a regression test covering float | tuple[float, float] exhaustiveness with a tuple-shaped sequence pattern plus a float() class pattern.
  • In sequence-pattern binding, detect when all subpatterns are irrefutable/wildcards and strip Placeholder narrow ops so they don’t block negated narrowing across later cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pyrefly/lib/test/pattern_match.rs Adds a regression test for match exhaustiveness/narrowing in the reported tuple-in-union scenario.
pyrefly/lib/binding/pattern.rs Adjusts sequence-pattern narrowing to remove spurious placeholders when the sequence subpatterns are all irrefutable, improving cross-case narrowing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if all_subpatterns_irrefutable
&& let Some(subject) = match_subject.as_single()
&& let Some((op, _)) = narrow_ops.0.get_mut(subject.name())
{
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.

Sequence pattern in match/case does not narrow tuple out of a union

3 participants