]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | A `repr(transparent)` type was also annotated with other, incompatible |
2 | representation hints. | |
3 | ||
4 | Erroneous code example: | |
5 | ||
6 | ```compile_fail,E0692 | |
7 | #[repr(transparent, C)] // error: incompatible representation hints | |
8 | struct Grams(f32); | |
9 | ``` | |
10 | ||
11 | A type annotated as `repr(transparent)` delegates all representation concerns to | |
12 | another type, so adding more representation hints is contradictory. Remove | |
13 | either the `transparent` hint or the other hints, like this: | |
14 | ||
15 | ``` | |
16 | #[repr(transparent)] | |
17 | struct Grams(f32); | |
18 | ``` | |
19 | ||
20 | Alternatively, move the other attributes to the contained type: | |
21 | ||
22 | ``` | |
23 | #[repr(C)] | |
24 | struct Foo { | |
25 | x: i32, | |
26 | // ... | |
27 | } | |
28 | ||
29 | #[repr(transparent)] | |
30 | struct FooWrapper(Foo); | |
31 | ``` | |
32 | ||
33 | Note that introducing another `struct` just to have a place for the other | |
34 | attributes may have unintended side effects on the representation: | |
35 | ||
36 | ``` | |
37 | #[repr(transparent)] | |
38 | struct Grams(f32); | |
39 | ||
40 | #[repr(C)] | |
41 | struct Float(f32); | |
42 | ||
43 | #[repr(transparent)] | |
44 | struct Grams2(Float); // this is not equivalent to `Grams` above | |
45 | ``` | |
46 | ||
47 | Here, `Grams2` is a not equivalent to `Grams` -- the former transparently wraps | |
48 | a (non-transparent) struct containing a single float, while `Grams` is a | |
49 | transparent wrapper around a float. This can make a difference for the ABI. |