File: mut-ref-mut.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 (35 lines) | stat: -rw-r--r-- 1,535 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
//@ revisions: stable2021 classic2021 structural2021 classic2024 structural2024
//@[stable2021] edition: 2021
//@[classic2021] edition: 2021
//@[structural2021] edition: 2021
//@[classic2024] edition: 2024
//@[structural2024] edition: 2024
//@[stable2021] run-pass
//@[classic2021] run-pass
//@[structural2021] run-pass
//@ dont-require-annotations: NOTE

//! Test diagnostics for binding with `mut` when the default binding mode is by-ref.
#![allow(incomplete_features, unused_assignments, unused_variables)]
#![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))]

pub fn main() {
    struct Foo(u8);

    let Foo(mut a) = &Foo(0);
    //[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference
    #[cfg(any(stable2021, classic2021, structural2021))] { a = 42 }
    #[cfg(any(classic2024, structural2024))] { a = &42 }

    let Foo(mut a) = &mut Foo(0);
    //[classic2024,structural2024]~^ ERROR: binding cannot be both mutable and by-reference
    #[cfg(any(stable2021, classic2021, structural2021))] { a = 42 }
    #[cfg(any(classic2024, structural2024))] { a = &mut 42 }

    let [&mut mut x] = &[&mut 0];
    //[classic2024]~^ ERROR: mismatched types
    //[classic2024]~| NOTE cannot match inherited `&` with `&mut` pattern
    //[structural2024]~^^^ ERROR binding cannot be both mutable and by-reference
    #[cfg(any(stable2021, classic2021, structural2021))] { x = 0 }
}