Skip to content

Commit 2ffdace

Browse files
committed
add notes about ABI
1 parent c63964a commit 2ffdace

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

active_discussion/representation.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
This discussion is meant to focus on two things:
66

77
- What guarantees does Rust make regarding the layout of data structures?
8+
- What guarantees does Rust make regarding ABI compatibility?
89
- What invariants does the compiler require from the various Rust types?
910
- the "validity invariant", as defined in [Ralf's blog post][bp]
1011

@@ -29,6 +30,26 @@ and control how an individual struct will be laid out -- notably with
2930
what sorts of guarantees we offer when it comes to layout, and also
3031
what effect the various `#[repr]` annotations have.
3132

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+
3253
### Validity invariant
3354

3455
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
93114
- [RFC 2195][] defined the layout of `#[repr(C)]` enums with payloads.
94115
- [RFC 2363][] offers a proposal to permit specifying discriminations.
95116
- 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)?
97119
- e.g., what about different structs with same definition
98120
- across executions of the same program?
99121
- Tuples
@@ -106,6 +128,7 @@ To start, we will create threads for each major categories of types
106128
- Out of scope: aliasing rules
107129
- We currently tell LLVM they are aligned and dereferenceable, have to justify that
108130
- Safe code may use them also
131+
- When using the C ABI, these map to the C pointer types, presumably
109132
- Raw pointers
110133
- Effectively same as integers?
111134
- Representation knobs:
@@ -126,3 +149,4 @@ We will also create categories for the following specific areas:
126149
[RFC 2195]: https://rust-lang.github.io/rfcs/2195-really-tagged-unions.html
127150
[RFC 1358]: https://rust-lang.github.io/rfcs/1358-repr-align.html
128151
[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

Comments
 (0)