Skip to content

Commit b55afe4

Browse files
committed
Address closure-related review
1 parent 377e095 commit b55afe4

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,15 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
750750
}
751751
}
752752
}
753+
} else if let PatKind::Deref(subpattern) = pat.kind {
754+
// A deref pattern is a bit special: the binding mode of its inner bindings
755+
// determines whether to borrow *at the level of the deref pattern* rather than
756+
// borrowing the bound place (since that inner place is inside the temporary that
757+
// stores the result of calling `deref()`/`deref_mut()` so can't be captured).
758+
let mutable = mc.typeck_results.pat_has_ref_mut_binding(subpattern);
759+
let mutability = if mutable { hir::Mutability::Mut } else { hir::Mutability::Not };
760+
let bk = ty::BorrowKind::from_mutbl(mutability);
761+
delegate.borrow(place, discr_place.hir_id, bk);
753762
}
754763
}));
755764
}

compiler/rustc_hir_typeck/src/mem_categorization.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,11 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
719719
self.cat_pattern_(subplace, subpat, op)?;
720720
}
721721
PatKind::Deref(subpat) => {
722+
let mutable = self.typeck_results.pat_has_ref_mut_binding(subpat);
723+
let mutability = if mutable { hir::Mutability::Mut } else { hir::Mutability::Not };
724+
let re_erased = self.tcx().lifetimes.re_erased;
722725
let ty = self.pat_ty_adjusted(subpat)?;
726+
let ty = Ty::new_ref(self.tcx(), re_erased, ty, mutability);
723727
// A deref pattern generates a temporary.
724728
let place = self.cat_rvalue(pat.hir_id, ty);
725729
self.cat_pattern_(place, subpat, op)?;

compiler/rustc_middle/src/ty/typeck_results.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ impl<'tcx> TypeckResults<'tcx> {
451451
/// This is computed from the typeck results since we want to make
452452
/// sure to apply any match-ergonomics adjustments, which we cannot
453453
/// determine from the HIR alone.
454-
pub fn pat_has_ref_mut_binding(&self, pat: &'tcx hir::Pat<'tcx>) -> bool {
454+
pub fn pat_has_ref_mut_binding(&self, pat: &hir::Pat<'_>) -> bool {
455455
let mut has_ref_mut = false;
456456
pat.walk(|pat| {
457457
if let hir::PatKind::Binding(_, id, _, _) = pat.kind
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ run-pass
2+
#![feature(deref_patterns)]
3+
#![allow(incomplete_features)]
4+
5+
fn main() {
6+
let b = Box::new("aaa".to_string());
7+
let f = || {
8+
let deref!(ref s) = b else { unreachable!() };
9+
assert_eq!(s.len(), 3);
10+
};
11+
assert_eq!(b.len(), 3);
12+
f();
13+
14+
let mut b = Box::new("aaa".to_string());
15+
let mut f = || {
16+
let deref!(ref mut s) = b else { unreachable!() };
17+
s.push_str("aa");
18+
};
19+
f();
20+
assert_eq!(b.len(), 5);
21+
}

0 commit comments

Comments
 (0)