Description
Almost all layout-related discussions add requirements on the platform in one way or another (e.g. due to repr(C)
), and I think it is worth it to have these all in one place.
For example, while discussing the definition of the layout of bool
, one definition is that "bool
is C compatible" and another one that "bool
has size 1
where 0
represents false
and 1
represents true
". If we require the C platform to have sizeof(_Bool) == 1
both definitions are correct, and if we don't explicitly require that somewhere, we would be implying it later on anyways if we specify that bool
is a "proper" C type.
Because not everyone is on Zulip, I'd like to start collecting discussion points for a minimal specification of what the C platform is and what requirements does Rust impose on it in the context of type layout (no calling conventions, etc.). I can send a PR afterwards with more points.
For background, @gankro wrote an excellent document about this: https://gankro.github.io/blah/rust-layouts-and-abis/
Goal of the discussion
The goal is to write down all the current agreed (in other UCG discussions) requirements that Rust has on the C platform and put them in one place to avoid repeating these in all other discussions. This list can evolve over time, and trade-offs on layouts can influence the platform requirements.
Some discussion points
-
Is a C platform required or optional?
-
If a C platform is optional, can the layout of
#[repr(C)]
types be platform-dependent ? If so, what's their layout when there is no C platform? -
Which optional parts of a C platform are relevant for Rust? e.g. floats, maybe atomics?
-
Does a C platform have to be C standard compliant? This allows us to only specify stricter requirements than what e.g. C17 guarantees.
-
@gankro's post mentions the following requirements that go beyond what C17 guarantees:
- Have 8-bit, unaligned bytes (chars)
- Have a boolean be a byte, where true = 1 and false = 0
- Have integers be two's complement
- Have IEEE 754(-2008?) binary floats, if they exist (e.g. we're comfortable with just disabling floats)
- Be at least 16-bit (just in terms of pointer size, I think?)
- Have NULL be 0 (although things may be mapped to 0, but that's messy since references can't be NULL)
Which points of @gankro's list do we need to settle during the layout discussions? They all look reasonable to me. This could be an initial specification.
-
Maybe offtopic: we might have to define / explain what a C platform is. Does C++ requires a C platform? Maybe we could look up how C++ does this.