|
| 1 | +//@ revisions: classic2021 structural2021 classic2024 structural2024 |
| 2 | +//@[classic2021] edition: 2021 |
| 3 | +//@[structural2021] edition: 2021 |
| 4 | +//@[classic2024] edition: 2024 |
| 5 | +//@[structural2024] edition: 2024 |
| 6 | +//@ aux-build:mixed-editions-macros.rs |
| 7 | +//! Tests for typing mixed-edition patterns under the `ref_pat_eat_one_layer_2024` and |
| 8 | +//! `ref_pat_eat_one_layer_2024_structural` feature gates. |
| 9 | +//! This is meant both to check that patterns are typed with edition-appropriate typing rules and |
| 10 | +//! that we keep our internal state consistent when mixing editions. |
| 11 | +#![allow(incomplete_features, unused)] |
| 12 | +#![cfg_attr(any(classic2021, classic2024), feature(ref_pat_eat_one_layer_2024))] |
| 13 | +#![cfg_attr(any(structural2021, structural2024), feature(ref_pat_eat_one_layer_2024_structural))] |
| 14 | + |
| 15 | +extern crate mixed_editions_macros; |
| 16 | +use mixed_editions_macros::*; |
| 17 | + |
| 18 | +// Tests type equality in a way that avoids coercing `&&T` to `&T`. |
| 19 | +trait Eq<T> {} |
| 20 | +impl<T> Eq<T> for T {} |
| 21 | +fn assert_type_eq<T, U: Eq<T>>(_: T, _: U) {} |
| 22 | + |
| 23 | +/// Make sure binding with `ref` in the presence of an inherited reference is forbidden when and |
| 24 | +/// only when the binding is from edition 2024. |
| 25 | +fn ref_binding_tests() { |
| 26 | + let match_ctor!(ref x) = &[0]; |
| 27 | + //[classic2024,structural2024]~^ ERROR: this pattern relies on behavior which may change in edition 2024 |
| 28 | + #[cfg(any(classic2021, structural2021))] assert_type_eq(x, &0u32); |
| 29 | + |
| 30 | + let [bind_ref!(y)] = &[0]; |
| 31 | + //[classic2021,structural2021]~^ ERROR: this pattern relies on behavior which may change in edition 2024 |
| 32 | + #[cfg(any(classic2024, structural2024))] assert_type_eq(y, &0u32); |
| 33 | +} |
| 34 | + |
| 35 | +/// Likewise, when binding with `mut`. |
| 36 | +fn mut_binding_tests() { |
| 37 | + let match_ctor!(mut x) = &[0]; |
| 38 | + //[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference |
| 39 | + #[cfg(any(classic2021, structural2021))] assert_type_eq(x, 0u32); |
| 40 | + |
| 41 | + let [bind_mut!(y)] = &[0]; |
| 42 | + //[classic2021,structural2021]~^ ERROR: binding cannot be both mutable and by-reference |
| 43 | + #[cfg(any(classic2024, structural2024))] assert_type_eq(y, 0u32); |
| 44 | +} |
| 45 | + |
| 46 | +/// Make sure reference patterns correspond to one deref on edition 2024 and two on edition 2021. |
| 47 | +fn layers_eaten_tests() { |
| 48 | + let match_ctor!(&x) = &[&0]; |
| 49 | + #[cfg(any(classic2021, structural2021))] assert_type_eq(x, 0u32); |
| 50 | + #[cfg(any(classic2024, structural2024))] assert_type_eq(x, &0u32); |
| 51 | + |
| 52 | + let [match_ref!(y)] = &[&0]; |
| 53 | + #[cfg(any(classic2021, structural2021))] assert_type_eq(y, &0u32); |
| 54 | + #[cfg(any(classic2024, structural2024))] assert_type_eq(y, 0u32); |
| 55 | +} |
| 56 | + |
| 57 | +/// Make sure downgrading mutable binding modes inside shared refs ("Rule 3") doesn't break. |
| 58 | +/// This only applies to `ref_pat_eat_one_layer_2024_structural`, which has Rule 3 in all editions; |
| 59 | +/// under `ref_pat_eat_one_layer_2024`, these should be errors. |
| 60 | +fn rule_3_tests() { |
| 61 | + let match_ref!([x]) = &&mut [0]; |
| 62 | + //[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable |
| 63 | + #[cfg(any(structural2021, structural2024))] assert_type_eq(x, &0u32); |
| 64 | + |
| 65 | + let &match_ctor!(y) = &&mut [0]; |
| 66 | + //[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable |
| 67 | + #[cfg(any(structural2021, structural2024))] assert_type_eq(y, &0u32); |
| 68 | + |
| 69 | + let &[bind!(z)] = &&mut [0]; |
| 70 | + //[classic2021,classic2024]~^ ERROR: cannot borrow data in a `&` reference as mutable |
| 71 | + #[cfg(any(structural2021, structural2024))] assert_type_eq(z, &0u32); |
| 72 | +} |
| 73 | + |
| 74 | +/// Test that the interaction between Rules 3 and 5 doesn't break. |
| 75 | +fn rules_3_and_5_tests() { |
| 76 | + let match_ref!([x]) = &mut &mut [0]; |
| 77 | + //[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern |
| 78 | + #[cfg(any(structural2021, structural2024))] assert_type_eq(x, &0u32); |
| 79 | + |
| 80 | + let &match_ctor!(y) = &mut &mut [0]; |
| 81 | + //[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern |
| 82 | + #[cfg(any(structural2021, structural2024))] assert_type_eq(y, &0u32); |
| 83 | + |
| 84 | + let &[bind!(z)] = &mut &mut [0]; |
| 85 | + //[classic2021,classic2024]~^ ERROR: cannot borrow as mutable inside an `&` pattern |
| 86 | + #[cfg(any(structural2021, structural2024))] assert_type_eq(z, &0u32); |
| 87 | +} |
| 88 | + |
| 89 | +/// Make sure matching a lone shared reference with a `&` ("Rule 4") doesn't break. |
| 90 | +fn rule_4_tests() { |
| 91 | + let match_ref!([x]) = &[0]; |
| 92 | + assert_type_eq(x, 0u32); |
| 93 | + |
| 94 | + let &match_ctor!(y) = &[0]; |
| 95 | + assert_type_eq(y, 0u32); |
| 96 | +} |
| 97 | + |
| 98 | +/// Make sure matching a `&mut` reference with a `&` pattern ("Rule 5") doesn't break. |
| 99 | +fn rule_5_tests() { |
| 100 | + let match_ref!(x) = &mut 0; |
| 101 | + assert_type_eq(x, 0u32); |
| 102 | + |
| 103 | + // also test inherited references (assumes rule 4) |
| 104 | + let [match_ref!(y)] = &mut [0]; |
| 105 | + assert_type_eq(y, 0u32); |
| 106 | +} |
| 107 | + |
| 108 | +/// Make sure binding with `ref mut` is an error within a `&` pattern matching a `&mut` reference. |
| 109 | +fn rule_5_mutability_error_tests() { |
| 110 | + let match_ref!(ref mut x) = &mut 0; |
| 111 | + //~^ ERROR: cannot borrow as mutable inside an `&` pattern |
| 112 | + let &bind_ref_mut!(x) = &mut 0; |
| 113 | + //~^ ERROR: cannot borrow as mutable inside an `&` pattern |
| 114 | + |
| 115 | + // also test inherited references (assumes rule 4) |
| 116 | + let [match_ref!(ref mut x)] = &mut [0]; |
| 117 | + //~^ ERROR: cannot borrow as mutable inside an `&` pattern |
| 118 | + let [&bind_ref_mut!(x)] = &mut [0]; |
| 119 | + //~^ ERROR: cannot borrow as mutable inside an `&` pattern |
| 120 | +} |
| 121 | + |
| 122 | +fn main() {} |
0 commit comments