File: borrowck-errors.rs

package info (click to toggle)
rustc 1.88.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 934,128 kB
  • sloc: xml: 158,127; python: 36,062; javascript: 19,855; sh: 19,700; cpp: 18,947; ansic: 12,993; asm: 4,792; makefile: 690; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (56 lines) | stat: -rw-r--r-- 2,433 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024
//@[stable2021] edition: 2021
//@[classic2021] edition: 2021
//@[structural2021] edition: 2021
//@[classic2024] edition: 2024
//@[structural2024] edition: 2024
//@ dont-require-annotations: NOTE

//! Tests for pattern errors not handled by the pattern typing rules, but by borrowck.
#![allow(incomplete_features)]
#![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))]

/// 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]~^ ERROR mismatched types
    //[stable2021]~| NOTE types differ in mutability
    //[classic2024]~^^^ ERROR: cannot move out of type
    #[cfg(any(classic2021, structural2021))] let _: u32 = x;
    #[cfg(structural2024)] let _: &u32 = x;

    let [&x] = &mut [&mut 0];
    //[stable2021]~^ ERROR mismatched types
    //[stable2021]~| NOTE types differ in mutability
    //[classic2024]~^^^ ERROR: cannot move out of type
    #[cfg(any(classic2021, structural2021))] let _: u32 = x;
    #[cfg(structural2024)] let _: &u32 = x;
}

pub fn main() {
    if let Some(&Some(x)) = Some(&Some(&mut 0)) {
        //~^ ERROR: cannot move out of a shared reference [E0507]
        let _: &u32 = x;
    }

    let &ref mut x = &0;
    //~^ ERROR cannot borrow data in a `&` reference as mutable [E0596]

    // For 2021 edition, this is also a regression test for #136223
    // since the maximum mutability is downgraded during the pattern check process.
    if let &Some(Some(x)) = &Some(&mut Some(0)) {
        //[stable2021,classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
        #[cfg(any(structural2021, structural2024))] let _: &u32 = x;
    }

    let &[x] = &&mut [0];
    //[stable2021,classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable
    #[cfg(any(structural2021, structural2024))] let _: &u32 = x;

    let [&mut x] = &mut [&mut 0];
    //[classic2024]~^ ERROR: cannot move out of type
    #[cfg(any(stable2021, classic2021, structural2021))] let _: u32 = x;
    #[cfg(structural2024)] let _: &mut u32 = x;
}