Skip to content

Commit

Permalink
lang: Strengthen RFC 3391 guarantees to match T-lang consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Oct 21, 2024
1 parent 1f81242 commit 830ff7a
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/ui/rfcs/rfc-3391-result-ffi-guarantees.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
//@ build-pass
/*!
The guarantees in RFC 3391 were strengthened as a result of the 2024 Oct 09 T-lang meeting[^1]
following the precedent of T-lang's guaranteeing[^2] ABI compatibility for "Option-like" enums[^2].
We now guarantee ABI compatibility for enums that conform to these rules described by scottmcm:
* The enum `E` has exactly two variants.
* One variant has exactly one field, of type `T`.
* `T` is a `rustc_nonnull_optimization_guaranteed` type.
* All fields of the other variant are 1-ZSTs.
Where "all" fields includes "there aren't any fields, so they're vacuously all 1-ZSTs".
Note: "1-ZST" means a type of size 0 and alignment 1.
The reason alignment of the zero-sized type matters is it can affect the alignment of the enum,
which also will affect its size if the enum has a non-zero size.
[^1]: <https://github.com/rust-lang/rust/pull/130628#issuecomment-2402761599>
[^2]: <https://github.com/rust-lang/rust/pull/60300#issuecomment-487000474>
*/

#![allow(dead_code)]
#![deny(improper_ctypes)]
#![feature(ptr_internals)]
Expand Down Expand Up @@ -59,4 +82,52 @@ extern "C" {
fn result_1zst_exhaustive_no_field_e(x: Result<NoField, num::NonZero<u8>>);
}

// Custom "Result-like" enum for testing custom "Option-like" types are also accepted,
enum Either<L, R> {
Left(L),
Right(R),
}

extern "C" {
fn either_ref_t(x: Either<&'static u8, ()>);
fn either_fn_t(x: Either<extern "C" fn(), ()>);
fn either_nonnull_t(x: Either<std::ptr::NonNull<u8>, ()>);
fn either_unique_t(x: Either<std::ptr::Unique<u8>, ()>);
fn either_nonzero_u8_t(x: Either<num::NonZero<u8>, ()>);
fn either_nonzero_u16_t(x: Either<num::NonZero<u16>, ()>);
fn either_nonzero_u32_t(x: Either<num::NonZero<u32>, ()>);
fn either_nonzero_u64_t(x: Either<num::NonZero<u64>, ()>);
fn either_nonzero_usize_t(x: Either<num::NonZero<usize>, ()>);
fn either_nonzero_i8_t(x: Either<num::NonZero<i8>, ()>);
fn either_nonzero_i16_t(x: Either<num::NonZero<i16>, ()>);
fn either_nonzero_i32_t(x: Either<num::NonZero<i32>, ()>);
fn either_nonzero_i64_t(x: Either<num::NonZero<i64>, ()>);
fn either_nonzero_isize_t(x: Either<num::NonZero<isize>, ()>);
fn either_transparent_struct_t(x: Either<TransparentStruct<num::NonZero<u8>>, ()>);
fn either_transparent_enum_t(x: Either<TransparentEnum<num::NonZero<u8>>, ()>);
fn either_phantom_t(x: Either<num::NonZero<u8>, std::marker::PhantomData<()>>);
fn either_1zst_exhaustive_no_variant_t(x: Either<num::NonZero<u8>, Z>);
fn either_1zst_exhaustive_no_field_t(x: Either<num::NonZero<u8>, NoField>);

fn either_ref_e(x: Either<(), &'static u8>);
fn either_fn_e(x: Either<(), extern "C" fn()>);
fn either_nonnull_e(x: Either<(), std::ptr::NonNull<u8>>);
fn either_unique_e(x: Either<(), std::ptr::Unique<u8>>);
fn either_nonzero_u8_e(x: Either<(), num::NonZero<u8>>);
fn either_nonzero_u16_e(x: Either<(), num::NonZero<u16>>);
fn either_nonzero_u32_e(x: Either<(), num::NonZero<u32>>);
fn either_nonzero_u64_e(x: Either<(), num::NonZero<u64>>);
fn either_nonzero_usize_e(x: Either<(), num::NonZero<usize>>);
fn either_nonzero_i8_e(x: Either<(), num::NonZero<i8>>);
fn either_nonzero_i16_e(x: Either<(), num::NonZero<i16>>);
fn either_nonzero_i32_e(x: Either<(), num::NonZero<i32>>);
fn either_nonzero_i64_e(x: Either<(), num::NonZero<i64>>);
fn either_nonzero_isize_e(x: Either<(), num::NonZero<isize>>);
fn either_transparent_struct_e(x: Either<(), TransparentStruct<num::NonZero<u8>>>);
fn either_transparent_enum_e(x: Either<(), TransparentEnum<num::NonZero<u8>>>);
fn either_phantom_e(x: Either<num::NonZero<u8>, std::marker::PhantomData<()>>);
fn either_1zst_exhaustive_no_variant_e(x: Either<Z, num::NonZero<u8>>);
fn either_1zst_exhaustive_no_field_e(x: Either<NoField, num::NonZero<u8>>);
}

pub fn main() {}

0 comments on commit 830ff7a

Please sign in to comment.