Repository navigation
fix: report unreachable match case after earlier sibling pattern - #5515
Open
bniladridas wants to merge 1 commit into
Open
bniladridas wants to merge 1 commit into
bniladridas wants to merge 1 commit into
Conversation
Narrow by the current case pattern before applying earlier unguarded case negations. This lets an Any subject become its current class first, so subtracting an earlier superclass produces Never and the case is reported unreachable. Keep object subjects on the existing early return because structural patterns can narrow object to Never spuriously. The check is limited to the Any case tracked by meta#3338. Signed-off-by: Niladri Das <125604915+bniladridas@users.noreply.github.com>
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D129509393. (Because this pull request was imported automatically, there will not be any future comments.) |
This was referenced Nov 4, 2026
This branch has not been deployed
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 #3338.
Anywas short-circuited in match-case reachability, socase bool()aftercase int()was never reported. Narrowing by the current class pattern first, then applying earlier unguarded case negations, reuses the existing narrowing and subtraction path:Anybecomesbool, thenboolminusintisNever.This supersedes the closed #5201 approach. It removes the second sibling-subsumption check, the separate class extraction, and the
prev_case_opsvector. The binder passes one earlier negation separately, and reachability applies the current pattern before that negation.Scope
Any.objectkeeps the existing early return because structural patterns such as tuples can narrowobjecttoNeverspuriously.case A(x=1)does not subsumecase B(). Existing reachability also does not report duplicate partial-class patterns on precise subjects, so positive detection is tracked separately in Report unreachable duplicate partial-class match cases #5516.NarrowOpis cloned becauseBinding::Narrowand the reachability metadata own separate copies. No vector or class-extraction clone remains.Test Plan
cargo test -p pyrefly pattern_match --quiet: 113 passed, 0 failed.cargo clippy -p pyrefly --lib -- -D warnings: passed.python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema --no-extension: passed.cargo test -p pyrefly --quiet -- --skip test::cycles::deep_phi_chain_term_expr: 10244 passed, 0 failed, 3 ignored on the rebasedorigin/mainat10226ec1. That test overflows the default 10 MiB debug stack on clean currentorigin/maintoo, so it is unrelated; it passes withPYREFLY_STACK_SIZE=67108864.New regression covers
Anyint then bool, subclass-first order, guarded predecessor, duplicate literal, Or-pattern, object no-report, and partial-class no-false-positive.I am a human, and I fully understand the code I am submitting.