Skip to content

Commit cf7cdfc

Browse files
Properly downgrade inherited mutability
1 parent 2f730c1 commit cf7cdfc

6 files changed

+74
-3
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
299299
if mutbls_match {
300300
(expected, INITIAL_BM, true)
301301
} else {
302-
(expected, def_bm, false)
302+
let mut new_bm = def_bm;
303+
if new_bm.0 == ByRef::Yes(Mutability::Mut) && mutbl == Mutability::Not {
304+
new_bm.0 = ByRef::Yes(Mutability::Not);
305+
}
306+
(expected, new_bm, false)
303307
}
304308
} else {
305309
(expected, INITIAL_BM, mutbls_match)

tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ pub fn main() {
2626
if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
2727
let _: u32 = x;
2828
}
29-
if let Some(&Some(&mut x)) = &mut Some(& Some(0)) {
29+
if let Some(&Some(&x)) = &mut Some(&Some(0)) {
3030
let _: u32 = x;
3131
}
32+
if let Some(&Some(x)) = &mut Some(&Some(0)) {
33+
let _: &u32 = x;
34+
}
35+
if let Some(&Some(&mut ref x)) = Some(&Some(&mut 0)) {
36+
let _: &u32 = x;
37+
}
3238
}

tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.rs

+7
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ pub fn main() {
1010
if let Some(&Some(&_)) = &Some(&mut Some(0)) {
1111
//~^ ERROR: mismatched types
1212
}
13+
if let Some(&Some(x)) = &mut Some(&Some(0)) {
14+
let _: &mut u32 = x;
15+
//~^ ERROR: mismatched types
16+
}
17+
if let Some(&Some(&x)) = Some(&Some(&mut 0)) {
18+
//~^ ERROR: mismatched types
19+
}
1320
}

tests/ui/match/ref_pat_eat_one_layer_2024/ref_pat_eat_one_layer_2024_fail.stderr

+27-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,32 @@ LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) {
2020
= note: expected type `{integer}`
2121
found reference `&_`
2222

23-
error: aborting due to 2 previous errors
23+
error[E0308]: mismatched types
24+
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:14:27
25+
|
26+
LL | let _: &mut u32 = x;
27+
| -------- ^ types differ in mutability
28+
| |
29+
| expected due to this
30+
|
31+
= note: expected mutable reference `&mut u32`
32+
found reference `&{integer}`
33+
34+
error[E0308]: mismatched types
35+
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:17:23
36+
|
37+
LL | if let Some(&Some(&x)) = Some(&Some(&mut 0)) {
38+
| ^^ ------------------- this expression has type `Option<&Option<&mut {integer}>>`
39+
| |
40+
| types differ in mutability
41+
|
42+
= note: expected mutable reference `&mut {integer}`
43+
found reference `&_`
44+
help: consider removing `&` from the pattern
45+
|
46+
LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) {
47+
| ~
48+
49+
error: aborting due to 4 previous errors
2450

2551
For more information about this error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ edition: 2024
2+
//@ compile-flags: -Zunstable-options
3+
#![allow(incomplete_features)]
4+
#![feature(ref_pat_eat_one_layer_2024)]
5+
6+
pub fn main() {
7+
if let Some(&Some(x)) = Some(&Some(&mut 0)) {
8+
//~^ ERROR: cannot move out of a shared reference [E0507]
9+
let _: &u32 = x;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0507]: cannot move out of a shared reference
2+
--> $DIR/ref_pat_eat_one_layer_2024_fail2.rs:7:29
3+
|
4+
LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) {
5+
| - ^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| data moved here
8+
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
9+
|
10+
help: consider borrowing the pattern binding
11+
|
12+
LL | if let Some(&Some(ref x)) = Some(&Some(&mut 0)) {
13+
| +++
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)