File: qualif-indirect-mutation-fail.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg2-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 893,176 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (61 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download | duplicates (5)
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
//@ compile-flags: --crate-type=lib
#![feature(const_precise_live_drops)]

// Mutable borrow of a field with drop impl.
pub const fn f() {
    let mut a: (u32, Option<String>) = (0, None); //~ ERROR destructor of
    let _ = &mut a.1;
}

// Mutable borrow of a type with drop impl.
pub const A1: () = {
    let mut x = None; //~ ERROR destructor of
    let mut y = Some(String::new());
    let a = &mut x;
    let b = &mut y;
    std::mem::swap(a, b);
    std::mem::forget(y);
};

// Mutable borrow of a type with drop impl.
pub const A2: () = {
    let mut x = None;
    let mut y = Some(String::new());
    let a = &mut x;
    let b = &mut y;
    std::mem::swap(a, b);
    std::mem::forget(y);
    let _z = x; //~ ERROR destructor of
};

// Shared borrow of a type that might be !Freeze and Drop.
pub const fn g1<T>() {
    let x: Option<T> = None; //~ ERROR destructor of
    let _ = x.is_some();
}

// Shared borrow of a type that might be !Freeze and Drop.
pub const fn g2<T>() {
    let x: Option<T> = None;
    let _ = x.is_some();
    let _y = x; //~ ERROR destructor of
}

// Mutable raw reference to a Drop type.
pub const fn address_of_mut() {
    let mut x: Option<String> = None; //~ ERROR destructor of
    &raw mut x;

    let mut y: Option<String> = None; //~ ERROR destructor of
    std::ptr::addr_of_mut!(y);
}

// Const raw reference to a Drop type. Conservatively assumed to allow mutation
// until resolution of https://github.com/rust-lang/rust/issues/56604.
pub const fn address_of_const() {
    let x: Option<String> = None; //~ ERROR destructor of
    &raw const x;

    let y: Option<String> = None; //~ ERROR destructor of
    std::ptr::addr_of!(y);
}