-
Notifications
You must be signed in to change notification settings - Fork 314
Preserve tuple match narrows across subsequent cases #3224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c7e6cc2
4948b1e
bd89c04
594c9cc
a9e47f6
44d671a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,11 +213,17 @@ impl<'a> BindingsBuilder<'a> { | |
| Some(idx) => idx, | ||
| Option::None => { | ||
| // More patterns than tuple elements, skip narrowing | ||
| narrow_ops.and_all(self.bind_pattern( | ||
| MatchSubject::None, | ||
| x, | ||
| key_for_subpattern, | ||
| )); | ||
| for (name, (op, range)) in self | ||
| .bind_pattern( | ||
| MatchSubject::None, | ||
| x, | ||
| key_for_subpattern, | ||
| ) | ||
| .0 | ||
| { | ||
| let subject = NarrowingSubject::Name(name); | ||
| narrow_ops.and_for_subject(&subject, op, range); | ||
| } | ||
| continue; | ||
| } | ||
| } | ||
|
|
@@ -238,11 +244,13 @@ impl<'a> BindingsBuilder<'a> { | |
| } | ||
| _ => MatchSubject::None, | ||
| }; | ||
| narrow_ops.and_all(self.bind_pattern( | ||
| subject_for_subpattern, | ||
| x, | ||
| key_for_subpattern, | ||
| )); | ||
| for (name, (op, range)) in self | ||
| .bind_pattern(subject_for_subpattern, x, key_for_subpattern) | ||
| .0 | ||
| { | ||
| let subject = NarrowingSubject::Name(name); | ||
| narrow_ops.and_for_subject(&subject, op, range); | ||
| } | ||
|
Comment on lines
+249
to
+253
|
||
| } | ||
| } | ||
| } | ||
|
|
@@ -599,11 +607,13 @@ impl<'a> BindingsBuilder<'a> { | |
| // shadow outer variables. When there is no narrowing subject | ||
| // (e.g. `match make_color():`), drop all narrows so that alias | ||
| // names don't resolve against unrelated outer variables. | ||
| new_narrow_ops.0.retain(|name, _| { | ||
| match_subject | ||
| .as_single() | ||
| .as_ref() | ||
| .is_some_and(|s| name == s.name()) | ||
| new_narrow_ops.0.retain(|name, _| match &match_subject { | ||
| MatchSubject::Single(subject) => name == subject.name(), | ||
| MatchSubject::Tuple(subjects) => subjects | ||
| .iter() | ||
| .flatten() | ||
| .any(|subject| name == subject.name()), | ||
|
Comment on lines
+611
to
+615
|
||
| MatchSubject::None => false, | ||
| }); | ||
| negated_prev_ops.and_all(new_narrow_ops.negate()); | ||
| self.stmts(body, parent); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In tuple/sequence pattern handling, this change switches from
and_allto per-entryand_for_subject, which avoids Placeholder insertion. That means a multi-element pattern likecase None, Nonewill produce independent narrows for both names, and whenstmt_matchnegates them for fallthrough it becomesa is not NoneANDb is not None(conjunction), which is not the logical negation of(a is None) AND (b is None). This can make later cases likecase _, Noneincorrectly treatbasNeverand can hide type errors. Consider representing fallthrough constraints at the tuple-pattern level (or otherwise avoid per-name negation for multi-name patterns) so the complement is modeled correctly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot apply changes based on this feedback