Skip to content

Commit 7458b29

Browse files
committed
compute dbm in user's pattern from deref kind and mutability
A slight refactor: we'll always want to pass the mutability in to `push_deref` to determine the default binding mode when a pattern is removed, so this eliminates the redundancy of having both that and the user's dbm as separate arguments.
1 parent 6a7f026 commit 7458b29

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

compiler/rustc_mir_build/src/thir/pattern/migration.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,10 @@ impl<'a, 'tcx> PatMigration<'a, 'tcx> {
228228
/// This should only be called when the pattern type adjustments list `ref_tys` is non-empty.
229229
/// This should be followed by a call to [`PatMigration::leave_ref`] when we leave the pattern.
230230
pub(super) fn visit_implicit_derefs(&mut self, pat: &hir::Pat<'_>, ref_tys: &'a [Ty<'tcx>]) {
231-
// Remember if this changed the default binding mode, in case we want to label it.
232-
let new_real_ref_mutbl = match self.real_default_mode() {
233-
ByRef::Yes(Mutability::Not) => Mutability::Not,
234-
_ => iter_ref_mutbls(pat.span, ref_tys).min().unwrap(),
235-
};
236-
self.push_deref(pat.span, ByRef::Yes(new_real_ref_mutbl), PatDerefKind::Implicit {
237-
ref_tys,
238-
});
231+
let mutbl = iter_ref_mutbls(pat.span, ref_tys)
232+
.min()
233+
.expect("`ref_tys` should have at least one element");
234+
self.push_deref(pat.span, mutbl, PatDerefKind::Implicit { ref_tys });
239235
}
240236

241237
/// Tracks the default binding mode when we're lowering a `&` or `&mut` pattern.
@@ -244,17 +240,25 @@ impl<'a, 'tcx> PatMigration<'a, 'tcx> {
244240
// dereferences. If reference patterns can match the default binding mode alone, we may need to
245241
// check `TypeckResults::skipped_ref_pats` to tell if this pattern corresponds to an implicit
246242
// dereference we've already visited.
247-
pub(super) fn visit_explicit_deref(&mut self, pat_span: Span) {
243+
pub(super) fn visit_explicit_deref(&mut self, pat_span: Span, mutbl: Mutability) {
248244
// If this eats a by-ref default binding mode, label the binding mode.
249245
self.add_default_mode_label_if_needed();
250246
// Set the default binding mode to by-value.
251-
self.push_deref(pat_span, ByRef::No, PatDerefKind::Explicit);
247+
self.push_deref(pat_span, mutbl, PatDerefKind::Explicit);
252248
}
253249

254250
/// Adds a deref to our deref-forest, so that we can track the default binding mode.
255251
// TODO: this is also for propagating binding mode changes when we suggest adding patterns
256-
fn push_deref(&mut self, span: Span, real_default_mode: ByRef, kind: PatDerefKind<'a, 'tcx>) {
252+
fn push_deref(&mut self, span: Span, mutbl: Mutability, kind: PatDerefKind<'a, 'tcx>) {
257253
let parent = self.innermost_deref;
254+
// Get the new default binding mode in the pattern the user wrote.
255+
let real_default_mode = match kind {
256+
PatDerefKind::Implicit { .. } => match self.real_default_mode() {
257+
ByRef::Yes(old_mutbl) => ByRef::Yes(Ord::min(mutbl, old_mutbl)),
258+
ByRef::No => ByRef::Yes(mutbl),
259+
},
260+
PatDerefKind::Explicit => ByRef::No,
261+
};
258262
// If this keeps the default binding mode the same, it shares a mode origin with its
259263
// parent. If it changes the default binding mode, its mode origin is itself.
260264
let default_mode_origin = if real_default_mode == self.real_default_mode() {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
306306
let mutability = if mutable { hir::Mutability::Mut } else { hir::Mutability::Not };
307307
PatKind::DerefPattern { subpattern: self.lower_pattern(subpattern), mutability }
308308
}
309-
hir::PatKind::Ref(subpattern, _) => {
309+
hir::PatKind::Ref(subpattern, mutbl) => {
310310
// Track the default binding mode for the Rust 2024 migration suggestion.
311311
if let Some(s) = &mut self.rust_2024_migration {
312-
s.visit_explicit_deref(pat.span);
312+
s.visit_explicit_deref(pat.span, mutbl);
313313
}
314314
let subpattern = self.lower_pattern(subpattern);
315315
if let Some(s) = &mut self.rust_2024_migration {

0 commit comments

Comments
 (0)