-
Notifications
You must be signed in to change notification settings - Fork 13.3k
allow deref patterns to participate in exhaustiveness analysis #140106
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
Conversation
Some changes occurred in exhaustiveness checking cc @Nadrieril |
This comment has been minimized.
This comment has been minimized.
PatKind::DerefPattern { subpattern, .. } => { | ||
// NB(deref_patterns): This assumes the deref pattern is matching on a trusted | ||
// `DerefPure` type. If the `Deref` impl isn't trusted, any deref pattern that can | ||
// fail (possibly due to expanding or-patterns inside it) must not influence | ||
// exhaustiveness analysis. |
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.
I'm assuming here that we're not trying to guarantee a single deref per column, but that'd also work if there's a practical way to do it. I'd have to look into how match lowering and or-pattern expansion work to see whether it's possible to at least guarantee it in that case. Of course, it's also consistent (and maybe easier to explain) if they can never participate in exhaustiveness for an untrusted Deref
impl.
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.
Not impossible to implement but not sure I'd want to guarantee that. Also this just cannot work with a hypothetical DerefMove
: for that we necessarily need to call deref
to determine the branch first. So I'm inclined to say "for deref patterns with custom Deref, we require unsafe impl DerefPure
".
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.
nit: "pattern that can fail" wasn't immediately clear to me. I might say "If the Deref
impl isn't trusted, exhaustiveness must take into account that multiple calls to deref
may return different results. Hence multiple deref!
patterns cannot be exhaustive together unless each is exhaustive by itself."
@@ -1119,6 +1132,47 @@ pub fn analyze_match<'p, 'tcx>( | |||
Ok(report) | |||
} | |||
|
|||
fn detect_mixed_deref_pat_ctors<'p, 'tcx>( |
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.
Is there a better home for this anywhere? The other use of PatternColumn
I could find was in lints.rs
, but this isn't really a lint, so it didn't feel right there.
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.
It's looking like maybe this should work for generic PatCx
so if/when r-a supports deref patterns it'll be able to run before match analysis there too. So it definitely shouldn't be in rustc.rs
or lints.rs
. usefulness.rs
doesn't quite feel right for something using PatColumn
, though it probably should be usefulness::compute_match_usefulness
that calls it. pat_column.rs
is only the implementation of PatColumn
so I'm not sure that's right either.
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.
I looked into rehousing this and making it work for generic PatCx
on my local branch. It's a bit awkward, though: in order to avoid doing a pass for every single pattern, it'd still be the responsibility of the frontend to report when the pattern needs to be checked for mixed constructors (i.e. when a deref pattern was lowered), so the API is a bit awkward. Maybe an extra pass for every pattern is inconsequential perf though? I'm not sure what would be best, but I can open a perf experiment if needed. In the mean time, I'll think more about how to handle diagnostics for mixed exhaustiveness, to maybe avoid this check altogether.
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.
Yeah, perf experiment seems the only way to know
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.
As for where to house it, we could rename lints.rs
to checks.rs
and put it there?
rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead. cc @rust-lang/rust-analyzer |
☔ The latest upstream changes (presumably #140415) made this pull request unmergeable. Please resolve the merge conflicts. |
2d27aa2
to
1a76c9d
Compare
Rebased to resolve the test/doctest conflicts. |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
allow deref patterns to participate in exhaustiveness analysis Per [this proposal](https://hackmd.io/4qDDMcvyQ-GDB089IPcHGg#Exhaustiveness), this PR allows deref patterns to participate in exhaustiveness analysis. Currently all deref patterns enforce `DerefPure` bounds on their scrutinees, so this assumes all patterns it's analyzing are well-behaved. This also doesn't support [mixed exhaustiveness](https://hackmd.io/4qDDMcvyQ-GDB089IPcHGg#Mixed-exhaustiveness), and instead emits an error if deref patterns are used together with normal constructors. I think mixed exhaustiveness would be nice to have (especially if we eventually want to support arbitrary `Deref` impls[^1]), but it'd require more work to get reasonable diagnostics[^2]. Tracking issue for deref patterns: rust-lang#87121 r? `@Nadrieril` [^1]: Regardless of whether we support limited exhaustiveness checking for untrusted `Deref` or always require other arms to be exhaustive, I think it'd be useful to allow mixed matching for user-defined smart pointers. And it'd be strange if it worked there but not for `Cow`. [^2]: I think listing out witnesses of non-exhaustiveness can be confusing when they're not necessarily disjoint, and when you only need to cover some of them, so we'd probably want special formatting and/or explanatory subdiagnostics. And if it's implemented similarly to unions, we'd probably also want some way of merging witnesses; the way witnesses for unions can appear duplicated is pretty unfortunate. I'm not sure yet how the diagnostics should look, especially for deeply nested patterns.
⌛ Trying commit 1a76c9d with merge 377ed5e3433b200e14f360ee68b384e0a3f066d8... |
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.
Pretty much flawless, can't find anything I'd change in this implementation. Started a perf run out of habit, not expecting a change. Happy to merge as-is, got some nits.
(0, Cow::Owned(_)) => {} | ||
(0, Cow::Borrowed(_)) => {} | ||
_ => {} | ||
(0, true) => {} |
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.
To make sure someone doesn't move the check inside exhaustiveness checking:
(0, true) => {} | |
(1, true) => {} |
PatKind::DerefPattern { subpattern, .. } => { | ||
// NB(deref_patterns): This assumes the deref pattern is matching on a trusted | ||
// `DerefPure` type. If the `Deref` impl isn't trusted, any deref pattern that can | ||
// fail (possibly due to expanding or-patterns inside it) must not influence | ||
// exhaustiveness analysis. |
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.
nit: "pattern that can fail" wasn't immediately clear to me. I might say "If the Deref
impl isn't trusted, exhaustiveness must take into account that multiple calls to deref
may return different results. Hence multiple deref!
patterns cannot be exhaustive together unless each is exhaustive by itself."
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (377ed5e): comparison URL. Overall result: ❌ regressions - please read the text belowBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary 0.2%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (primary -0.4%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 769.964s -> 770.737s (0.10%) |
1a76c9d
to
2d4e19c
Compare
Applied the comment and test changes; ty! I also added a note about the mixed constructor check. I'll save moving it elsewhere and making it generic for later, since that's a bit more disruptive. (diff) I only have a bit of experience with perf readouts, but that regression might be spurious? I checked the regressions listed there; none of them spend more time in Edit: It does look like a perf improvement had been merged, and that's what this was being compared against. I'll rebase onto |
This does not yet handle the case of mixed deref patterns with normal constructors; it'll ICE in `Constructor::is_covered_by`. That'll be fixed in a later commit.
Without adding proper support for mixed exhaustiveness, mixing deref patterns with normal constructors would either violate `ConstructorSet::split`'s invariant 4 or 7. We'd either be ignoring rows with normal constructors or we'd have problems in unspecialization from non-disjoint constructors. Checking mixed exhaustivenss similarly to how unions are currently checked should work, but the diagnostics for unions are confusing. Since mixing deref patterns with normal constructors is pretty niche (currently it only makes sense for `Cow`), emitting an error lets us avoid committing to supporting mixed exhaustiveness without a good answer for the diagnostics.
rust-analyzer doesn't construct `DerefPattern(_)` constructors, so these shouldn't crash. It looks like this is how slice patterns are implemented too.
2d4e19c
to
09fed2d
Compare
The perf diff is small enough to ignore imo. Let's merge! @bors r+ |
☀️ Test successful - checks-actions |
What is this?This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.Comparing ae3e8c6 (parent) -> 7e552b4 (this PR) Test differencesShow 13 test diffsStage 1
Stage 2
Additionally, 4 doctest diffs were found. These are ignored, as they are noisy. Job group index
Test dashboardRun cargo run --manifest-path src/ci/citool/Cargo.toml -- \
test-dashboard 7e552b46af72df390ed233b58a7f51650515b2a8 --output-dir test-dashboard And then open Job duration changes
How to interpret the job duration changes?Job durations can vary a lot, based on the actual runner instance |
Finished benchmarking commit (7e552b4): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (primary 0.6%, secondary -4.2%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (primary 0.7%, secondary 2.2%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 769.46s -> 769.201s (-0.03%) |
Per this proposal, this PR allows deref patterns to participate in exhaustiveness analysis. Currently all deref patterns enforce
DerefPure
bounds on their scrutinees, so this assumes all patterns it's analyzing are well-behaved. This also doesn't support mixed exhaustiveness, and instead emits an error if deref patterns are used together with normal constructors. I think mixed exhaustiveness would be nice to have (especially if we eventually want to support arbitraryDeref
impls1), but it'd require more work to get reasonable diagnostics2.Tracking issue for deref patterns: #87121
r? @Nadrieril
Footnotes
Regardless of whether we support limited exhaustiveness checking for untrusted
Deref
or always require other arms to be exhaustive, I think it'd be useful to allow mixed matching for user-defined smart pointers. And it'd be strange if it worked there but not forCow
. ↩I think listing out witnesses of non-exhaustiveness can be confusing when they're not necessarily disjoint, and when you only need to cover some of them, so we'd probably want special formatting and/or explanatory subdiagnostics. And if it's implemented similarly to unions, we'd probably also want some way of merging witnesses; the way witnesses for unions can appear duplicated is pretty unfortunate. I'm not sure yet how the diagnostics should look, especially for deeply nested patterns. ↩