-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Refactor integer range handling in the usefulness algorithm #66326
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
c75685b
Note link between apply/specialize/arity functions
Nadrieril 8f7ba7a
Clarify conditions for exhaustive integer range matching
Nadrieril 3531c52
Factor out range construction in `all_constructors`
Nadrieril 34ad52e
`pat_constructor` does not need `pcx` anymore
Nadrieril 8c1835d
IntRange::from_pat is redundant with pat_constructors
Nadrieril f3752ee
Cleanup `constructor_covered_by_range`
Nadrieril 81db2ee
Special-case range inclusion when the range is integral but non-exhau…
Nadrieril 0a18610
Move range exhaustiveness check to IntRange::intersection
Nadrieril 6de7afd
Prefer IntRange::into_ctor to range_to_ctor
Nadrieril c6c8625
Special-case subtracting from a range if that range is not an IntRange
Nadrieril 75a5088
Avoid converting through Constructor when subtracting ranges
Nadrieril 1909e3f
`Constructor::display` was only needed for displaying `IntRange`
Nadrieril f93a700
Introduce IntRange constructor
Nadrieril 4232816
formatting
Nadrieril 6b8bfef
Add `IntRange::to_pat` and use it instead of custom `display()`
Nadrieril 84784dd
Eagerly convert ranges to IntRange
Nadrieril aadd5e5
Factor out getting the boundaries of an `IntRange`
Nadrieril d1642f1
Inline now-trivial IntRange::from_ctor
Nadrieril 0192124
Make should_treat_range_exhaustively a method
Nadrieril f674f89
Store Const directly in ConstantRange
Nadrieril 3e5aadc
Remove unnecessary data from ConstantValue/ConstantRange
Nadrieril e47d631
Malformed range patterns can't happen thanks to E0030
Nadrieril d31b4c3
Remove fishy condition
Nadrieril c38aad4
Add test for failing `try_eval_bits`
Nadrieril 9165dd0
Apply suggestions from code review
Nadrieril b679e77
Factor out IntRange::is_subrange
Nadrieril addd8a9
Apply suggestions from code review
Nadrieril cde9808
Add regression test
Nadrieril 694a511
Apply suggestions from code review
Nadrieril File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Special-case range inclusion when the range is integral but non-exhau…
…stive
- Loading branch information
commit 81db2ee902566dfe8d1324f7849ea202028f68fd
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -641,6 +641,15 @@ impl<'tcx> Constructor<'tcx> { | |
IntRange::should_treat_range_exhaustively(tcx, ty) | ||
} | ||
|
||
fn is_integral_range(&self) -> bool { | ||
let ty = match self { | ||
ConstantValue(value, _) => value.ty, | ||
ConstantRange(_, _, ty, _, _) => ty, | ||
_ => return false, | ||
}; | ||
IntRange::is_integral(ty) | ||
} | ||
|
||
fn variant_index_for_adt<'a>( | ||
&self, | ||
cx: &MatchCheckCtxt<'a, 'tcx>, | ||
|
@@ -1471,6 +1480,12 @@ impl<'tcx> IntRange<'tcx> { | |
} | ||
} | ||
|
||
fn is_subrange(&self, other: &Self) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels like a useful operation that could be added to the standard library on ranges; cc @dtolnay |
||
let (lo, hi) = (*self.range.start(), *self.range.end()); | ||
let (other_lo, other_hi) = (*other.range.start(), *other.range.end()); | ||
other_lo <= lo && hi <= other_hi | ||
} | ||
|
||
fn suspicious_intersection(&self, other: &Self) -> bool { | ||
// `false` in the following cases: | ||
// 1 ---- // 1 ---------- // 1 ---- // 1 ---- | ||
|
@@ -2300,13 +2315,25 @@ fn specialize_one_pattern<'p, 'a: 'p, 'q: 'p, 'tcx>( | |
IntRange::from_pat(cx.tcx, cx.param_env, pat), | ||
) { | ||
(Some(ctor), Some(pat)) => ctor.intersection(&pat).map(|_| { | ||
// Constructor splitting should ensure that all intersections we encounter | ||
// are actually inclusions. | ||
let (pat_lo, pat_hi) = pat.range.into_inner(); | ||
let (ctor_lo, ctor_hi) = ctor.range.into_inner(); | ||
assert!(pat_lo <= ctor_lo && ctor_hi <= pat_hi); | ||
Nadrieril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PatStack::default() | ||
}), | ||
_ => None, | ||
} | ||
} else if constructor.is_integral_range() { | ||
// If we have an integer range that should not be matched exhaustively, fallback to | ||
// checking for inclusion. | ||
match ( | ||
IntRange::from_ctor(cx.tcx, cx.param_env, constructor), | ||
IntRange::from_pat(cx.tcx, cx.param_env, pat), | ||
) { | ||
(Some(ctor), Some(pat)) if ctor.is_subrange(&pat) => Some(PatStack::default()), | ||
_ => None, | ||
} | ||
} else { | ||
// Fallback for non-ranges and ranges that involve | ||
// floating-point numbers, which are not conveniently handled | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.