Skip to content

Commit be9d90e

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

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

compiler/rustc_hir_typeck/src/mem_categorization.rs

Lines changed: 4 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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!(s) = &mut b else { unreachable!() };
17+
// FIXME(deref_patterns): this version only captured `&b`.
18+
// let deref!(ref mut s) = b else { unreachable!() };
19+
s.push_str("aa");
20+
};
21+
f();
22+
assert_eq!(b.len(), 5);
23+
}

0 commit comments

Comments
 (0)