Description
Proposal
There is an ongoing discussion about creating a new stable ABI layout. The idea is not to stabilize #[repr(Rust)]
, but to instead create a new layout of the form #[repr(Interoperable_2024)]
, where the suffix is the edition year to permit future updates if/when needed. The idea is that the stabilized ABI layout will be widely known and shared between different compiler vendors, chip vendors, etc., so that instead of having to use #[repr(C)]
(which is platform dependent), we can have a truly independent layout whose form is well-specified and allows code developed by different vendors to interoperate with one another.
Summary and problem statement
As has been noted elsewhere, C isn't just a programming language, it's been beaten beyond recognition into a communications protocol between languages. For Rust to interoperate with Swift, we have to use something like #[repr(C)]
to beat our data into a form that both languages recognize. The issue is that C was never designed with this in mind. On some platforms the ABI is well defined, but on others, it's basically whatever the first compiler decided it was going to be. This makes interoperability a nightmare as you have to read through the compiler's source to understand what the ABI is, and that's assuming you have access to the source code. If you don't, then you have to do some reverse engineering, which is not guaranteed to catch all corner cases.
Motivation, use-cases, and solution sketches
I want to define an ABI spec that can be shared between different platforms and languages. @Kornel gave some examples in a sibling discussion discussing how a stabilized ABI could help library authors.
I don't have any solid solution, or even solution sketches. I believe that a working group needs to be set up that has the following goals it wants to accomplish:
Interoperable_2024
- A fully stabilized representation that is an alternative torepr(C)
. This will not replacerepr(C)
, it sits beside it. Because this is fully specified on all platforms, library authors can target it for stability. Vendors of other rust compilers can interoperate withrustc
. This could be particularly useful for things likerust-gpu
, whererust-gpu
might compile a chunk of code for the GPU,rustc
compiles for the CPU, and the linker can still make the two work with each other. The limitation is that only a subset of Rust can cross the ABI because this is a minimum viable product that is an alternative torepr(C)
, not an extension of it. Get buy-in from other vendors and other languages.Interoperable_2027
- ExtendsInteroperable_2024
with more modern features (TBD what they are). These should be useful beyond Rust. Get more buy-in from other vendors and other languages. At this point the working group is operating like a normative body, getting as many groups to cooperate as is possible.- Further work as needed.
Links and related work
The initial discussion started in the middle of https://internals.rust-lang.org/t/discussion-editions-in-rust-gcc-and-other-rust-compilers/16608, which was hijacked from the the original topic there.
At several people's suggestion, I created a new topic at https://internals.rust-lang.org/t/repr-interoperable-2024/16763.
@Kornel Linked to it in the library discussion at https://internals.rust-lang.org/t/for-a-library-author-how-maintenance-of-a-stable-abi-would-look-like/16769.
https://users.rust-lang.org/t/modular-abi-for-rust/42590
https://internals.rust-lang.org/t/a-stable-modular-abi-for-rust/12347
https://internals.rust-lang.org/t/when-is-the-abi-stable/10420/2
https://rust-lang.github.io/unsafe-code-guidelines/layout/structs-and-tuples.html
https://internals.rust-lang.org/t/dynamically-linking-rust-crates-to-rust-crates/10369
https://crates.io/crates/abi_stable
https://internals.rust-lang.org/t/making-repr-un-enum-ffi-safe-pre-rfc/5325
Other (sort of) related posts:
https://internals.rust-lang.org/t/add-repr-inherit-type/16626
https://internals.rust-lang.org/t/question-about-c-abi-stability/12449
https://internals.rust-lang.org/t/pre-rfc-repr-c-for-traits/12598
Initial people involved
I'm not sure how will own this in the end (I can push on it, but I don't have sufficient technical expertise to lead it), but I think all of the people that have posted to either https://internals.rust-lang.org/t/repr-interoperable-2024/16763 or https://internals.rust-lang.org/t/for-a-library-author-how-maintenance-of-a-stable-abi-would-look-like/16769 would be interested.
What happens now?
This issue is part of the lang-team initiative process. Once this issue is filed, a Zulip topic will be opened for discussion, and the lang-team will review open proposals in its weekly triage meetings. You should receive feedback within a week or two.
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.