]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_error_codes/src/error_codes/E0690.md
New upstream version 1.55.0+dfsg1
[rustc.git] / compiler / rustc_error_codes / src / error_codes / E0690.md
1 A struct with the representation hint `repr(transparent)` had two or more fields
2 that were not guaranteed to be zero-sized.
3
4 Erroneous code example:
5
6 ```compile_fail,E0690
7 #[repr(transparent)]
8 struct LengthWithUnit<U> { // error: transparent struct needs at most one
9 value: f32, // non-zero-sized field, but has 2
10 unit: U,
11 }
12 ```
13
14 Because transparent structs are represented exactly like one of their fields at
15 run time, said field must be uniquely determined. If there are multiple fields,
16 it is not clear how the struct should be represented.
17 Note that fields of zero-sized types (e.g., `PhantomData`) can also exist
18 alongside the field that contains the actual data, they do not count for this
19 error. When generic types are involved (as in the above example), an error is
20 reported because the type parameter could be non-zero-sized.
21
22 To combine `repr(transparent)` with type parameters, `PhantomData` may be
23 useful:
24
25 ```
26 use std::marker::PhantomData;
27
28 #[repr(transparent)]
29 struct LengthWithUnit<U> {
30 value: f32,
31 unit: PhantomData<U>,
32 }
33 ```