Skip to content

Commit e3792e7

Browse files
borsgitbot
authored and
gitbot
committed
Auto merge of rust-lang#134177 - matthiaskrgr:rollup-hgp8q60, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - rust-lang#132975 (De-duplicate and improve definition of core::ffi::c_char) - rust-lang#133598 (Change `GetManyMutError` to match T-libs-api decision) - rust-lang#134148 (add comments in check_expr_field) - rust-lang#134163 (coverage: Rearrange the code for embedding per-function coverage metadata) - rust-lang#134165 (wasm(32|64): update alignment string) - rust-lang#134170 (Subtree update of `rust-analyzer`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c765a34 + a82af82 commit e3792e7

File tree

4 files changed

+114
-81
lines changed

4 files changed

+114
-81
lines changed

alloc/src/slice.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub use core::slice::ArrayChunksMut;
2727
pub use core::slice::ArrayWindows;
2828
#[stable(feature = "inherent_ascii_escape", since = "1.60.0")]
2929
pub use core::slice::EscapeAscii;
30+
#[unstable(feature = "get_many_mut", issue = "104642")]
31+
pub use core::slice::GetManyMutError;
3032
#[stable(feature = "slice_get_slice", since = "1.28.0")]
3133
pub use core::slice::SliceIndex;
3234
#[cfg(not(no_global_oom_handling))]

core/src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1076,4 +1076,4 @@ impl Error for crate::time::TryFromFloatSecsError {}
10761076
impl Error for crate::ffi::FromBytesUntilNulError {}
10771077

10781078
#[unstable(feature = "get_many_mut", issue = "104642")]
1079-
impl<const N: usize> Error for crate::slice::GetManyMutError<N> {}
1079+
impl Error for crate::slice::GetManyMutError {}

core/src/ffi/mod.rs

+80-53
Original file line numberDiff line numberDiff line change
@@ -91,59 +91,86 @@ pub type c_ssize_t = isize;
9191

9292
mod c_char_definition {
9393
cfg_if! {
94-
// These are the targets on which c_char is unsigned.
95-
if #[cfg(any(
96-
all(
97-
target_os = "linux",
98-
any(
99-
target_arch = "aarch64",
100-
target_arch = "arm",
101-
target_arch = "hexagon",
102-
target_arch = "powerpc",
103-
target_arch = "powerpc64",
104-
target_arch = "s390x",
105-
target_arch = "riscv64",
106-
target_arch = "riscv32",
107-
target_arch = "csky"
108-
)
109-
),
110-
all(target_os = "android", any(target_arch = "aarch64", target_arch = "arm")),
111-
all(target_os = "l4re", target_arch = "x86_64"),
112-
all(
113-
any(target_os = "freebsd", target_os = "openbsd", target_os = "rtems"),
114-
any(
115-
target_arch = "aarch64",
116-
target_arch = "arm",
117-
target_arch = "powerpc",
118-
target_arch = "powerpc64",
119-
target_arch = "riscv64"
120-
)
121-
),
122-
all(
123-
target_os = "netbsd",
124-
any(
125-
target_arch = "aarch64",
126-
target_arch = "arm",
127-
target_arch = "powerpc",
128-
target_arch = "riscv64"
129-
)
130-
),
131-
all(
132-
target_os = "vxworks",
133-
any(
134-
target_arch = "aarch64",
135-
target_arch = "arm",
136-
target_arch = "powerpc64",
137-
target_arch = "powerpc"
138-
)
139-
),
140-
all(
141-
target_os = "fuchsia",
142-
any(target_arch = "aarch64", target_arch = "riscv64")
143-
),
144-
all(target_os = "nto", target_arch = "aarch64"),
145-
target_os = "horizon",
146-
target_os = "aix",
94+
// These are the targets on which c_char is unsigned. Usually the
95+
// signedness is the same for all target_os values on a given architecture
96+
// but there are some exceptions (see isSignedCharDefault() in clang).
97+
//
98+
// aarch64:
99+
// Section 10 "Arm C and C++ language mappings" in Procedure Call Standard for the Arm®
100+
// 64-bit Architecture (AArch64) says C/C++ char is unsigned byte.
101+
// https://github.com/ARM-software/abi-aa/blob/2024Q3/aapcs64/aapcs64.rst#arm-c-and-c-language-mappings
102+
// arm:
103+
// Section 8 "Arm C and C++ Language Mappings" in Procedure Call Standard for the Arm®
104+
// Architecture says C/C++ char is unsigned byte.
105+
// https://github.com/ARM-software/abi-aa/blob/2024Q3/aapcs32/aapcs32.rst#arm-c-and-c-language-mappings
106+
// csky:
107+
// Section 2.1.2 "Primary Data Type" in C-SKY V2 CPU Applications Binary Interface
108+
// Standards Manual says ANSI C char is unsigned byte.
109+
// https://github.com/c-sky/csky-doc/blob/9f7121f7d40970ba5cc0f15716da033db2bb9d07/C-SKY_V2_CPU_Applications_Binary_Interface_Standards_Manual.pdf
110+
// Note: this doesn't seem to match Clang's default (https://github.com/rust-lang/rust/issues/129945).
111+
// hexagon:
112+
// Section 3.1 "Basic data type" in Qualcomm Hexagon™ Application
113+
// Binary Interface User Guide says "By default, the `char` data type is unsigned."
114+
// https://docs.qualcomm.com/bundle/publicresource/80-N2040-23_REV_K_Qualcomm_Hexagon_Application_Binary_Interface_User_Guide.pdf
115+
// msp430:
116+
// Section 2.1 "Basic Types" in MSP430 Embedded Application Binary
117+
// Interface says "The char type is unsigned by default".
118+
// https://www.ti.com/lit/an/slaa534a/slaa534a.pdf
119+
// Note: this doesn't seem to match Clang's default (https://github.com/rust-lang/rust/issues/129945).
120+
// powerpc/powerpc64:
121+
// - PPC32 SysV: "Table 3-1 Scalar Types" in System V Application Binary Interface PowerPC
122+
// Processor Supplement says ANSI C char is unsigned byte
123+
// https://refspecs.linuxfoundation.org/elf/elfspec_ppc.pdf
124+
// - PPC64 ELFv1: Section 3.1.4 "Fundamental Types" in 64-bit PowerPC ELF Application
125+
// Binary Interface Supplement 1.9 says ANSI C is unsigned byte
126+
// https://refspecs.linuxfoundation.org/ELF/ppc64/PPC-elf64abi.html#FUND-TYPE
127+
// - PPC64 ELFv2: Section 2.1.2.2 "Fundamental Types" in 64-Bit ELF V2 ABI Specification
128+
// says char is unsigned byte
129+
// https://openpowerfoundation.org/specifications/64bitelfabi/
130+
// - AIX: XL C for AIX Language Reference says "By default, char behaves like an unsigned char."
131+
// https://www.ibm.com/docs/en/xl-c-aix/13.1.3?topic=specifiers-character-types
132+
// riscv32/riscv64:
133+
// C/C++ type representations section in RISC-V Calling Conventions
134+
// page in RISC-V ELF psABI Document says "char is unsigned."
135+
// https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/draft-20240829-13bfa9f54634cb60d86b9b333e109f077805b4b3/riscv-cc.adoc#cc-type-representations
136+
// s390x:
137+
// - ELF: "Table 1.1.: Scalar types" in ELF Application Binary Interface s390x Supplement
138+
// Version 1.6.1 categorize ISO C char in unsigned integer
139+
// https://github.com/IBM/s390x-abi/releases/tag/v1.6.1
140+
// - z/OS: XL C/C++ Language Reference says: "By default, char behaves like an unsigned char."
141+
// https://www.ibm.com/docs/en/zos/3.1.0?topic=specifiers-character-types
142+
// Xtensa:
143+
// - "The char type is unsigned by default for Xtensa processors."
144+
//
145+
// On the following operating systems, c_char is signed by default, regardless of architecture.
146+
// Darwin (macOS, iOS, etc.):
147+
// Apple targets' c_char is signed by default even on arm
148+
// https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Handle-data-types-and-data-alignment-properly
149+
// Windows:
150+
// Windows MSVC C++ Language Reference says "Microsoft-specific: Variables of type char
151+
// are promoted to int as if from type signed char by default, unless the /J compilation
152+
// option is used."
153+
// https://learn.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp?view=msvc-170#character-types)
154+
// L4RE:
155+
// The kernel builds with -funsigned-char on all targets (but useserspace follows the
156+
// architecture defaults). As we only have a target for userspace apps so there are no
157+
// special cases for L4RE below.
158+
if #[cfg(all(
159+
not(windows),
160+
not(target_vendor = "apple"),
161+
any(
162+
target_arch = "aarch64",
163+
target_arch = "arm",
164+
target_arch = "csky",
165+
target_arch = "hexagon",
166+
target_arch = "msp430",
167+
target_arch = "powerpc",
168+
target_arch = "powerpc64",
169+
target_arch = "riscv64",
170+
target_arch = "riscv32",
171+
target_arch = "s390x",
172+
target_arch = "xtensa",
173+
)
147174
))] {
148175
pub type c_char = u8;
149176
} else {

core/src/slice/mod.rs

+31-27
Original file line numberDiff line numberDiff line change
@@ -4629,13 +4629,11 @@ impl<T> [T] {
46294629
pub fn get_many_mut<I, const N: usize>(
46304630
&mut self,
46314631
indices: [I; N],
4632-
) -> Result<[&mut I::Output; N], GetManyMutError<N>>
4632+
) -> Result<[&mut I::Output; N], GetManyMutError>
46334633
where
46344634
I: GetManyMutIndex + SliceIndex<Self>,
46354635
{
4636-
if !get_many_check_valid(&indices, self.len()) {
4637-
return Err(GetManyMutError { _private: () });
4638-
}
4636+
get_many_check_valid(&indices, self.len())?;
46394637
// SAFETY: The `get_many_check_valid()` call checked that all indices
46404638
// are disjunct and in bounds.
46414639
unsafe { Ok(self.get_many_unchecked_mut(indices)) }
@@ -4978,53 +4976,59 @@ impl<T, const N: usize> SlicePattern for [T; N] {
49784976
/// This will do `binomial(N + 1, 2) = N * (N + 1) / 2 = 0, 1, 3, 6, 10, ..`
49794977
/// comparison operations.
49804978
#[inline]
4981-
fn get_many_check_valid<I: GetManyMutIndex, const N: usize>(indices: &[I; N], len: usize) -> bool {
4979+
fn get_many_check_valid<I: GetManyMutIndex, const N: usize>(
4980+
indices: &[I; N],
4981+
len: usize,
4982+
) -> Result<(), GetManyMutError> {
49824983
// NB: The optimizer should inline the loops into a sequence
49834984
// of instructions without additional branching.
4984-
let mut valid = true;
49854985
for (i, idx) in indices.iter().enumerate() {
4986-
valid &= idx.is_in_bounds(len);
4986+
if !idx.is_in_bounds(len) {
4987+
return Err(GetManyMutError::IndexOutOfBounds);
4988+
}
49874989
for idx2 in &indices[..i] {
4988-
valid &= !idx.is_overlapping(idx2);
4990+
if idx.is_overlapping(idx2) {
4991+
return Err(GetManyMutError::OverlappingIndices);
4992+
}
49894993
}
49904994
}
4991-
valid
4995+
Ok(())
49924996
}
49934997

4994-
/// The error type returned by [`get_many_mut<N>`][`slice::get_many_mut`].
4998+
/// The error type returned by [`get_many_mut`][`slice::get_many_mut`].
49954999
///
49965000
/// It indicates one of two possible errors:
49975001
/// - An index is out-of-bounds.
4998-
/// - The same index appeared multiple times in the array.
5002+
/// - The same index appeared multiple times in the array
5003+
/// (or different but overlapping indices when ranges are provided).
49995004
///
50005005
/// # Examples
50015006
///
50025007
/// ```
50035008
/// #![feature(get_many_mut)]
5009+
/// use std::slice::GetManyMutError;
50045010
///
50055011
/// let v = &mut [1, 2, 3];
5006-
/// assert!(v.get_many_mut([0, 999]).is_err());
5007-
/// assert!(v.get_many_mut([1, 1]).is_err());
5012+
/// assert_eq!(v.get_many_mut([0, 999]), Err(GetManyMutError::IndexOutOfBounds));
5013+
/// assert_eq!(v.get_many_mut([1, 1]), Err(GetManyMutError::OverlappingIndices));
50085014
/// ```
50095015
#[unstable(feature = "get_many_mut", issue = "104642")]
5010-
// NB: The N here is there to be forward-compatible with adding more details
5011-
// to the error type at a later point
5012-
#[derive(Clone, PartialEq, Eq)]
5013-
pub struct GetManyMutError<const N: usize> {
5014-
_private: (),
5016+
#[derive(Debug, Clone, PartialEq, Eq)]
5017+
pub enum GetManyMutError {
5018+
/// An index provided was out-of-bounds for the slice.
5019+
IndexOutOfBounds,
5020+
/// Two indices provided were overlapping.
5021+
OverlappingIndices,
50155022
}
50165023

50175024
#[unstable(feature = "get_many_mut", issue = "104642")]
5018-
impl<const N: usize> fmt::Debug for GetManyMutError<N> {
5025+
impl fmt::Display for GetManyMutError {
50195026
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5020-
f.debug_struct("GetManyMutError").finish_non_exhaustive()
5021-
}
5022-
}
5023-
5024-
#[unstable(feature = "get_many_mut", issue = "104642")]
5025-
impl<const N: usize> fmt::Display for GetManyMutError<N> {
5026-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5027-
fmt::Display::fmt("an index is out of bounds or appeared multiple times in the array", f)
5027+
let msg = match self {
5028+
GetManyMutError::IndexOutOfBounds => "an index is out of bounds",
5029+
GetManyMutError::OverlappingIndices => "there were overlapping indices",
5030+
};
5031+
fmt::Display::fmt(msg, f)
50285032
}
50295033
}
50305034

0 commit comments

Comments
 (0)