summaryrefslogtreecommitdiff
path: root/rust/zerocopy-derive
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2026-07-25 10:15:23 -0700
committerLinus Torvalds <torvalds@linux-foundation.org>2026-07-25 10:15:23 -0700
commit3dab139d4795f688e4f243e40c7474df00d329d9 (patch)
treea1a5e86f6d9e9ea1c74d6d04541f9be9e3c12d17 /rust/zerocopy-derive
parentef9ce800df95726978490534f9e2c14ca71858e8 (diff)
parent880c43b185ca52239e75bc546cc4f4d9154d0fed (diff)
Merge tag 'rust-fixes-7.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linuxHEADmaster
Pull rust fixes from Miguel Ojeda: "Toolchain and infrastructure: - 'zerocopy' crates: update to v0.8.54 to fix a modpost error under 'CONFIG_CC_OPTIMIZE_FOR_SIZE=y'. There are actually two updates in the PR: the one to v0.8.52 is fairly large and was originally not intended for a fixes PR, but the actual fix landed in the v0.8.54 one. Thus I included both here. The v0.8.52 update includes two things upstream added for us: '--cfg no_fp_fmt_parse' to avoid a local workaround, and the new 'most_traits' feature. The good news is that, after these updates, the delta with upstream is now trivial: only an identifier prefix change and the SPDX parentheses. - Fix an objtool warning by adding one more 'noreturn' function for Rust 1.99.0 (expected 2026-10-01). - Clean up new 'semicolon_in_expressions_from_macros' lint errors for Rust 1.99.0 (expected 2026-10-01). The lint can be allowed, but it will be a hard error at some point in the future anyway, so clean it up now. - Locally allow new 'suspicious_runtime_symbol_definitions' lint for Rust 1.98.0 (expected 2026-08-20). - Globally allow 'clippy::unwrap_or_default' lint since it relies on optimizations -- under 'CONFIG_CC_OPTIMIZE_FOR_SIZE=y' it does not work well. 'kernel' crate: - 'time' module: fix 'Delta::as_micros_ceil()' to round negative values correctly" * tag 'rust-fixes-7.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux: rust: time: fix as_micros_ceil() to round correctly for negative Delta rust: device: avoid trailing ; in printing macros objtool/rust: add one more `noreturn` Rust function for Rust 1.99.0 rust: zerocopy: update to v0.8.54 rust: zerocopy: update to v0.8.52 rust: allow `clippy::unwrap_or_default` globally rust: allow `suspicious_runtime_symbol_definitions` lint for Rust >= 1.98
Diffstat (limited to 'rust/zerocopy-derive')
-rw-r--r--rust/zerocopy-derive/README.md14
-rw-r--r--rust/zerocopy-derive/derive/from_bytes.rs2
-rw-r--r--rust/zerocopy-derive/derive/into_bytes.rs2
-rw-r--r--rust/zerocopy-derive/derive/known_layout.rs3
-rw-r--r--rust/zerocopy-derive/derive/mod.rs20
-rw-r--r--rust/zerocopy-derive/derive/try_from_bytes.rs6
-rw-r--r--rust/zerocopy-derive/derive/unaligned.rs2
-rw-r--r--rust/zerocopy-derive/lib.rs36
-rw-r--r--rust/zerocopy-derive/repr.rs2
-rw-r--r--rust/zerocopy-derive/util.rs34
10 files changed, 88 insertions, 33 deletions
diff --git a/rust/zerocopy-derive/README.md b/rust/zerocopy-derive/README.md
index 110f4a401778..d62c79804342 100644
--- a/rust/zerocopy-derive/README.md
+++ b/rust/zerocopy-derive/README.md
@@ -1,14 +1,14 @@
# `zerocopy-derive`
-These source files come from the Rust `zerocopy-derive` crate, version v0.8.50
-(released 2026-05-31), hosted in the <https://github.com/google/zerocopy>
+These source files come from the Rust `zerocopy-derive` crate, version v0.8.54
+(released 2026-07-08), hosted in the <https://github.com/google/zerocopy>
repository, licensed under "BSD-2-Clause OR Apache-2.0 OR MIT" and only
-modified to add the SPDX license identifiers and to remove the generation of
+modified to tweak the SPDX license identifiers and to remove the generation of
non-ASCII identifiers.
For copyright details, please see:
- https://github.com/google/zerocopy/blob/v0.8.50/README.md?plain=1
- https://github.com/google/zerocopy/blob/v0.8.50/LICENSE-BSD
- https://github.com/google/zerocopy/blob/v0.8.50/LICENSE-APACHE
- https://github.com/google/zerocopy/blob/v0.8.50/LICENSE-MIT
+ https://github.com/google/zerocopy/blob/v0.8.54/README.md?plain=1
+ https://github.com/google/zerocopy/blob/v0.8.54/LICENSE-BSD
+ https://github.com/google/zerocopy/blob/v0.8.54/LICENSE-APACHE
+ https://github.com/google/zerocopy/blob/v0.8.54/LICENSE-MIT
diff --git a/rust/zerocopy-derive/derive/from_bytes.rs b/rust/zerocopy-derive/derive/from_bytes.rs
index d693a63b7645..66d820f6ad4c 100644
--- a/rust/zerocopy-derive/derive/from_bytes.rs
+++ b/rust/zerocopy-derive/derive/from_bytes.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::{Span, TokenStream};
use syn::{
parse_quote, Data, DataEnum, DataStruct, DataUnion, Error, Expr, ExprLit, ExprUnary, Lit, UnOp,
diff --git a/rust/zerocopy-derive/derive/into_bytes.rs b/rust/zerocopy-derive/derive/into_bytes.rs
index ad52a6b45d28..0103a78d087f 100644
--- a/rust/zerocopy-derive/derive/into_bytes.rs
+++ b/rust/zerocopy-derive/derive/into_bytes.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{Data, DataEnum, DataStruct, DataUnion, Error, Type};
diff --git a/rust/zerocopy-derive/derive/known_layout.rs b/rust/zerocopy-derive/derive/known_layout.rs
index fddffd167c82..d0c4cecfff15 100644
--- a/rust/zerocopy-derive/derive/known_layout.rs
+++ b/rust/zerocopy-derive/derive/known_layout.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Data, Error, Type};
@@ -87,7 +87,6 @@ fn derive_known_layout_for_repr_c_struct<'a>(
};
let inner_extras = {
- let leading_fields_tys = leading_fields_tys.clone();
let methods = make_methods(*trailing_field_ty);
let (_, ty_generics, _) = ctx.ast.generics.split_for_impl();
diff --git a/rust/zerocopy-derive/derive/mod.rs b/rust/zerocopy-derive/derive/mod.rs
index 665ba7da55a8..b3839fcf73c9 100644
--- a/rust/zerocopy-derive/derive/mod.rs
+++ b/rust/zerocopy-derive/derive/mod.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
pub mod from_bytes;
pub mod into_bytes;
pub mod known_layout;
@@ -15,8 +15,8 @@ use crate::{
util::{Ctx, DataExt, FieldBounds, ImplBlockBuilder, Trait},
};
-pub(crate) fn derive_immutable(ctx: &Ctx, _top_level: Trait) -> TokenStream {
- match &ctx.ast.data {
+pub(crate) fn derive_immutable(ctx: &Ctx, _top_level: Trait) -> Result<TokenStream, Error> {
+ Ok(match &ctx.ast.data {
Data::Struct(strct) => {
ImplBlockBuilder::new(ctx, strct, Trait::Immutable, FieldBounds::ALL_SELF).build()
}
@@ -26,7 +26,7 @@ pub(crate) fn derive_immutable(ctx: &Ctx, _top_level: Trait) -> TokenStream {
Data::Union(unn) => {
ImplBlockBuilder::new(ctx, unn, Trait::Immutable, FieldBounds::ALL_SELF).build()
}
- }
+ })
}
pub(crate) fn derive_hash(ctx: &Ctx, _top_level: Trait) -> Result<TokenStream, Error> {
@@ -97,16 +97,20 @@ pub(crate) fn derive_split_at(ctx: &Ctx, _top_level: Trait) -> Result<TokenStrea
match &ctx.ast.data {
Data::Struct(_) => {}
Data::Enum(_) | Data::Union(_) => {
- return Err(Error::new(Span::call_site(), "can only be applied to structs"));
+ return ctx
+ .error_or_skip(Error::new(Span::call_site(), "can only be applied to structs"));
}
};
if repr.get_packed().is_some() {
- return Err(Error::new(Span::call_site(), "must not have #[repr(packed)] attribute"));
+ return ctx.error_or_skip(Error::new(
+ Span::call_site(),
+ "must not have #[repr(packed)] attribute",
+ ));
}
if !(repr.is_c() || repr.is_transparent()) {
- return Err(Error::new(
+ return ctx.error_or_skip(Error::new(
Span::call_site(),
"must have #[repr(C)] or #[repr(transparent)] in order to guarantee this type's layout is splitable",
));
@@ -116,7 +120,7 @@ pub(crate) fn derive_split_at(ctx: &Ctx, _top_level: Trait) -> Result<TokenStrea
let trailing_field = if let Some(((_, _, trailing_field), _)) = fields.split_last() {
trailing_field
} else {
- return Err(Error::new(Span::call_site(), "must at least one field"));
+ return ctx.error_or_skip(Error::new(Span::call_site(), "must at least one field"));
};
let zerocopy_crate = &ctx.zerocopy_crate;
diff --git a/rust/zerocopy-derive/derive/try_from_bytes.rs b/rust/zerocopy-derive/derive/try_from_bytes.rs
index a3e4a75631a5..44f083328786 100644
--- a/rust/zerocopy-derive/derive/try_from_bytes.rs
+++ b/rust/zerocopy-derive/derive/try_from_bytes.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
@@ -531,8 +531,8 @@ fn derive_has_field_struct_union(ctx: &Ctx, data: &dyn DataExt) -> TokenStream {
data,
Trait::ProjectField {
variant_id: variant_id.clone(),
- field: field.clone(),
- field_id: field_id.clone(),
+ field,
+ field_id,
invariants: parse_quote!((Aliasing, Alignment, #zerocopy_crate::invariant::Initialized)),
},
FieldBounds::None,
diff --git a/rust/zerocopy-derive/derive/unaligned.rs b/rust/zerocopy-derive/derive/unaligned.rs
index d6dea0a11f1e..7c97d62e2dcb 100644
--- a/rust/zerocopy-derive/derive/unaligned.rs
+++ b/rust/zerocopy-derive/derive/unaligned.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::{Span, TokenStream};
use syn::{Data, DataEnum, DataStruct, DataUnion, Error};
diff --git a/rust/zerocopy-derive/lib.rs b/rust/zerocopy-derive/lib.rs
index c517ea7db1eb..d387de368367 100644
--- a/rust/zerocopy-derive/lib.rs
+++ b/rust/zerocopy-derive/lib.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
// Copyright 2019 The Fuchsia Authors
//
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
@@ -129,6 +129,40 @@ derive!(ByteHash => derive_hash => crate::derive::derive_hash);
derive!(ByteEq => derive_eq => crate::derive::derive_eq);
derive!(SplitAt => derive_split_at => crate::derive::derive_split_at);
+#[cfg_attr(not(zerocopy_unstable_linux), doc(hidden))]
+#[proc_macro_derive(most_traits, attributes(zerocopy))]
+pub fn most_traits(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
+ let ast = syn::parse_macro_input!(ts as DeriveInput);
+ let ctx = match Ctx::try_from_derive_input(ast) {
+ Ok(ctx) => ctx,
+ Err(e) => return e.into_compile_error().into(),
+ }
+ .skip_on_error();
+
+ // top-level traits for which to attempt a derive
+ let derives: [(fn(&Ctx, Trait) -> _, _); 6] = [
+ (crate::derive::known_layout::derive, Trait::KnownLayout),
+ (crate::derive::derive_immutable, Trait::Immutable),
+ (crate::derive::from_bytes::derive_from_bytes, Trait::FromBytes),
+ (crate::derive::into_bytes::derive_into_bytes, Trait::IntoBytes),
+ (crate::derive::derive_split_at, Trait::SplitAt),
+ (crate::derive::unaligned::derive_unaligned, Trait::Unaligned),
+ ];
+
+ let mut tokens = proc_macro2::TokenStream::new();
+ for (derive, t) in derives {
+ tokens.extend(derive(&ctx, t))
+ }
+
+ // We wrap in `const_block` as a backstop in case any derive fails
+ // to wrap its output in `const_block` (and thus fails to annotate)
+ // with the full set of `#[allow(...)]` attributes).
+ let ts = const_block([Some(tokens)]);
+ #[cfg(test)]
+ crate::util::testutil::check_hygiene(ts.clone());
+ ts.into()
+}
+
/// Deprecated: prefer [`FromZeros`] instead.
#[deprecated(since = "0.8.0", note = "`FromZeroes` was renamed to `FromZeros`")]
#[doc(hidden)]
diff --git a/rust/zerocopy-derive/repr.rs b/rust/zerocopy-derive/repr.rs
index 74fd376d9fda..1525e94302d1 100644
--- a/rust/zerocopy-derive/repr.rs
+++ b/rust/zerocopy-derive/repr.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
// Copyright 2019 The Fuchsia Authors
//
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
diff --git a/rust/zerocopy-derive/util.rs b/rust/zerocopy-derive/util.rs
index 5ba5228e2a44..5c5e9d3bdcb8 100644
--- a/rust/zerocopy-derive/util.rs
+++ b/rust/zerocopy-derive/util.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
// Copyright 2019 The Fuchsia Authors
//
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
@@ -98,6 +98,11 @@ impl Ctx {
}
}
+ pub(crate) fn skip_on_error(mut self) -> Self {
+ self.skip_on_error = true;
+ self
+ }
+
pub(crate) fn core_path(&self) -> TokenStream {
let zerocopy_crate = &self.zerocopy_crate;
quote!(#zerocopy_crate::util::macro_util::core_reexport)
@@ -106,20 +111,21 @@ impl Ctx {
pub(crate) fn cfg_compile_error(&self) -> TokenStream {
// By checking both during the compilation of the proc macro *and* in
// the generated code, we ensure that `--cfg
- // zerocopy_unstable_derive_on_error` need only be passed *either* when
+ // zerocopy_unstable_linux` need only be passed *either* when
// compiling this crate *or* when compiling the user's crate. The former
// is preferable, but in some situations (such as when cross-compiling
// using `cargo build --target`), it doesn't get propagated to this
// crate's build by default.
- if cfg!(zerocopy_unstable_derive_on_error) {
+ if cfg!(zerocopy_unstable_linux) {
quote!()
} else if let Some(span) = self.on_error_span {
let core = self.core_path();
- let error_message = "`on_error` is experimental; pass '--cfg zerocopy_unstable_derive_on_error' to enable";
+ let error_message =
+ "`on_error` is experimental; pass '--cfg zerocopy_unstable_linux' to enable";
quote::quote_spanned! {span=>
#[allow(unused_attributes, unexpected_cfgs)]
const _: () = {
- #[cfg(not(zerocopy_unstable_derive_on_error))]
+ #[cfg(not(zerocopy_unstable_linux))]
#core::compile_error!(#error_message);
};
}
@@ -612,6 +618,20 @@ impl<'a> ImplBlockBuilder<'a> {
}
};
+ let zerocopy_bounds =
+ field_type_bounds
+ .into_iter()
+ .chain(padding_check_bound)
+ .chain(self_bounds)
+ .map(|bound| {
+ if self.ctx.skip_on_error {
+ parse_quote!(for<'zc> #bound)
+ } else {
+ bound.clone()
+ }
+ })
+ .collect::<Vec<_>>();
+
let bounds = self
.ctx
.ast
@@ -621,9 +641,7 @@ impl<'a> ImplBlockBuilder<'a> {
.map(|where_clause| where_clause.predicates.iter())
.into_iter()
.flatten()
- .chain(field_type_bounds.iter())
- .chain(padding_check_bound.iter())
- .chain(self_bounds.iter());
+ .chain(zerocopy_bounds.iter());
// The parameters with trait bounds, but without type defaults.
let mut params: Vec<_> = self