-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Support Result<T, E> across FFI when niche optimization can be used #122253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fc37633
Support Result<T, E> across FFI when niche optimization can be used
MasterAwesome b3e6d52
We don't need to check for non-exhaustive fields
MasterAwesome 08b85a1
Don't lint niche optimized variants in enums
MasterAwesome 223d5eb
Add tests
MasterAwesome 014ddac
Disallow single-variant enums
MasterAwesome b3f6511
Add a test against Result<(), ()>
MasterAwesome 764f64f
Reword `is_niche_optimization_candidate` doc
MasterAwesome 437ca26
Remove comment out of RFC's scope
MasterAwesome ed532cc
Put the RFC behind a feature gate `result_ffi_guarantees`
MasterAwesome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/doc/unstable-book/src/language-features/result-ffi-guarantees.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
99
tests/ui/feature-gates/feature-gate-result_ffi_guarantees.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.