Skip to content

Commit 890ce91

Browse files
committed
Address closure-related review
1 parent 2e22b35 commit 890ce91

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

+9
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

+4
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,11 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
723723
self.cat_pattern_(subplace, subpat, op)?;
724724
}
725725
PatKind::Deref(subpat) => {
726+
let mutable = self.typeck_results.pat_has_ref_mut_binding(subpat);
727+
let mutability = if mutable { hir::Mutability::Mut } else { hir::Mutability::Not };
728+
let re_erased = self.tcx().lifetimes.re_erased;
726729
let ty = self.pat_ty_adjusted(subpat)?;
730+
let ty = Ty::new_ref(self.tcx(), re_erased, ty, mutability);
727731
// A deref pattern generates a temporary.
728732
let place = self.cat_rvalue(pat.hir_id, ty);
729733
self.cat_pattern_(place, subpat, op)?;

compiler/rustc_middle/src/ty/typeck_results.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<'tcx> TypeckResults<'tcx> {
443443
/// This is computed from the typeck results since we want to make
444444
/// sure to apply any match-ergonomics adjustments, which we cannot
445445
/// determine from the HIR alone.
446-
pub fn pat_has_ref_mut_binding(&self, pat: &'tcx hir::Pat<'tcx>) -> bool {
446+
pub fn pat_has_ref_mut_binding(&self, pat: &hir::Pat<'_>) -> bool {
447447
let mut has_ref_mut = false;
448448
pat.walk(|pat| {
449449
if let hir::PatKind::Binding(_, id, _, _) = pat.kind
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)