Skip to content

Commit af75014

Browse files
"No ref mut behind &" on all editions
1 parent 0746577 commit af75014

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
406406
pat: &'tcx Pat<'tcx>,
407407
expected: Ty<'tcx>,
408408
mut def_br: ByRef,
409-
mut max_ref_mutability: MutblCap,
409+
mut max_ref_mutbl: MutblCap,
410410
) -> (Ty<'tcx>, ByRef, MutblCap) {
411411
let mut expected = self.try_structurally_resolve_type(pat.span, expected);
412412
// Peel off as many `&` or `&mut` from the scrutinee type as possible. For example,
@@ -438,10 +438,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
438438
});
439439
}
440440

441-
if pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024 {
442-
def_br = def_br.cap_ref_mutability(max_ref_mutability.as_mutbl());
441+
if self.tcx.features().ref_pat_eat_one_layer_2024 {
442+
def_br = def_br.cap_ref_mutability(max_ref_mutbl.as_mutbl());
443443
if def_br == ByRef::Yes(Mutability::Not) {
444-
max_ref_mutability = MutblCap::Not;
444+
max_ref_mutbl = MutblCap::Not;
445445
}
446446
}
447447

@@ -453,7 +453,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
453453
.insert(pat.hir_id, pat_adjustments);
454454
}
455455

456-
(expected, def_br, max_ref_mutability)
456+
(expected, def_br, max_ref_mutbl)
457457
}
458458

459459
fn check_pat_lit(
@@ -2130,18 +2130,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21302130
mut expected: Ty<'tcx>,
21312131
mut pat_info: PatInfo<'tcx, '_>,
21322132
) -> Ty<'tcx> {
2133-
let new_match_ergonomics =
2134-
pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024;
2133+
let no_ref_mut_behind_and = self.tcx.features().ref_pat_eat_one_layer_2024;
2134+
let new_match_ergonomics = pat.span.at_least_rust_2024() && no_ref_mut_behind_and;
21352135

2136-
if new_match_ergonomics {
2137-
let pat_prefix_span =
2138-
inner.span.find_ancestor_inside(pat.span).map(|end| pat.span.until(end));
2136+
let pat_prefix_span =
2137+
inner.span.find_ancestor_inside(pat.span).map(|end| pat.span.until(end));
21392138

2139+
if no_ref_mut_behind_and {
21402140
if pat_mutbl == Mutability::Not {
21412141
// Prevent the inner pattern from binding with `ref mut`.
21422142
pat_info.max_ref_mutbl = pat_info.max_ref_mutbl.cap_to_weakly_not(pat_prefix_span);
21432143
}
2144+
} else {
2145+
pat_info.max_ref_mutbl = MutblCap::Mut;
2146+
}
21442147

2148+
if new_match_ergonomics {
21452149
if let ByRef::Yes(inh_mut) = pat_info.binding_mode {
21462150
// ref pattern consumes inherited reference
21472151

@@ -2179,8 +2183,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21792183
.rust_2024_migration_desugared_pats_mut()
21802184
.insert(pat_info.top_info.hir_id);
21812185
}
2182-
2183-
pat_info.max_ref_mutbl = MutblCap::Mut;
21842186
}
21852187

21862188
let tcx = self.tcx;
@@ -2195,16 +2197,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21952197
// the bad interactions of the given hack detailed in (note_1).
21962198
debug!("check_pat_ref: expected={:?}", expected);
21972199
match *expected.kind() {
2198-
ty::Ref(_, r_ty, r_mutbl) if r_mutbl >= pat_mutbl && new_match_ergonomics => {
2199-
if r_mutbl == Mutability::Not {
2200+
ty::Ref(_, r_ty, r_mutbl)
2201+
if (new_match_ergonomics && r_mutbl >= pat_mutbl)
2202+
|| r_mutbl == pat_mutbl =>
2203+
{
2204+
if no_ref_mut_behind_and && r_mutbl == Mutability::Not {
22002205
pat_info.max_ref_mutbl = MutblCap::Not;
22012206
}
22022207

22032208
(expected, r_ty)
22042209
}
22052210

2206-
ty::Ref(_, r_ty, r_mutbl) if r_mutbl == pat_mutbl => (expected, r_ty),
2207-
22082211
_ => {
22092212
let inner_ty = self.next_ty_var(inner.span);
22102213
let ref_ty = self.new_ref_ty(pat.span, pat_mutbl, inner_ty);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ edition: 2021
2+
//@ run-pass
3+
#![allow(incomplete_features)]
4+
#![feature(ref_pat_eat_one_layer_2024)]
5+
6+
fn main() {
7+
let &[[x]] = &[&mut [42]];
8+
let _: &i32 = x;
9+
}

0 commit comments

Comments
 (0)