5
5
This discussion is meant to focus on two things:
6
6
7
7
- What guarantees does Rust make regarding the layout of data structures?
8
+ - What guarantees does Rust make regarding ABI compatibility?
8
9
- What invariants does the compiler require from the various Rust types?
9
10
- the "validity invariant", as defined in [ Ralf's blog post] [ bp ]
10
11
@@ -29,6 +30,26 @@ and control how an individual struct will be laid out -- notably with
29
30
what sorts of guarantees we offer when it comes to layout, and also
30
31
what effect the various ` #[repr] ` annotations have.
31
32
33
+ ### ABI compatibilty
34
+
35
+ When one either calls a foreign function or is called by one, extra
36
+ care is needed to ensure that all the ABI details line up. ABI compatibility
37
+ is related to data structure layout but -- in some cases -- can add another
38
+ layer of complexity. For example, consider a struct with one field, like this one:
39
+
40
+ ``` rust
41
+ #[repr(C )]
42
+ struct Foo { field : u32 }
43
+ ```
44
+
45
+ The memory layout of ` Foo ` is identical to a ` u32 ` . But in many ABIs,
46
+ the struct type ` Foo ` is treated differently at the point of a
47
+ function call than a ` u32 ` would be. Eliminating these gaps is the
48
+ goal of the ` #[repr(transparent)] ` annotation introduced in [ RFC
49
+ 1758] . For built-in types, such as ` &T ` and so forth, it is important
50
+ for us to specify how they are treated at the point of a function
51
+ call.
52
+
32
53
### Validity invariant
33
54
34
55
The "validity invariant" for each type defines what must hold whenever
@@ -93,7 +114,8 @@ To start, we will create threads for each major categories of types
93
114
- [ RFC 2195] [ ] defined the layout of ` #[repr(C)] ` enums with payloads.
94
115
- [ RFC 2363] [ ] offers a proposal to permit specifying discriminations.
95
116
- Structs
96
- - Do we ever say * anything* about how a ` #[repr(rust)] ` struct is laid out?
117
+ - Do we ever say * anything* about how a ` #[repr(rust)] ` struct is laid out
118
+ (and/or treated by the ABI)?
97
119
- e.g., what about different structs with same definition
98
120
- across executions of the same program?
99
121
- Tuples
@@ -106,6 +128,7 @@ To start, we will create threads for each major categories of types
106
128
- Out of scope: aliasing rules
107
129
- We currently tell LLVM they are aligned and dereferenceable, have to justify that
108
130
- Safe code may use them also
131
+ - When using the C ABI, these map to the C pointer types, presumably
109
132
- Raw pointers
110
133
- Effectively same as integers?
111
134
- Representation knobs:
@@ -126,3 +149,4 @@ We will also create categories for the following specific areas:
126
149
[ RFC 2195 ] : https://rust-lang.github.io/rfcs/2195-really-tagged-unions.html
127
150
[ RFC 1358 ] : https://rust-lang.github.io/rfcs/1358-repr-align.html
128
151
[ RFC 1240 ] : https://rust-lang.github.io/rfcs/1240-repr-packed-unsafe-ref.html
152
+ [ RFC 1758 ] : https://rust-lang.github.io/rfcs/1758-repr-transparent.html
0 commit comments