Skip to content

Commit 1d737e7

Browse files
committed
Track mutability of deref patterns
1 parent d192a85 commit 1d737e7

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
@@ -645,7 +645,7 @@ impl<'tcx> Pat<'tcx> {
645645
AscribeUserType { subpattern, .. }
646646
| Binding { subpattern: Some(subpattern), .. }
647647
| Deref { subpattern }
648-
| DerefPattern { subpattern }
648+
| DerefPattern { subpattern, .. }
649649
| InlineConstant { subpattern, .. } => subpattern.walk_(it),
650650
Leaf { subpatterns } | Variant { subpatterns, .. } => {
651651
subpatterns.iter().for_each(|field| field.pattern.walk_(it))
@@ -763,6 +763,7 @@ pub enum PatKind<'tcx> {
763763
/// Deref pattern, written `box P` for now.
764764
DerefPattern {
765765
subpattern: Box<Pat<'tcx>>,
766+
mutability: hir::Mutability,
766767
},
767768

768769
/// One of the following:
@@ -1167,7 +1168,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> {
11671168
}
11681169
write!(f, "{subpattern}")
11691170
}
1170-
PatKind::DerefPattern { ref subpattern } => {
1171+
PatKind::DerefPattern { ref subpattern, .. } => {
11711172
write!(f, "deref!({subpattern})")
11721173
}
11731174
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)