From bbc212a14319e7a71f612eec4b1fd16989a62311 Mon Sep 17 00:00:00 2001 From: Zac Harrold Date: Wed, 5 Aug 2026 19:55:18 +1000 Subject: [PATCH] Default `RawOsError` to `i16` on 16-bit targets --- library/core/src/io/error.rs | 7 +++++-- library/coretests/tests/io/error.rs | 11 +++++++++++ library/coretests/tests/io/mod.rs | 1 + library/coretests/tests/lib.rs | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 library/coretests/tests/io/error.rs diff --git a/library/core/src/io/error.rs b/library/core/src/io/error.rs index 61abdb16d07da..502817c70bb3c 100644 --- a/library/core/src/io/error.rs +++ b/library/core/src/io/error.rs @@ -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, _ => i32, }; diff --git a/library/coretests/tests/io/error.rs b/library/coretests/tests/io/error.rs new file mode 100644 index 0000000000000..62f6f6cace8b3 --- /dev/null +++ b/library/coretests/tests/io/error.rs @@ -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, + _ => 0 as core::ffi::c_int, + }; +} diff --git a/library/coretests/tests/io/mod.rs b/library/coretests/tests/io/mod.rs index 7683131a54034..9341f1d85550b 100644 --- a/library/coretests/tests/io/mod.rs +++ b/library/coretests/tests/io/mod.rs @@ -1,2 +1,3 @@ mod borrowed_buf; +mod error; mod io_slice; diff --git a/library/coretests/tests/lib.rs b/library/coretests/tests/lib.rs index 06c5926ecc2f8..35327264ccd77 100644 --- a/library/coretests/tests/lib.rs +++ b/library/coretests/tests/lib.rs @@ -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)]