From: Benno Lossin Date: Fri, 16 Jan 2026 10:54:19 +0000 (+0100) Subject: rust: pin-init: internal: add utility API for syn error handling X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26bd9402389eaebed086755afb03453dcae6617a;p=thirdparty%2Fkernel%2Flinux.git rust: pin-init: internal: add utility API for syn error handling The API is similar to diagnostics handling in rustc and uses a `ErrorGuaranteed` value to signify that an error has been emitted. It supports both fatal errors (which abort the macro expansion immediately by returning `Err(ErrorGuaranteed)`) and non-fatal ones at generation time. These errors are appended to the token stream after generation has finished normally. This allows giving good errors while still expanding most of the code as expected to avoid the user encountering additional errors (for example missing definitions). Suggested-by: Gary Guo Tested-by: Andreas Hindborg Reviewed-by: Gary Guo [ remove duplicate word in commit message - Benno ] Signed-off-by: Benno Lossin --- diff --git a/rust/pin-init/internal/src/diagnostics.rs b/rust/pin-init/internal/src/diagnostics.rs new file mode 100644 index 0000000000000..555876c01babe --- /dev/null +++ b/rust/pin-init/internal/src/diagnostics.rs @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use std::fmt::Display; + +use proc_macro2::TokenStream; +use syn::{spanned::Spanned, Error}; + +pub(crate) struct DiagCtxt(TokenStream); +pub(crate) struct ErrorGuaranteed(()); + +impl DiagCtxt { + #[expect(dead_code)] + pub(crate) fn error(&mut self, span: impl Spanned, msg: impl Display) -> ErrorGuaranteed { + let error = Error::new(span.span(), msg); + self.0.extend(error.into_compile_error()); + ErrorGuaranteed(()) + } + + #[expect(dead_code)] + pub(crate) fn with( + fun: impl FnOnce(&mut DiagCtxt) -> Result, + ) -> TokenStream { + let mut dcx = Self(TokenStream::new()); + match fun(&mut dcx) { + Ok(mut stream) => { + stream.extend(dcx.0); + stream + } + Err(ErrorGuaranteed(())) => dcx.0, + } + } +} diff --git a/rust/pin-init/internal/src/lib.rs b/rust/pin-init/internal/src/lib.rs index 4c4dc639ce823..0e1a4724549df 100644 --- a/rust/pin-init/internal/src/lib.rs +++ b/rust/pin-init/internal/src/lib.rs @@ -12,6 +12,7 @@ use proc_macro::TokenStream; +mod diagnostics; mod helpers; mod pin_data; mod pinned_drop;