Skip to content

fix support check more enum exhaustive #3294#3297

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

fix support check more enum exhaustive #3294#3297
asukaminato0721 wants to merge 2 commits intofacebook:mainfrom
asukaminato0721:3294

Conversation

@asukaminato0721
Copy link
Copy Markdown
Contributor

Summary

Fixes #3294

Test Plan

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

This comment has been minimized.

@github-actions github-actions Bot added size/s and removed size/s labels May 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 3, 2026

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 May 3, 2026 20:15
Copilot AI review requested due to automatic review settings May 3, 2026 20:15
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 extends Pyrefly’s match-exhaustiveness checking so that certain non-enum builtin subjects (e.g., int, str, list[int]) can emit a “Match on T is not exhaustive” diagnostic, addressing issue #3294.

Changes:

  • Added a regression test ensuring non-exhaustive matches on common builtins produce NonExhaustiveMatch errors.
  • Threaded a new include_open_builtins flag through binding → solver to enable exhaustiveness checking for selected builtin types.
  • Updated the exhaustiveness eligibility logic to optionally include several “open domain” builtins.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pyrefly/lib/test/pattern_match.rs Adds regression coverage for non-exhaustive matches on builtins (int, str, list[int]).
pyrefly/lib/binding/pattern.rs Computes and passes include_open_builtins into the match exhaustiveness expectation.
pyrefly/lib/binding/binding.rs Extends BindingExpect::MatchExhaustiveness payload with include_open_builtins.
pyrefly/lib/alt/solve.rs Propagates include_open_builtins to check_match_exhaustiveness.
pyrefly/lib/alt/narrow.rs Expands should_check_exhaustiveness to optionally include selected builtin classes.

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

Comment on lines +532 to 533
let include_open_builtins = x.cases.len() > 1;
for case in x.cases {
Comment on lines 1094 to 1105
/// A match statement that may be non-exhaustive at runtime.
/// Due to gaps in our type algebra, we only check exhaustiveness for enums & unions
/// of enum literals.
/// Since this makes use of narrowing, not every match subject will be
/// checked for exhaustiveness, only variables and chained subscripts/attributes of variables
MatchExhaustiveness {
subject_idx: Idx<Key>,
narrowing_subject: NarrowingSubject,
narrow_ops_for_fall_through: (Box<NarrowOp>, TextRange),
subject_range: TextRange,
include_open_builtins: bool,
},
Comment thread pyrefly/lib/alt/narrow.rs
Comment on lines 1831 to +1846
Type::ClassType(cls) => {
// Final classes can't have subclasses, so they are exhaustible, with the exception
// of Flag enums, whose members can be combined into new members via bitwise ops
!self.is_flag_enum(cls) && self.is_final(cls.class_object())
// bool is effectively Literal[True] | Literal[False]
|| cls.is_builtin("bool")
|| include_open_builtins
&& (cls.is_builtin("bytearray")
|| cls.is_builtin("bytes")
|| cls.is_builtin("dict")
|| cls.is_builtin("float")
|| cls.is_builtin("frozenset")
|| cls.is_builtin("int")
|| cls.is_builtin("list")
|| cls.is_builtin("set")
|| cls.is_builtin("str")
|| cls.is_builtin("tuple"))
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.

support check more enum exhaustive

2 participants