File: field_checks.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 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; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (65 lines) | stat: -rw-r--r-- 1,399 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
57
58
59
60
61
62
63
64
65
use std::mem::ManuallyDrop;

union U1 { // OK
    a: u8,
}

union U2<T: Copy> { // OK
    a: T,
}

union U22<T> { // OK
    a: ManuallyDrop<T>,
}

union U23<T> { // OK
    a: (ManuallyDrop<T>, i32),
}

union U24<T> { // OK
    a: [ManuallyDrop<T>; 2],
}

union U3 {
    a: String, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
}

union U32 { // field that does not drop but is not `Copy`, either
    a: std::cell::RefCell<i32>, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
}

union U4<T> {
    a: T, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
}

union U5 { // Having a drop impl is OK
    a: u8,
}

impl Drop for U5 {
    fn drop(&mut self) {}
}

union U5Nested { // a nested union that drops is NOT OK
    nest: U5, //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
}

union U5Nested2 { // for now we don't special-case empty arrays
    nest: [U5; 0], //~ ERROR field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
}

union U6 { // OK
    s: &'static i32,
    m: &'static mut i32,
}

union U7<T> { // OK
    f: (&'static mut i32, ManuallyDrop<T>, i32),
}

union U8<T> { // OK
    f1: [(&'static mut i32, i32); 8],
    f2: [ManuallyDrop<T>; 2],
}

fn main() {}