-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in match checking cc @Nadrieril |
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.
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.
compiler/rustc_hir_typeck/src/pat.rs
Outdated
// 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)), |
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 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.
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.
+1 on making this a debug assert
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.
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.
This comment has been minimized.
This comment has been minimized.
be33911
to
dc134ca
Compare
This comment has been minimized.
This comment has been minimized.
dc134ca
to
6a5b56a
Compare
6a5b56a
to
0cbc959
Compare
Rebased to resolve a conflict: as of #138492, the Footnotes
|
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.
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.
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.
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.
0cbc959
to
a006927
Compare
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
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 As for 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 |
a006927
to
d412694
Compare
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 like this iteration of the docs, just one nit..
d412694
to
adefa58
Compare
☔ The latest upstream changes (presumably #139622) made this pull request unmergeable. Please resolve the merge conflicts. |
adefa58
to
9dc6231
Compare
I cleaned up the history a bit while rebasing. The change to make the |
This allows us to better distinguish builtin and overloaded implicit dereferences.
9dc6231
to
86ee4ef
Compare
let implicit_deref_mutbls = adjustments.iter().map(|adjust| { | ||
let &ty::Ref(_, _, mutbl) = adjust.source.kind() else { |
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 a future PR, we could store the mutability here, which would remove the need for matching on the type here and in clippy.
&& !adjustments.is_empty() | ||
&& adjustments.iter().any(|adjust| adjust.source.is_ref()) |
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: I would prefer to match on the PatAdjust
here.
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.
done here. that's definitely better with PatAdjust::BuiltinDeref
only being for ty::Ref(..)
, yeah
compiler/rustc_hir_typeck/src/pat.rs
Outdated
/// 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>> }, |
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: AdtDef
has too much info, I think this should just be the adt's DefId
.
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.
done here
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.
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.
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.
See the previous two commits.
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.
fc51373
to
3b91b7a
Compare
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