Skip to content

deref patterns: implement implicit deref patterns #138528

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 13 commits into
base: master
Choose a base branch
from

Conversation

dianne
Copy link
Contributor

@dianne dianne commented Mar 15, 2025

This implements implicit deref patterns (per https://hackmd.io/4qDDMcvyQ-GDB089IPcHGg#Implicit-deref-patterns) and adds tests and an unstable book chapter.

Best reviewed commit-by-commit. Overall there's a lot of additions, but a lot of that is tests, documentation, and simple(?) refactoring.

Tracking issue: #87121

r? @Nadrieril

@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 Mar 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 15, 2025

Some changes occurred in match checking

cc @Nadrieril

Copy link
Contributor Author

@dianne dianne Mar 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implicit and explicit deref patterns report different expected types when matching on a String since string literal patterns are AdjustMode::Pass (meaning the String doesn't get peeled either). As a fun bonus, I think that means this is compatible with the string_deref_patterns feature, if we don't end up going with a more general solution.

Comment on lines 571 to 692
// If an inline const pattern has a library-defined pointer-like type and
// `deref_patterns` is enabled, don't implicitly add deref patterns for its ADT.
// Nothing in the library that implements `DerefPure` supports structural equality,
// but we don't check that until `const_to_pat` in THIR construction. By avoiding
// type errors here, we can get a more meaningful error there.
ty::Adt(adt, _) => AdjustMode::peel_until_adt(Some(*adt)),
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 think this'll be dead if #138492 lands: no other expression patterns can be ty::Adt(..), right? I don't love leaving untested code in, so maybe this case should be removed (and maybe replaced with a debug assert?) if so. It's not too catastrophic if that assumption ends up being wrong: it'd lead to confusing diagnostics for a relatively niche error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on making this a debug assert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here at the bottom of the pat.rs diff (I can't seem to link to an individual line). Ideally I'd like if the way expression patterns were handled was a bit more consistent, but that'd be a job for a separate PR.

@rust-log-analyzer

This comment has been minimized.

@dianne dianne force-pushed the implicit-deref-patterns branch from be33911 to dc134ca Compare March 15, 2025 04:34
@rust-log-analyzer

This comment has been minimized.

@dianne dianne force-pushed the implicit-deref-patterns branch from dc134ca to 6a5b56a Compare March 15, 2025 06:07
@dianne dianne force-pushed the implicit-deref-patterns branch from 6a5b56a to 0cbc959 Compare April 3, 2025 23:06
@dianne
Copy link
Contributor Author

dianne commented Apr 3, 2025

Rebased to resolve a conflict: as of #138492, the inline_const_pat feature gate has been removed, so I've removed1 a test for the interaction between implicit deref pattern typing and that feature. The implementation of inline const block patterns has been left intact though, so for now I've left in the inline-const-specific case in typeck that the test was for.

Footnotes

  1. I added a // FIXME(inline_const_pat) comment to the test file in its place, in case inline_const_pat is eventually reinstated.

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.

About the middle 4 commits (the ones that set up ResolvedPat): I think we can do simpler. To start with, #139449 should make peeling simpler overall, and then I'd prefer an enum over dyn Fn callbacks (cf Zulip DMs).

About the rest of the commits, LGTM and I left some comments.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 6, 2025
Copy link
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for pushing this feature! I'm really looking forward to using this. I can't really comment on the implementation but have some thoughts about the unstable book entry.

@dianne dianne force-pushed the implicit-deref-patterns branch from 0cbc959 to a006927 Compare April 8, 2025 09:58
@rustbot
Copy link
Collaborator

rustbot commented Apr 8, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@dianne
Copy link
Contributor Author

dianne commented Apr 8, 2025

Ty for the reviews! I've pushed an update to hopefully address them. Most of the changes I've added as new commits for ease of comparison, but since the ResolvedPat refactoring was kind of severe, I figured it'd be cleanest to edit the commit history so that it can be compared more easily with the master branch. In case it helps with the review, the changes between the two histories is here.

As for ResolvedPat and PeelKind, I'm still not totally happy with them, but I've been struggling to come up with anything simpler.

This isn't ready for merging, since I haven't rebased on top of #139449 yet.

Edit: The clippy change is in 64c7917, which changes the pat_adjustments table in TypeckResults to contain more information.

@dianne dianne force-pushed the implicit-deref-patterns branch from a006927 to d412694 Compare April 9, 2025 08:04
Copy link
Contributor

@mejrs mejrs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this iteration of the docs, just one nit..

@dianne dianne force-pushed the implicit-deref-patterns branch from d412694 to adefa58 Compare April 9, 2025 22:50
@bors
Copy link
Collaborator

bors commented Apr 10, 2025

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

@dianne dianne force-pushed the implicit-deref-patterns branch from adefa58 to 9dc6231 Compare April 11, 2025 02:28
@dianne
Copy link
Contributor Author

dianne commented Apr 11, 2025

I cleaned up the history a bit while rebasing. The change to make the pat_adjustments table contain the kind of each implicit deref is now the first commit, so everything else can properly depend on it. @rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 11, 2025
This allows us to better distinguish builtin and overloaded implicit
dereferences.
@dianne dianne force-pushed the implicit-deref-patterns branch from 9dc6231 to 86ee4ef Compare April 13, 2025 00:49
Comment on lines 98 to 99
let implicit_deref_mutbls = adjustments.iter().map(|adjust| {
let &ty::Ref(_, _, mutbl) = adjust.source.kind() else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future PR, we could store the mutability here, which would remove the need for matching on the type here and in clippy.

Comment on lines 73 to 75
&& !adjustments.is_empty()
&& adjustments.iter().any(|adjust| adjust.source.is_ref())
Copy link
Member

@Nadrieril Nadrieril Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would prefer to match on the PatAdjust here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here. that's definitely better with PatAdjust::BuiltinDeref only being for ty::Ref(..), yeah

/// Only peel reference types. This is used for explicit `deref!(_)` patterns, which dereference
/// any number of `&`/`&mut` references, plus a single smart pointer.
ExplicitDerefPat,
/// Implicitly peel any number of references, and if `deref_patterns` is enabled, smart pointer
/// ADTs. In order to peel only as much as necessary for the pattern to match, the `until_adt`
/// field contains the ADT def that the pattern is a constructor for, if applicable, so that we
/// don't peel it. See [`ResolvedPat`] for more information.
// TODO: add `ResolvedPat` and `until_adt`.
Implicit,
Implicit { until_adt: Option<AdtDef<'tcx>> },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: AdtDef has too much info, I think this should just be the adt's DefId.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here

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.

Looking gooood! I'm very happy with this PR now. I left a few nits that aren't too important, except I agree with your proposal here to check if there can be an expression pattern without inline_const_pats. I'm in favor of doing it in this PR so we don't forget.

dianne and others added 12 commits April 16, 2025 14:42
Since this uses `pat_adjustments`, I've also tweaked the documentation
to mention implicit deref patterns and made sure the pattern migration
diagnostic logic accounts for it. I'll adjust `ExprUseVisitor` in a
later commit and add some tests there for closure capture inference.
See the doc comment on `ResolvedPat` for more information. This and the
next couple commits split resolution apart from checking for path,
struct, and tuple struct patterns, in order to find the pattern's type
before peeling the scrutinee. This helps us avoid peeling the scrutinee
when the pattern could match it.

The reason this handles errors from resolution after peeling is for
struct and tuple struct patterns: we check their subpatterns even when
they fail to resolve, to potentially catch more resolution errors. By
doing this after peeling, we're able to use the updated `PatInfo`. I
don't know if there's currently any observable difference from using the
outdated `PatInfo`, but it could potentially be a source of subtle
diagnostic bugs in the future, so I'm opting to peel first.
See the previous commit for details. This doesn't yet extract the struct
pat's type's ADT def before peeling, but it should now be possible.
This is the use for the previous commits' refactors; see the messages
there for more information.
Implicit deref patterns allow previously ill-typed programs. Make sure
they're still ill-typed without the feature gate. I've thrown in a test
for `deref!(_)` too, though it seems it refers to `deref_patterns` as a
library feature.
- Clarifies the uses of implicit and explicit deref patterns
- Includes motivating examples for both syntaxes
- Shows the interaction with match ergonomics for reference types
- Cross-links with `string_deref_patterns` and `box_patterns`

The examples are contrived, but hopefully the intent comes across.
@dianne dianne force-pushed the implicit-deref-patterns branch from fc51373 to 3b91b7a Compare April 16, 2025 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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