|
10 | 10 |
|
11 | 11 | #![feature(untagged_unions)]
|
12 | 12 |
|
13 |
| -union U { |
| 13 | +union U1 { |
14 | 14 | a: u8
|
15 | 15 | }
|
16 | 16 |
|
| 17 | +union U2 { |
| 18 | + a: String |
| 19 | +} |
| 20 | + |
| 21 | +union U3<T> { |
| 22 | + a: T |
| 23 | +} |
| 24 | + |
| 25 | +union U4<T: Copy> { |
| 26 | + a: T |
| 27 | +} |
| 28 | + |
| 29 | +fn generic_noncopy<T: Default>() { |
| 30 | + let mut u3 = U3 { a: T::default() }; |
| 31 | + u3.a = T::default(); //~ ERROR assignment to non-`Copy` union field requires unsafe |
| 32 | +} |
| 33 | + |
| 34 | +fn generic_copy<T: Copy + Default>() { |
| 35 | + let mut u3 = U3 { a: T::default() }; |
| 36 | + // FIXME: it should be known here that `T: Copy`, need to use correct "parameter environment" |
| 37 | + u3.a = T::default(); //~ ERROR assignment to non-`Copy` union field requires unsafe |
| 38 | + let mut u4 = U4 { a: T::default() }; |
| 39 | + u4.a = T::default(); // OK |
| 40 | +} |
| 41 | + |
17 | 42 | fn main() {
|
18 |
| - let mut u = U { a: 10 }; // OK |
19 |
| - let a = u.a; //~ ERROR access to union field requires unsafe function or block |
20 |
| - u.a = 11; //~ ERROR access to union field requires unsafe function or block |
21 |
| - let U { a } = u; //~ ERROR matching on union field requires unsafe function or block |
22 |
| - if let U { a: 12 } = u {} //~ ERROR matching on union field requires unsafe function or block |
23 |
| - // let U { .. } = u; // OK |
| 43 | + let mut u1 = U1 { a: 10 }; // OK |
| 44 | + let a = u1.a; //~ ERROR access to union field requires unsafe |
| 45 | + u1.a = 11; // OK |
| 46 | + let U1 { a } = u1; //~ ERROR matching on union field requires unsafe |
| 47 | + if let U1 { a: 12 } = u1 {} //~ ERROR matching on union field requires unsafe |
| 48 | + // let U1 { .. } = u1; // OK |
| 49 | + |
| 50 | + let mut u2 = U2 { a: String::from("old") }; // OK |
| 51 | + u2.a = String::from("new"); //~ ERROR assignment to non-`Copy` union field requires unsafe |
| 52 | + let mut u3 = U3 { a: 0 }; // OK |
| 53 | + u3.a = 1; // OK |
| 54 | + let mut u3 = U3 { a: String::from("old") }; // OK |
| 55 | + u3.a = String::from("new"); //~ ERROR assignment to non-`Copy` union field requires unsafe |
24 | 56 | }
|
0 commit comments