From 31d03e7356ec86d0640f1a2534ae1d10175f3e93 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Tue, 16 Jun 2026 10:02:54 -0400 Subject: [PATCH 1/6] Distinguish `repr(C)` ZSTs from others in ABI compatibility rules --- library/core/src/primitive_docs.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index c6fbe52ee887b..7c09359fe496f 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -1841,7 +1841,14 @@ mod prim_ref {} /// call will be valid ABI-wise. The callee receives the result of transmuting the function pointer /// from `fn()` to `fn(i32)`; that transmutation is itself a well-defined operation, it's just /// almost certainly UB to later call that function pointer.) -/// - Any two types with size 0 and alignment 1 are ABI-compatible. +/// - Any two types fulfilling all the following conditions are ABI-compatible; +/// such types are said to have "trivial ABI": +/// - Size 0 +/// - Alignment 1 +/// - Not `repr(C)` +/// - Not a `repr(transparent)` wrapper around a type that fails to satisfy these conditions +/// - A `repr(transparent)` type is ABI-compatible with its unique field that does not have trivial ABI +/// (as defined above). If there is no such field, the type has trivial ABI. /// - A `repr(transparent)` type `T` is ABI-compatible with its unique non-trivial field, i.e., the /// unique field that doesn't have size 0 and alignment 1 (if there is such a field). /// - `i32` is ABI-compatible with `NonZero`, and similar for all other integer types. From 34de188f14dbd6190a8e83c2bd05d904f56ccb3d Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Fri, 19 Jun 2026 14:54:37 -0400 Subject: [PATCH 2/6] Make most arrays non-trivial --- library/core/src/primitive_docs.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index 7c09359fe496f..7946c3bcefdb0 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -1847,6 +1847,7 @@ mod prim_ref {} /// - Alignment 1 /// - Not `repr(C)` /// - Not a `repr(transparent)` wrapper around a type that fails to satisfy these conditions +/// - Not an array whose element type fails to satisfy these conditions /// - A `repr(transparent)` type is ABI-compatible with its unique field that does not have trivial ABI /// (as defined above). If there is no such field, the type has trivial ABI. /// - A `repr(transparent)` type `T` is ABI-compatible with its unique non-trivial field, i.e., the From 41e5d136020c6ec910c938ed2feb745348fec8fc Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Thu, 2 Jul 2026 10:29:18 -0400 Subject: [PATCH 3/6] Improve wording --- library/core/src/primitive_docs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index 7946c3bcefdb0..a66703b321eca 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -1846,8 +1846,8 @@ mod prim_ref {} /// - Size 0 /// - Alignment 1 /// - Not `repr(C)` -/// - Not a `repr(transparent)` wrapper around a type that fails to satisfy these conditions -/// - Not an array whose element type fails to satisfy these conditions +/// - If `repr(transparent)`, all field types must have trivial ABI +/// - If an array, the element type must have trivial ABI /// - A `repr(transparent)` type is ABI-compatible with its unique field that does not have trivial ABI /// (as defined above). If there is no such field, the type has trivial ABI. /// - A `repr(transparent)` type `T` is ABI-compatible with its unique non-trivial field, i.e., the From a01211b7bd4a7a88872f1bda141382682b513a1e Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Thu, 2 Jul 2026 13:15:33 -0400 Subject: [PATCH 4/6] Future-proofing --- library/core/src/primitive_docs.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index a66703b321eca..a2eabbb6a0e89 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -1848,6 +1848,8 @@ mod prim_ref {} /// - Not `repr(C)` /// - If `repr(transparent)`, all field types must have trivial ABI /// - If an array, the element type must have trivial ABI +/// - Note: additional conditions may be added in future versions of Rust, +/// to exclude types newly introduced in those versions /// - A `repr(transparent)` type is ABI-compatible with its unique field that does not have trivial ABI /// (as defined above). If there is no such field, the type has trivial ABI. /// - A `repr(transparent)` type `T` is ABI-compatible with its unique non-trivial field, i.e., the From a9bed71ec9b36d1f82cdfb12e24605c60961ae9d Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Thu, 2 Jul 2026 22:13:39 -0400 Subject: [PATCH 5/6] Improve wording Co-authored-by: Brian Smith Co-authored-by: Ralf Jung --- library/core/src/primitive_docs.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index a2eabbb6a0e89..8f13bcd0256e4 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -1843,17 +1843,17 @@ mod prim_ref {} /// almost certainly UB to later call that function pointer.) /// - Any two types fulfilling all the following conditions are ABI-compatible; /// such types are said to have "trivial ABI": -/// - Size 0 -/// - Alignment 1 -/// - Not `repr(C)` -/// - If `repr(transparent)`, all field types must have trivial ABI -/// - If an array, the element type must have trivial ABI -/// - Note: additional conditions may be added in future versions of Rust, -/// to exclude types newly introduced in those versions +/// - It has size 0. +/// - It has alignment 1. +/// - One of the following apply: +/// - It is a `repr(Rust)` (implicitly or explicitly, possibly with additional flags such as `packed`) `struct`, `enum`, `union` (regardless of its fields). +/// - It is a [tuple][prim_tuple] (regardless of its fields, and including [`()`][prim_unit]). +/// - It is a `repr(transparent)` `struct`, `enum`, or `union`, and all fields have trivial ABI. +/// - It is an array, and its element type has trivial ABI. (Arrays of length 0 are not exempt from this requirement.) +/// - It is [the never type `!`][prim_never]. +/// - It is a function item type or closure type. /// - A `repr(transparent)` type is ABI-compatible with its unique field that does not have trivial ABI -/// (as defined above). If there is no such field, the type has trivial ABI. -/// - A `repr(transparent)` type `T` is ABI-compatible with its unique non-trivial field, i.e., the -/// unique field that doesn't have size 0 and alignment 1 (if there is such a field). +/// (as defined above), if such a field exists. /// - `i32` is ABI-compatible with `NonZero`, and similar for all other integer types. /// - If `T` is guaranteed to be subject to the [null pointer /// optimization](option/index.html#representation), and `E` is an enum satisfying the following From 3e86bb00f5f3c54c3070c614e036d58891551489 Mon Sep 17 00:00:00 2001 From: Jules Bertholet Date: Sat, 4 Jul 2026 10:39:08 -0400 Subject: [PATCH 6/6] Avoid negation Co-authored-by: Ralf Jung --- library/core/src/primitive_docs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/primitive_docs.rs b/library/core/src/primitive_docs.rs index 8f13bcd0256e4..fb4a20eec3a67 100644 --- a/library/core/src/primitive_docs.rs +++ b/library/core/src/primitive_docs.rs @@ -1849,7 +1849,7 @@ mod prim_ref {} /// - It is a `repr(Rust)` (implicitly or explicitly, possibly with additional flags such as `packed`) `struct`, `enum`, `union` (regardless of its fields). /// - It is a [tuple][prim_tuple] (regardless of its fields, and including [`()`][prim_unit]). /// - It is a `repr(transparent)` `struct`, `enum`, or `union`, and all fields have trivial ABI. -/// - It is an array, and its element type has trivial ABI. (Arrays of length 0 are not exempt from this requirement.) +/// - It is an array, and its element type has trivial ABI. (This requirement applies even to arrays of length 0.) /// - It is [the never type `!`][prim_never]. /// - It is a function item type or closure type. /// - A `repr(transparent)` type is ABI-compatible with its unique field that does not have trivial ABI