Skip to content

Commit 7b553ea

Browse files
committed
Track mutability of deref patterns
1 parent 109d0e6 commit 7b553ea

File tree

6 files changed

+10
-7
lines changed

6 files changed

+10
-7
lines changed

compiler/rustc_middle/src/thir.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl<'tcx> Pat<'tcx> {
644644
AscribeUserType { subpattern, .. }
645645
| Binding { subpattern: Some(subpattern), .. }
646646
| Deref { subpattern }
647-
| DerefPattern { subpattern }
647+
| DerefPattern { subpattern, .. }
648648
| InlineConstant { subpattern, .. } => subpattern.walk_(it),
649649
Leaf { subpatterns } | Variant { subpatterns, .. } => {
650650
subpatterns.iter().for_each(|field| field.pattern.walk_(it))
@@ -762,6 +762,7 @@ pub enum PatKind<'tcx> {
762762
/// Deref pattern, written `box P` for now.
763763
DerefPattern {
764764
subpattern: Box<Pat<'tcx>>,
765+
mutability: hir::Mutability,
765766
},
766767

767768
/// One of the following:
@@ -1165,7 +1166,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
11651166
}
11661167
write!(f, "{subpattern}")
11671168
}
1168-
PatKind::DerefPattern { ref subpattern } => {
1169+
PatKind::DerefPattern { ref subpattern, .. } => {
11691170
write!(f, "deref!({subpattern})")
11701171
}
11711172
PatKind::Constant { value } => write!(f, "{value}"),

compiler/rustc_middle/src/thir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
229229
match &pat.kind {
230230
AscribeUserType { subpattern, ascription: _ }
231231
| Deref { subpattern }
232-
| DerefPattern { subpattern }
232+
| DerefPattern { subpattern, .. }
233233
| Binding { subpattern: Some(subpattern), .. } => visitor.visit_pat(subpattern),
234234
Binding { .. } | Wild | Never | Error(_) => {}
235235
Variant { subpatterns, adt_def: _, args: _, variant_index: _ } | Leaf { subpatterns } => {

compiler/rustc_mir_build/src/build/matches/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
943943
self.visit_primary_bindings(subpattern, pattern_user_ty.deref(), f);
944944
}
945945

946-
PatKind::DerefPattern { ref subpattern } => {
946+
PatKind::DerefPattern { ref subpattern, .. } => {
947947
self.visit_primary_bindings(subpattern, UserTypeProjections::none(), f);
948948
}
949949

compiler/rustc_mir_build/src/build/matches/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
250250
default_irrefutable()
251251
}
252252

253-
PatKind::DerefPattern { ref subpattern } => {
253+
PatKind::DerefPattern { ref subpattern, .. } => {
254254
// Create a new temporary for each deref pattern.
255255
// FIXME(deref_patterns): dedup temporaries to avoid multiple `deref()` calls?
256256
let temp = cx.temp(

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
257257
}
258258

259259
hir::PatKind::Deref(subpattern) => {
260-
PatKind::DerefPattern { subpattern: self.lower_pattern(subpattern) }
260+
let mutable = self.typeck_results.pat_has_ref_mut_binding(subpattern);
261+
let mutability = if mutable { hir::Mutability::Mut } else { hir::Mutability::Not };
262+
PatKind::DerefPattern { subpattern: self.lower_pattern(subpattern), mutability }
261263
}
262264
hir::PatKind::Ref(subpattern, _) | hir::PatKind::Box(subpattern) => {
263265
PatKind::Deref { subpattern: self.lower_pattern(subpattern) }

compiler/rustc_mir_build/src/thir/print.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
687687
self.print_pat(subpattern, depth_lvl + 2);
688688
print_indented!(self, "}", depth_lvl + 1);
689689
}
690-
PatKind::DerefPattern { subpattern } => {
690+
PatKind::DerefPattern { subpattern, .. } => {
691691
print_indented!(self, "DerefPattern { ", depth_lvl + 1);
692692
print_indented!(self, "subpattern:", depth_lvl + 2);
693693
self.print_pat(subpattern, depth_lvl + 2);

0 commit comments

Comments
 (0)