Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions library/core/src/io/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,16 @@ impl CustomOwner {
///
/// This is an [`i32`] on all currently supported platforms, but platforms
/// added in the future (such as UEFI) may use a different primitive type like
/// [`usize`]. Use `as` or [`into`] conversions where applicable to ensure maximum
/// portability.
/// [`usize`] or [`i16`]. Use `as` or [`into`] conversions where applicable to
/// ensure maximum portability.
///
/// [`into`]: Into::into
#[unstable(feature = "raw_os_error_ty", issue = "107792")]
pub type RawOsError = cfg_select! {
target_os = "uefi" => usize,
// For 16-bit AVR and MSP430, i16 is equivalent to c_int.
// Using i16 to be explicit.
target_pointer_width = "16" => i16,
Comment thread
bushrat011899 marked this conversation as resolved.
_ => i32,
};
Comment thread
bushrat011899 marked this conversation as resolved.

Expand Down
11 changes: 11 additions & 0 deletions library/coretests/tests/io/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use core::io::RawOsError;

/// On all targets to date, [`RawOsError`] is equivalent to a `c_int`, with the
/// notable exception of UEFI, where it is instead defined as `usize`.
#[test]
fn raw_os_error_ffi_guarantees() {
let _: RawOsError = cfg_select! {
target_os = "uefi" => 0 as usize,

@lygstate lygstate Aug 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, the test already declare that.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah tgross35's suggestion to use explicitly sized types is compared against the c_int assumption just so if a target is ever added that violates it, we know about it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw I was thinking to put it in a `const _: () = { /* ... */ } so the tests don't actually have to be run (I don't think we run coretests on these 16-bit platforms), but it doesn't matter much

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not a huge fan of that particular const trick. The test as written is still compile-time evaluated, it'll just hopefully give a nicer message if/when it fails.

_ => 0 as core::ffi::c_int,
};
}
1 change: 1 addition & 0 deletions library/coretests/tests/io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod borrowed_buf;
mod error;
mod io_slice;
1 change: 1 addition & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
#![feature(pointer_is_aligned_to)]
#![feature(portable_simd)]
#![feature(ptr_metadata)]
#![feature(raw_os_error_ty)]
#![feature(rustc_attrs)]
#![feature(signed_bigint_helpers)]
#![feature(slice_from_ptr_range)]
Expand Down
Loading