Properly downgrade inherited mutability

This commit is contained in:
Jules Bertholet 2024-04-05 21:18:05 -04:00
parent 93544d5db3
commit 4cd87c463c
No known key found for this signature in database
GPG Key ID: 32034DAFC38C1BFC
6 changed files with 77 additions and 3 deletions

View File

@ -299,7 +299,11 @@ fn calc_default_binding_mode(
if mutbls_match {
(expected, INITIAL_BM, true)
} else {
(expected, def_bm, false)
let mut new_bm = def_bm;
if new_bm.0 == ByRef::Yes(Mutability::Mut) && mutbl == Mutability::Not {
new_bm.0 = ByRef::Yes(Mutability::Not);
}
(expected, new_bm, false)
}
} else {
(expected, INITIAL_BM, mutbls_match)

View File

@ -26,7 +26,16 @@ pub fn main() {
if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
let _: u32 = x;
}
if let Some(&Some(&mut x)) = &mut Some(& Some(0)) {
if let Some(&Some(&x)) = &mut Some(&Some(0)) {
let _: u32 = x;
}
if let Some(&Some(x)) = &mut Some(&Some(0)) {
let _: &u32 = x;
}
if let Some(&Some(&mut ref x)) = Some(&Some(&mut 0)) {
let _: &u32 = x;
}
if let Some(Some(&mut x)) = &Some(Some(&mut 0)) {
let _: &u32 = x;
}
}

View File

@ -10,4 +10,11 @@ pub fn main() {
if let Some(&Some(&_)) = &Some(&mut Some(0)) {
//~^ ERROR: mismatched types
}
if let Some(&Some(x)) = &mut Some(&Some(0)) {
let _: &mut u32 = x;
//~^ ERROR: mismatched types
}
if let Some(&Some(&x)) = Some(&Some(&mut 0)) {
//~^ ERROR: mismatched types
}
}

View File

@ -20,6 +20,32 @@ LL | if let Some(&Some(&_)) = &Some(&mut Some(0)) {
= note: expected type `{integer}`
found reference `&_`
error: aborting due to 2 previous errors
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:14:27
|
LL | let _: &mut u32 = x;
| -------- ^ types differ in mutability
| |
| expected due to this
|
= note: expected mutable reference `&mut u32`
found reference `&{integer}`
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:17:23
|
LL | if let Some(&Some(&x)) = Some(&Some(&mut 0)) {
| ^^ ------------------- this expression has type `Option<&Option<&mut {integer}>>`
| |
| types differ in mutability
|
= note: expected mutable reference `&mut {integer}`
found reference `&_`
help: consider removing `&` from the pattern
|
LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) {
| ~
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0308`.

View File

@ -0,0 +1,11 @@
//@ edition: 2024
//@ compile-flags: -Zunstable-options
#![allow(incomplete_features)]
#![feature(ref_pat_eat_one_layer_2024)]
pub fn main() {
if let Some(&Some(x)) = Some(&Some(&mut 0)) {
//~^ ERROR: cannot move out of a shared reference [E0507]
let _: &u32 = x;
}
}

View File

@ -0,0 +1,17 @@
error[E0507]: cannot move out of a shared reference
--> $DIR/ref_pat_eat_one_layer_2024_fail2.rs:7:29
|
LL | if let Some(&Some(x)) = Some(&Some(&mut 0)) {
| - ^^^^^^^^^^^^^^^^^^^
| |
| data moved here
| move occurs because `x` has type `&mut u32`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
LL | if let Some(&Some(ref x)) = Some(&Some(&mut 0)) {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0507`.