Repository navigation
fix: report unreachable match case subsumed by earlier sibling class pattern - #5201
bniladridas wants to merge 2 commits into
Conversation
|
This pull request has been imported. If you are a Meta employee, you can view this in D124670346. (Because this pull request was imported automatically, there will not be any future comments.) |
This comment has been minimized.
This comment has been minimized.
6c269ae to
2333cb5
Compare
|
Hi @samwgoldman (Sam Goldman), I've rebased this onto the latest main and re-run the checks ( |
|
Diff from mypy_primer, showing the effect of this PR on open source code: ============================================================
SUMMARY
============================================================
Total: +6 new errors, -0 fixed errors
By preset: +6/-0 (default), +6/-0 (strict)
Projects with changes (2):
jax: +1 -0
pytensor: +5 -0
============================================================
FULL DIFF DETAILS
------------------------------------------------------------
jax (https://github.com/google/jax)
+ ERROR jax/experimental/mosaic/gpu/dialect_lowering.py:1166:5-1167:63: Case pattern can never match: an earlier case already matches all `CopyPartitionedAttr` values [unreachable-match-case]
pytensor (https://github.com/pymc-devs/pytensor)
+ ERROR pytensor/link/numba/dispatch/sparse/variable.py:340:9-341:49: Case pattern can never match: an earlier case already matches all `CSCMatrixType` values [unreachable-match-case]
+ ERROR pytensor/link/numba/dispatch/sparse/variable.py:362:9-363:49: Case pattern can never match: an earlier case already matches all `CSCMatrixType` values [unreachable-match-case]
+ ERROR pytensor/link/numba/dispatch/sparse/variable.py:428:9-429:49: Case pattern can never match: an earlier case already matches all `CSCMatrixType` values [unreachable-match-case]
+ ERROR pytensor/link/numba/dispatch/sparse/variable.py:571:9-584:28: Case pattern can never match: an earlier case already matches all `CSCMatrixType` values [unreachable-match-case]
+ ERROR pytensor/link/numba/dispatch/sparse/variable.py:626:9-649:27: Case pattern can never match: an earlier case already matches all `CSCMatrixType` values [unreachable-match-case] |
…pattern When the match subject is Any or object, narrowing cannot produce Never, so a class pattern already covered by an earlier unguarded case went unreported. Track preceding unguarded case ops at bind time and, at solve time, report when the current case's class is a subset of their union. Signed-off-by: Niladri Das <125604915+bniladridas@users.noreply.github.com>
pattern_class_operand now rejects classes whose metadata has_base_any() is true, preventing false unreachable-match-case diagnostics when a base class is unresolved and both siblings extend Any. Signed-off-by: Niladri Das <125604915+bniladridas@users.noreply.github.com>
2333cb5 to
4d3aa06
Compare
| let class_ty = classes.pop().expect("checked length above"); | ||
| if let Type::ClassDef(cls) = class_ty | ||
| && self.get_class_tparams(&cls).is_none() | ||
| && !self.get_metadata_for_class(&cls).has_base_any() |
There was a problem hiding this comment.
the added guard skips sibling subsumption for classes that extend Any and fixes the unresolved-base false positive.
Summary
When the match subject is
Anyorobject, narrowing cannot produceNever, so a class pattern already covered by an earlier unguarded sibling case went unreported. This tracks preceding unguarded case ops at bind time and reports at solve time when the current case's class is a subset of their union.The subject-based check stays authoritative for precise subjects. The sibling check runs only when subject narrowing is silent (
Any/objectearly return) or fails to produceNever.A later guard skips classes whose base extends
Any, so unresolved base classes (for example numba types) do not produce false positives.Fixes #3338
Test Plan
cargo test -p pyrefly pattern_match: 112 tests pass, including the newtest_match_case_unreachable_subsumed_by_sibling_any_subjectcovering subsumption onAny, subclass-first ordering, and guarded predecessors.cargo test -p pyrefly: 9068 passed, 0 failed (last full run before the rebase; the change is a one-line guard).cargo clippy -p pyrefly --lib: clean (last run before the rebase).python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema --no-extension: passes.unreachable-match-case.