Track mutability of deref patterns

This commit is contained in:
Nadrieril 2024-04-04 00:25:16 +02:00
parent 1dabacd059
commit 5c4909b8e1
6 changed files with 10 additions and 7 deletions

View File

@ -642,7 +642,7 @@ fn walk_(&self, it: &mut impl FnMut(&Pat<'tcx>) -> bool) {
AscribeUserType { subpattern, .. }
| Binding { subpattern: Some(subpattern), .. }
| Deref { subpattern }
| DerefPattern { subpattern }
| DerefPattern { subpattern, .. }
| InlineConstant { subpattern, .. } => subpattern.walk_(it),
Leaf { subpatterns } | Variant { subpatterns, .. } => {
subpatterns.iter().for_each(|field| field.pattern.walk_(it))
@ -760,6 +760,7 @@ pub enum PatKind<'tcx> {
/// Deref pattern, written `box P` for now.
DerefPattern {
subpattern: Box<Pat<'tcx>>,
mutability: hir::Mutability,
},
/// One of the following:
@ -1163,7 +1164,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
}
write!(f, "{subpattern}")
}
PatKind::DerefPattern { ref subpattern } => {
PatKind::DerefPattern { ref subpattern, .. } => {
write!(f, "deref!({subpattern})")
}
PatKind::Constant { value } => write!(f, "{value}"),

View File

@ -229,7 +229,7 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
match &pat.kind {
AscribeUserType { subpattern, ascription: _ }
| Deref { subpattern }
| DerefPattern { subpattern }
| DerefPattern { subpattern, .. }
| Binding { subpattern: Some(subpattern), .. } => visitor.visit_pat(subpattern),
Binding { .. } | Wild | Never | Error(_) => {}
Variant { subpatterns, adt_def: _, args: _, variant_index: _ } | Leaf { subpatterns } => {

View File

@ -938,7 +938,7 @@ pub(super) fn visit_primary_bindings(
self.visit_primary_bindings(subpattern, pattern_user_ty.deref(), f);
}
PatKind::DerefPattern { ref subpattern } => {
PatKind::DerefPattern { ref subpattern, .. } => {
self.visit_primary_bindings(subpattern, UserTypeProjections::none(), f);
}

View File

@ -249,7 +249,7 @@ pub(in crate::build) fn new(
default_irrefutable()
}
PatKind::DerefPattern { ref subpattern } => {
PatKind::DerefPattern { ref subpattern, .. } => {
// Create a new temporary for each deref pattern.
// FIXME(deref_patterns): dedup temporaries to avoid multiple `deref()` calls?
let temp = cx.temp(

View File

@ -264,7 +264,9 @@ fn lower_pattern_unadjusted(&mut self, pat: &'tcx hir::Pat<'tcx>) -> Box<Pat<'tc
}
hir::PatKind::Deref(subpattern) => {
PatKind::DerefPattern { subpattern: self.lower_pattern(subpattern) }
let mutable = self.typeck_results.pat_has_ref_mut_binding(subpattern);
let mutability = if mutable { hir::Mutability::Mut } else { hir::Mutability::Not };
PatKind::DerefPattern { subpattern: self.lower_pattern(subpattern), mutability }
}
hir::PatKind::Ref(subpattern, _) | hir::PatKind::Box(subpattern) => {
PatKind::Deref { subpattern: self.lower_pattern(subpattern) }

View File

@ -688,7 +688,7 @@ fn print_pat_kind(&mut self, pat_kind: &PatKind<'tcx>, depth_lvl: usize) {
self.print_pat(subpattern, depth_lvl + 2);
print_indented!(self, "}", depth_lvl + 1);
}
PatKind::DerefPattern { subpattern } => {
PatKind::DerefPattern { subpattern, .. } => {
print_indented!(self, "DerefPattern { ", depth_lvl + 1);
print_indented!(self, "subpattern:", depth_lvl + 2);
self.print_pat(subpattern, depth_lvl + 2);