2025-01-22 23:59:03 -08:00
|
|
|
//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024
|
2025-01-21 02:07:32 -08:00
|
|
|
//@[stable2021] edition: 2021
|
2025-01-22 23:59:03 -08:00
|
|
|
//@[classic2021] edition: 2021
|
|
|
|
|
//@[structural2021] edition: 2021
|
2025-01-21 02:07:32 -08:00
|
|
|
//@[classic2024] edition: 2024
|
|
|
|
|
//@[structural2024] edition: 2024
|
2024-12-23 18:38:14 -08:00
|
|
|
//! Tests for pattern errors not handled by the pattern typing rules, but by borrowck.
|
2024-04-05 21:18:05 -04:00
|
|
|
#![allow(incomplete_features)]
|
2025-01-22 23:59:03 -08:00
|
|
|
#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))]
|
|
|
|
|
#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))]
|
2024-04-05 21:18:05 -04:00
|
|
|
|
2025-01-21 02:07:32 -08:00
|
|
|
/// These patterns additionally use `&` to match a `&mut` reference type, which causes compilation
|
|
|
|
|
/// to fail in HIR typeck on stable. As such, they need to be separate from the other tests.
|
|
|
|
|
fn errors_caught_in_hir_typeck_on_stable() {
|
|
|
|
|
let [&x] = &[&mut 0];
|
|
|
|
|
//[stable2021]~^ mismatched types
|
|
|
|
|
//[stable2021]~| types differ in mutability
|
|
|
|
|
//[classic2024]~^^^ ERROR: cannot move out of type
|
2025-01-22 23:59:03 -08:00
|
|
|
#[cfg(any(classic2021, structural2021))] let _: u32 = x;
|
|
|
|
|
#[cfg(structural2024)] let _: &u32 = x;
|
2025-01-21 02:07:32 -08:00
|
|
|
|
|
|
|
|
let [&x] = &mut [&mut 0];
|
|
|
|
|
//[stable2021]~^ mismatched types
|
|
|
|
|
//[stable2021]~| types differ in mutability
|
|
|
|
|
//[classic2024]~^^^ ERROR: cannot move out of type
|
2025-01-22 23:59:03 -08:00
|
|
|
#[cfg(any(classic2021, structural2021))] let _: u32 = x;
|
|
|
|
|
#[cfg(structural2024)] let _: &u32 = x;
|
2025-01-21 02:07:32 -08:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 21:18:05 -04:00
|
|
|
pub fn main() {
|
|
|
|
|
if let Some(&Some(x)) = Some(&Some(&mut 0)) {
|
|
|
|
|
//~^ ERROR: cannot move out of a shared reference [E0507]
|
|
|
|
|
let _: &u32 = x;
|
|
|
|
|
}
|
2024-05-04 14:57:27 -04:00
|
|
|
|
|
|
|
|
let &ref mut x = &0;
|
|
|
|
|
//~^ cannot borrow data in a `&` reference as mutable [E0596]
|
2025-01-02 21:47:54 -08:00
|
|
|
|
2025-02-01 19:47:12 +00:00
|
|
|
// For 2021 edition, this is also a regression test for #136223
|
|
|
|
|
// since the maximum mutability is downgraded during the pattern check process.
|
2025-01-02 21:47:54 -08:00
|
|
|
if let &Some(Some(x)) = &Some(&mut Some(0)) {
|
2025-01-22 23:59:03 -08:00
|
|
|
//[stable2021,classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
|
|
|
|
|
#[cfg(any(structural2021, structural2024))] let _: &u32 = x;
|
2025-01-02 21:47:54 -08:00
|
|
|
}
|
2025-01-04 01:38:17 -08:00
|
|
|
|
|
|
|
|
let &[x] = &&mut [0];
|
2025-01-22 23:59:03 -08:00
|
|
|
//[stable2021,classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
|
|
|
|
|
#[cfg(any(structural2021, structural2024))] let _: &u32 = x;
|
2025-01-04 01:38:17 -08:00
|
|
|
|
|
|
|
|
let [&mut x] = &mut [&mut 0];
|
2025-01-15 01:21:35 -08:00
|
|
|
//[classic2024]~^ ERROR: cannot move out of type
|
2025-01-22 23:59:03 -08:00
|
|
|
#[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x;
|
2025-01-21 02:07:32 -08:00
|
|
|
#[cfg(structural2024)] let _: &mut u32 = x;
|
2024-04-05 21:18:05 -04:00
|
|
|
}
|