Rollup merge of #127369 - Jules-Bertholet:match-ergonomics-2021, r=Nadrieril

Match ergonomics 2024: align with RFC again

- `&` matches `&mut` on old editions
- Add some more tests

r? ``@Nadrieril``

cc https://github.com/rust-lang/rust/issues/123076

``@rustbot`` label A-edition-2024 A-patterns
This commit is contained in:
Jubilee 2024-07-05 23:23:35 -07:00 committed by GitHub
commit 3ba4195a68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 142 additions and 49 deletions

View File

@ -2217,7 +2217,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!("check_pat_ref: expected={:?}", expected);
match *expected.kind() {
ty::Ref(_, r_ty, r_mutbl)
if (new_match_ergonomics && r_mutbl >= pat_mutbl)
if (no_ref_mut_behind_and && r_mutbl >= pat_mutbl)
|| r_mutbl == pat_mutbl =>
{
if no_ref_mut_behind_and && r_mutbl == Mutability::Not {

View File

@ -1,37 +1,15 @@
//@ run-pass
//@ edition: 2021
//@ revisions: classic structural both
#![allow(incomplete_features)]
#![feature(ref_pat_eat_one_layer_2024)]
#![cfg_attr(any(classic, both), feature(ref_pat_eat_one_layer_2024))]
#![cfg_attr(any(structural, both), feature(ref_pat_eat_one_layer_2024_structural))]
pub fn main() {
if let Some(Some(&x)) = &Some(&Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(Some(&x)) = &Some(Some(&0)) {
if let &Some(Some(x)) = &Some(&mut Some(0)) {
let _: &u32 = x;
//~^ ERROR: mismatched types
}
if let Some(Some(&&x)) = &Some(Some(&0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(&Some(x)) = &Some(Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(Some(&x)) = &Some(&Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
//~^ ERROR: mismatched types
if let Some(&x) = Some(&mut 0) {
let _: u32 = x;
}
}

View File

@ -0,0 +1,37 @@
//@ edition: 2021
#![allow(incomplete_features)]
#![feature(ref_pat_eat_one_layer_2024)]
pub fn main() {
if let Some(Some(&x)) = &Some(&Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(Some(&x)) = &Some(Some(&0)) {
let _: &u32 = x;
//~^ ERROR: mismatched types
}
if let Some(Some(&&x)) = &Some(Some(&0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(&Some(x)) = &Some(Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(Some(&x)) = &Some(&Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
//~^ ERROR: mismatched types
let _: u32 = x;
}
}

View File

@ -1,5 +1,5 @@
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2021.rs:5:22
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:5:22
|
LL | if let Some(Some(&x)) = &Some(&Some(0)) {
| ^^ --------------- this expression has type `&Option<&Option<{integer}>>`
@ -14,7 +14,7 @@ LL | if let Some(Some(x)) = &Some(&Some(0)) {
| ~
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2021.rs:10:23
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:10:23
|
LL | let _: &u32 = x;
| ---- ^ expected `&u32`, found integer
@ -27,7 +27,7 @@ LL | let _: &u32 = &x;
| +
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2021.rs:13:23
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:13:23
|
LL | if let Some(Some(&&x)) = &Some(Some(&0)) {
| ^^ --------------- this expression has type `&Option<Option<&{integer}>>`
@ -43,7 +43,7 @@ LL + if let Some(Some(&x)) = &Some(Some(&0)) {
|
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2021.rs:17:17
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:17:17
|
LL | if let Some(&Some(x)) = &Some(Some(0)) {
| ^^^^^^^^ -------------- this expression has type `&Option<Option<{integer}>>`
@ -54,7 +54,7 @@ LL | if let Some(&Some(x)) = &Some(Some(0)) {
found reference `&_`
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2021.rs:21:22
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:21:22
|
LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
| ^^^^^^ ----------------------- this expression has type `&mut Option<&mut Option<{integer}>>`
@ -64,7 +64,7 @@ LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
= note: expected type `{integer}`
found mutable reference `&mut _`
note: to declare a mutable binding use: `mut x`
--> $DIR/ref_pat_eat_one_layer_2021.rs:21:22
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:21:22
|
LL | if let Some(Some(&mut x)) = &mut Some(&mut Some(0)) {
| ^^^^^^
@ -74,7 +74,7 @@ LL | if let Some(Some(x)) = &mut Some(&mut Some(0)) {
| ~
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2021.rs:25:22
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:25:22
|
LL | if let Some(Some(&x)) = &Some(&Some(0)) {
| ^^ --------------- this expression has type `&Option<&Option<{integer}>>`
@ -89,7 +89,7 @@ LL | if let Some(Some(x)) = &Some(&Some(0)) {
| ~
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2021.rs:29:27
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:29:27
|
LL | if let Some(&mut Some(&x)) = &Some(&mut Some(0)) {
| ^^ ------------------- this expression has type `&Option<&mut Option<{integer}>>`
@ -104,7 +104,7 @@ LL | if let Some(&mut Some(x)) = &Some(&mut Some(0)) {
| ~
error[E0308]: mismatched types
--> $DIR/ref_pat_eat_one_layer_2021.rs:33:23
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:33:23
|
LL | if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
| ^^^^^^ ------------------- this expression has type `&mut Option<&Option<{integer}>>`
@ -114,7 +114,7 @@ LL | if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
= note: expected type `{integer}`
found mutable reference `&mut _`
note: to declare a mutable binding use: `mut x`
--> $DIR/ref_pat_eat_one_layer_2021.rs:33:23
--> $DIR/ref_pat_eat_one_layer_2021_fail.rs:33:23
|
LL | if let Some(&Some(&mut x)) = &mut Some(&Some(0)) {
| ^^^^^^

View File

@ -63,4 +63,27 @@ pub fn main() {
if let Some(&mut x) = &Some(&mut 0) {
let _: &u32 = x;
}
fn generic<R: Ref>() -> (R, bool) {
R::meow()
}
trait Ref: Sized {
fn meow() -> (Self, bool);
}
impl Ref for &'static [(); 0] {
fn meow() -> (Self, bool) {
(&[], false)
}
}
impl Ref for &'static mut [(); 0] {
fn meow() -> (Self, bool) {
(&mut [], true)
}
}
let (&_, b) = generic();
assert!(!b);
}

View File

@ -150,7 +150,20 @@ LL | let Foo(mut a) = &mut Foo(0);
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 14 previous errors
error[E0277]: the trait bound `&_: main::Ref` is not satisfied
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:83:14
|
LL | let &_ = generic();
| ^^^^^^^^^ the trait `main::Ref` is not implemented for `&_`
|
= help: the trait `main::Ref` is implemented for `&'static mut [(); 0]`
note: required by a bound in `generic`
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:69:19
|
LL | fn generic<R: Ref>() -> R {
| ^^^ required by this bound in `generic`
Some errors have detailed explanations: E0308, E0658.
For more information about an error, try `rustc --explain E0308`.
error: aborting due to 15 previous errors
Some errors have detailed explanations: E0277, E0308, E0658.
For more information about an error, try `rustc --explain E0277`.

View File

@ -180,7 +180,20 @@ LL | let Foo(mut a) = &mut Foo(0);
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 16 previous errors
error[E0277]: the trait bound `&_: main::Ref` is not satisfied
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:83:14
|
LL | let &_ = generic();
| ^^^^^^^^^ the trait `main::Ref` is not implemented for `&_`
|
= help: the trait `main::Ref` is implemented for `&'static mut [(); 0]`
note: required by a bound in `generic`
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:69:19
|
LL | fn generic<R: Ref>() -> R {
| ^^^ required by this bound in `generic`
Some errors have detailed explanations: E0308, E0658.
For more information about an error, try `rustc --explain E0308`.
error: aborting due to 17 previous errors
Some errors have detailed explanations: E0277, E0308, E0658.
For more information about an error, try `rustc --explain E0277`.

View File

@ -65,4 +65,20 @@ pub fn main() {
let Foo(mut a) = &mut Foo(0);
//~^ ERROR: binding cannot be both mutable and by-reference
a = &mut 42;
fn generic<R: Ref>() -> R {
R::meow()
}
trait Ref: Sized {
fn meow() -> Self;
}
impl Ref for &'static mut [(); 0] {
fn meow() -> Self {
&mut []
}
}
let &_ = generic(); //~ERROR: the trait bound `&_: main::Ref` is not satisfied [E0277]
}

View File

@ -161,7 +161,20 @@ LL | let Foo(mut a) = &mut Foo(0);
= help: add `#![feature(mut_ref)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 15 previous errors
error[E0277]: the trait bound `&_: main::Ref` is not satisfied
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:83:14
|
LL | let &_ = generic();
| ^^^^^^^^^ the trait `main::Ref` is not implemented for `&_`
|
= help: the trait `main::Ref` is implemented for `&'static mut [(); 0]`
note: required by a bound in `generic`
--> $DIR/ref_pat_eat_one_layer_2024_fail.rs:69:19
|
LL | fn generic<R: Ref>() -> R {
| ^^^ required by this bound in `generic`
Some errors have detailed explanations: E0308, E0658.
For more information about an error, try `rustc --explain E0308`.
error: aborting due to 16 previous errors
Some errors have detailed explanations: E0277, E0308, E0658.
For more information about an error, try `rustc --explain E0277`.