Skip to content

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Apr 21, 2025

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 arbitrary Deref impls1), but it'd require more work to get reasonable diagnostics2.

Tracking issue for deref patterns: #87121

r? @Nadrieril

Footnotes

  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.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 21, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 21, 2025

Some changes occurred in exhaustiveness checking

cc @Nadrieril

@rust-log-analyzer

This comment has been minimized.

Comment on lines +497 to +501
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.
Copy link
Contributor Author

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.

Copy link
Member

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".

Copy link
Member

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>(
Copy link
Contributor Author

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.

Copy link
Contributor Author

@dianne dianne Apr 21, 2025

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.

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Member

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?

@rustbot
Copy link
Collaborator

rustbot commented Apr 21, 2025

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

@bors
Copy link
Collaborator

bors commented Apr 29, 2025

☔ The latest upstream changes (presumably #140415) made this pull request unmergeable. Please resolve the merge conflicts.

dianne added 4 commits April 30, 2025 17:34
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.
@dianne dianne force-pushed the deref-pat-usefulness branch from 2d27aa2 to 1a76c9d Compare May 1, 2025 00:55
@dianne
Copy link
Contributor Author

dianne commented May 1, 2025

Rebased to resolve the test/doctest conflicts.

@Nadrieril
Copy link
Member

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 6, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request May 6, 2025
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.
@bors
Copy link
Collaborator

bors commented May 6, 2025

⌛ Trying commit 1a76c9d with merge 377ed5e3433b200e14f360ee68b384e0a3f066d8...

Copy link
Member

@Nadrieril Nadrieril left a 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) => {}
Copy link
Member

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:

Suggested change
(0, true) => {}
(1, true) => {}

Comment on lines +497 to +501
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.
Copy link
Member

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."

@Nadrieril Nadrieril mentioned this pull request May 6, 2025
6 tasks
@bors
Copy link
Collaborator

bors commented May 6, 2025

☀️ Try build successful - checks-actions
Build commit: 377ed5e (377ed5e3433b200e14f360ee68b384e0a3f066d8)

@rust-timer
Copy link
Collaborator

Queued 377ed5e with parent 1a95cc6, future comparison URL.
There is currently 1 preceding artifact in the queue.
It will probably take at least ~1.8 hours until the benchmark run finishes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-perf Status: Waiting on a perf run to be completed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants