about summary refs log tree commit diff
path: root/tests/ui/pattern/rfc-3627-match-ergonomics-2024/experimental/ref-binding-on-inh-ref-errors.rs
blob: c9e3f75cf178aa46ccb7eb58f13d38288df8d68d (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//@ 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 errors from binding with `ref x` under a by-ref default binding mode in edition 2024.
//! These can't be in the same body as tests for other errors, since they're emitted during THIR
//! construction. The errors on stable edition 2021 Rust are unrelated.
#![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 only fail on the eat-inner variant of the new edition 2024 pattern typing rules.
/// The eat-outer variant eats the inherited reference, so binding with `ref` isn't a problem.
fn errors_from_eating_the_real_reference() {
    let [&ref x] = &[&0];
    //[structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x;
    #[cfg(classic2024)] let _: &&u32 = x;

    let [&ref x] = &mut [&0];
    //[structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x;
    #[cfg(classic2024)] let _: &&u32 = x;

    let [&mut ref x] = &mut [&mut 0];
    //[structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x;
    #[cfg(classic2024)] let _: &&mut u32 = x;

    let [&mut ref mut x] = &mut [&mut 0];
    //[structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(stable2021, classic2021, structural2021))] let _: &mut u32 = x;
    #[cfg(classic2024)] let _: &mut &mut u32 = x;
}

/// To make absolutely sure binding with `ref` ignores inherited references on stable, let's
/// quarantine these typeck errors (from using a `&` pattern to match a `&mut` reference type).
fn errors_from_eating_the_real_reference_caught_in_hir_typeck_on_stable() {
    let [&ref x] = &[&mut 0];
    //[stable2021]~^ ERROR: mismatched types
    //[stable2021]~| NOTE types differ in mutability
    //[structural2024]~^^^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(classic2021, structural2021))] let _: &u32 = x;
    #[cfg(classic2024)] let _: &&mut u32 = x;

    let [&ref x] = &mut [&mut 0];
    //[stable2021]~^ ERROR: mismatched types
    //[stable2021]~| NOTE types differ in mutability
    //[structural2024]~^^^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(classic2021, structural2021))] let _: &u32 = x;
    #[cfg(classic2024)] let _: &&mut u32 = x;
}

/// This one also needs to be quarantined for a typeck error on `classic2024` (eat-outer).
fn errors_dependent_on_eating_order_caught_in_hir_typeck_when_eating_outer() {
    let [&mut ref x] = &[&mut 0];
    //[classic2024]~^ ERROR: mismatched types
    //[classic2024]~| NOTE cannot match inherited `&` with `&mut` pattern
    //[structural2024]~^^^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x;
}

/// These should be errors in all editions. In edition 2024, they should be caught by the pattern
/// typing rules disallowing `ref` when there's an inherited reference. In old editions where that
/// resets the binding mode, they're borrowck errors due to binding with `ref mut`.
/// As a quirk of how the edition 2024 error is emitted during THIR construction, it ends up going
/// through borrowck as well, using the old `ref` behavior as a fallback, so we get that error too.
fn borrowck_errors_in_old_editions() {
    let [ref mut x] = &[0];
    //~^ ERROR: cannot borrow data in a `&` reference as mutable
    //[classic2024,structural2024]~| ERROR: binding modifiers may only be written when the default binding mode is `move`
}

/// The remaining tests are purely for testing `ref` bindings in the presence of an inherited
/// reference. These should always fail on edition 2024 and succeed on edition 2021.
pub fn main() {
    let [ref x] = &[0];
    //[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x;

    let [ref x] = &mut [0];
    //[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(stable2021, classic2021, structural2021))] let _: &u32 = x;

    let [ref mut x] = &mut [0];
    //[classic2024,structural2024]~^ ERROR: binding modifiers may only be written when the default binding mode is `move`
    #[cfg(any(stable2021, classic2021, structural2021))] let _: &mut u32 = x;
}