Skip to content

Commit

Permalink
Put the RFC behind a feature gate result_ffi_guarantees
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterAwesome committed Apr 24, 2024
1 parent 437ca26 commit ed532cc
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 28 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ declare_features! (
(incomplete, repr128, "1.16.0", Some(56071)),
/// Allows `repr(simd)` and importing the various simd intrinsics.
(unstable, repr_simd, "1.4.0", Some(27731)),
/// Allows enums like Result<T, E> to be used across FFI, if T's niche value can
/// be used to describe E or vise-versa.
(unstable, result_ffi_guarantees, "CURRENT_RUSTC_VERSION", Some(110503)),
/// Allows bounding the return type of AFIT/RPITIT.
(incomplete, return_type_notation, "1.70.0", Some(109417)),
/// Allows `extern "rust-cold"`.
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,10 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
[var_one, var_two] => match (&var_one.fields.raw[..], &var_two.fields.raw[..]) {
([], [field]) | ([field], []) => field.ty(tcx, args),
([field1], [field2]) => {
if !tcx.features().result_ffi_guarantees {
return None;
}

let ty1 = field1.ty(tcx, args);
let ty2 = field2.ty(tcx, args);

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,7 @@ symbols! {
require,
residual,
result,
result_ffi_guarantees,
resume,
return_position_impl_trait_in_trait,
return_type_notation,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# `result_ffi_guarantees`

The tracking issue for this feature is: [#110503]

[#110503]: https://github.com/rust-lang/rust/issues/110503

------------------------

This feature adds the possibility of using `Result<T, E>` in FFI if T's niche
value can be used to describe E or vise-versa.

See [RFC 3391] for more information.

[RFC 3391]: https://github.com/rust-lang/rfcs/blob/master/text/3391-result_ffi_guarantees.md
99 changes: 99 additions & 0 deletions tests/ui/feature-gates/feature-gate-result_ffi_guarantees.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#![allow(dead_code)]
#![deny(improper_ctypes)]
#![feature(ptr_internals)]

use std::num;

enum Z {}

#[repr(transparent)]
struct TransparentStruct<T>(T, std::marker::PhantomData<Z>);

#[repr(transparent)]
enum TransparentEnum<T> {
Variant(T, std::marker::PhantomData<Z>),
}

struct NoField;

extern "C" {
fn result_ref_t(x: Result<&'static u8, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_fn_t(x: Result<extern "C" fn(), ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonnull_t(x: Result<std::ptr::NonNull<u8>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_unique_t(x: Result<std::ptr::Unique<u8>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_u8_t(x: Result<num::NonZero<u8>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_u16_t(x: Result<num::NonZero<u16>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_u32_t(x: Result<num::NonZero<u32>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_u64_t(x: Result<num::NonZero<u64>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_usize_t(x: Result<num::NonZero<usize>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_i8_t(x: Result<num::NonZero<i8>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_i16_t(x: Result<num::NonZero<i16>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_i32_t(x: Result<num::NonZero<i32>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_i64_t(x: Result<num::NonZero<i64>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_isize_t(x: Result<num::NonZero<isize>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_transparent_struct_t(x: Result<TransparentStruct<num::NonZero<u8>>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_transparent_enum_t(x: Result<TransparentEnum<num::NonZero<u8>>, ()>);
//~^ ERROR `extern` block uses type `Result
fn result_phantom_t(x: Result<num::NonZero<u8>, std::marker::PhantomData<()>>);
//~^ ERROR `extern` block uses type `Result
fn result_1zst_exhaustive_no_variant_t(x: Result<num::NonZero<u8>, Z>);
//~^ ERROR `extern` block uses type `Result
fn result_1zst_exhaustive_no_field_t(x: Result<num::NonZero<u8>, NoField>);
//~^ ERROR `extern` block uses type `Result

fn result_ref_e(x: Result<(), &'static u8>);
//~^ ERROR `extern` block uses type `Result
fn result_fn_e(x: Result<(), extern "C" fn()>);
//~^ ERROR `extern` block uses type `Result
fn result_nonnull_e(x: Result<(), std::ptr::NonNull<u8>>);
//~^ ERROR `extern` block uses type `Result
fn result_unique_e(x: Result<(), std::ptr::Unique<u8>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_u8_e(x: Result<(), num::NonZero<u8>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_u16_e(x: Result<(), num::NonZero<u16>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_u32_e(x: Result<(), num::NonZero<u32>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_u64_e(x: Result<(), num::NonZero<u64>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_usize_e(x: Result<(), num::NonZero<usize>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_i8_e(x: Result<(), num::NonZero<i8>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_i16_e(x: Result<(), num::NonZero<i16>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_i32_e(x: Result<(), num::NonZero<i32>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_i64_e(x: Result<(), num::NonZero<i64>>);
//~^ ERROR `extern` block uses type `Result
fn result_nonzero_isize_e(x: Result<(), num::NonZero<isize>>);
//~^ ERROR `extern` block uses type `Result
fn result_transparent_struct_e(x: Result<(), TransparentStruct<num::NonZero<u8>>>);
//~^ ERROR `extern` block uses type `Result
fn result_transparent_enum_e(x: Result<(), TransparentEnum<num::NonZero<u8>>>);
//~^ ERROR `extern` block uses type `Result
fn result_phantom_e(x: Result<num::NonZero<u8>, std::marker::PhantomData<()>>);
//~^ ERROR `extern` block uses type `Result
fn result_1zst_exhaustive_no_variant_e(x: Result<Z, num::NonZero<u8>>);
//~^ ERROR `extern` block uses type `Result
fn result_1zst_exhaustive_no_field_e(x: Result<NoField, num::NonZero<u8>>);
//~^ ERROR `extern` block uses type `Result
}

pub fn main() {}
Loading

0 comments on commit ed532cc

Please sign in to comment.