Skip to content

Conversation

@asukaminato0721
Copy link
Contributor

Summary

Fixes #598

Updated Pattern::MatchAs so that, when a case introduces an alias, the resultant narrows are mirrored back onto the original match subject (without rebinding names twice), ensuring later branches see the refined type.

Introduced NarrowOp::rebase_subject plus facet-merging helpers and reworked NarrowOps::and_for_subject in to re-target alias narrows, compose facet chains/origins, and insert the rebased operations without placeholder pollution so negated flows correctly eliminate matched variants.

Test Plan

Added test_match_alias_narrows_subject

@meta-cla meta-cla bot added the cla signed label Jan 3, 2026
@github-actions

This comment has been minimized.

@asukaminato0721 asukaminato0721 marked this pull request as ready for review January 3, 2026 07:43
Copilot AI review requested due to automatic review settings January 3, 2026 07:43
Copy link

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 issue #598 where match case patterns with type guards and aliases (using as) were not properly narrowing the original match subject. The fix ensures that when a case introduces an alias, type narrowing operations are mirrored back onto the original subject, allowing subsequent branches and exhaustiveness checks to work correctly.

Key Changes:

  • Added logic to rebase narrowing operations from alias names back to original match subjects
  • Implemented facet subject merging for proper handling of nested property access patterns
  • Updated type narrowing to properly track and apply constraints across aliased patterns

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
pyrefly/lib/test/pattern_match.rs Added test case demonstrating that match aliases now correctly narrow the original subject variable
pyrefly/lib/test/flow_branching.rs Updated existing test assertions to reflect correct narrowing behavior (e.g., x now narrows to Literal[2] and B instead of broader types)
pyrefly/lib/binding/pattern.rs Modified Pattern::MatchAs handling to rebase alias narrowing operations onto the original subject when they differ
pyrefly/lib/binding/narrow.rs Added rebase_subject, rebase_facet_subject, and merge_facet_subjects functions plus and_for_subject method to support narrowing operation rebasing

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

@github-actions
Copy link

github-actions bot commented Jan 3, 2026

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

@stroxler stroxler self-assigned this Jan 9, 2026
@meta-codesync
Copy link

meta-codesync bot commented Jan 9, 2026

@stroxler has imported this pull request. If you are a Meta employee, you can view this in D90406316.

Copy link
Contributor

@stroxler stroxler left a comment

Choose a reason for hiding this comment

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

This looks reasonable to me, thanks for the PR! I'll aim to get a second review and merge it

Copy link
Contributor

@rchen152 rchen152 left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

Copy link
Contributor

@rchen152 rchen152 left a comment

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync
Copy link

meta-codesync bot commented Jan 10, 2026

@stroxler merged this pull request in 51588a4.

@asukaminato0721 asukaminato0721 deleted the 598 branch January 10, 2026 05:23
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.

Match case on type don't exaust on types

4 participants