Skip to content

Commit e36d1cd

Browse files
Jules-Bertholetlcnr
authored andcommitted
"No ref mut behind &" on all editions
1 parent d4cdd73 commit e36d1cd

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(
@@ -2131,18 +2131,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21312131
mut expected: Ty<'tcx>,
21322132
mut pat_info: PatInfo<'tcx, '_>,
21332133
) -> Ty<'tcx> {
2134-
let new_match_ergonomics =
2135-
pat.span.at_least_rust_2024() && self.tcx.features().ref_pat_eat_one_layer_2024;
2134+
let no_ref_mut_behind_and = self.tcx.features().ref_pat_eat_one_layer_2024;
2135+
let new_match_ergonomics = pat.span.at_least_rust_2024() && no_ref_mut_behind_and;
21362136

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

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

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

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

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

22042209
(expected, r_ty)
22052210
}
22062211

2207-
ty::Ref(_, r_ty, r_mutbl) if r_mutbl == pat_mutbl => (expected, r_ty),
2208-
22092212
_ => {
22102213
let inner_ty = self.next_ty_var(inner.span);
22112214
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)