[PATCH v2 04/15] rust: pin-init: internal: add utility API for syn error handling

Benno Lossin posted 15 patches 4 weeks ago
There is a newer version of this series
[PATCH v2 04/15] rust: pin-init: internal: add utility API for syn error handling
Posted by Benno Lossin 4 weeks ago
Add a function to turn a `syn::Result<TokenStream>` into a
`TokenStream`, either containing the error or the normal stream.

Add a nullable custom error type wrapping `syn::Error`.

Signed-off-by: Benno Lossin <lossin@kernel.org>
---
Changes in v2: added this patch
---
 rust/pin-init/internal/src/lib.rs | 44 +++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/rust/pin-init/internal/src/lib.rs b/rust/pin-init/internal/src/lib.rs
index 4c4dc639ce82..21f5e33486ce 100644
--- a/rust/pin-init/internal/src/lib.rs
+++ b/rust/pin-init/internal/src/lib.rs
@@ -36,3 +36,47 @@ pub fn derive_zeroable(input: TokenStream) -> TokenStream {
 pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
     zeroable::maybe_derive(input.into()).into()
 }
+
+#[expect(dead_code)]
+fn ok_or_compile_error(res: syn::Result<proc_macro2::TokenStream>) -> TokenStream {
+    match res {
+        Ok(stream) => stream,
+        Err(error) => error.into_compile_error(),
+    }
+    .into()
+}
+
+pub(crate) struct Error(Option<syn::Error>);
+
+impl From<syn::Error> for Error {
+    fn from(value: syn::Error) -> Self {
+        Self(Some(value))
+    }
+}
+
+impl Error {
+    #[expect(dead_code)]
+    pub(crate) fn none() -> Self {
+        Self(None)
+    }
+
+    #[expect(dead_code)]
+    pub(crate) fn combine(&mut self, error: impl Into<Self>) {
+        let error = error.into();
+        if let Some(this) = self.0.as_mut() {
+            if let Some(error) = error.0 {
+                this.combine(error);
+            }
+        } else {
+            self.0 = error.0;
+        }
+    }
+}
+
+impl quote::ToTokens for Error {
+    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
+        if let Some(error) = self.0.as_ref() {
+            quote::ToTokens::to_tokens(&error.to_compile_error(), tokens);
+        }
+    }
+}
-- 
2.52.0
Re: [PATCH v2 04/15] rust: pin-init: internal: add utility API for syn error handling
Posted by Gary Guo 3 weeks, 6 days ago
On Sun Jan 11, 2026 at 12:25 PM GMT, Benno Lossin wrote:
> Add a function to turn a `syn::Result<TokenStream>` into a
> `TokenStream`, either containing the error or the normal stream.
>
> Add a nullable custom error type wrapping `syn::Error`.
>
> Signed-off-by: Benno Lossin <lossin@kernel.org>
> ---
> Changes in v2: added this patch
> ---
>  rust/pin-init/internal/src/lib.rs | 44 +++++++++++++++++++++++++++++++
>  1 file changed, 44 insertions(+)
>
> diff --git a/rust/pin-init/internal/src/lib.rs b/rust/pin-init/internal/src/lib.rs
> index 4c4dc639ce82..21f5e33486ce 100644
> --- a/rust/pin-init/internal/src/lib.rs
> +++ b/rust/pin-init/internal/src/lib.rs
> @@ -36,3 +36,47 @@ pub fn derive_zeroable(input: TokenStream) -> TokenStream {
>  pub fn maybe_derive_zeroable(input: TokenStream) -> TokenStream {
>      zeroable::maybe_derive(input.into()).into()
>  }
> +
> +#[expect(dead_code)]
> +fn ok_or_compile_error(res: syn::Result<proc_macro2::TokenStream>) -> TokenStream {
> +    match res {
> +        Ok(stream) => stream,
> +        Err(error) => error.into_compile_error(),
> +    }
> +    .into()
> +}
> +
> +pub(crate) struct Error(Option<syn::Error>);

I don't think it's a good idea to call a type `Error` when it could just be no
error.

I think what I propose in https://lore.kernel.org/all/DFK7ITVQ97RL.2SZ2ANDIQ39H3@garyguo.net
is better than what's implemented here.

Best,
Gary

> +
> +impl From<syn::Error> for Error {
> +    fn from(value: syn::Error) -> Self {
> +        Self(Some(value))
> +    }
> +}
> +
> +impl Error {
> +    #[expect(dead_code)]
> +    pub(crate) fn none() -> Self {
> +        Self(None)
> +    }
> +
> +    #[expect(dead_code)]
> +    pub(crate) fn combine(&mut self, error: impl Into<Self>) {
> +        let error = error.into();
> +        if let Some(this) = self.0.as_mut() {
> +            if let Some(error) = error.0 {
> +                this.combine(error);
> +            }
> +        } else {
> +            self.0 = error.0;
> +        }
> +    }
> +}
> +
> +impl quote::ToTokens for Error {
> +    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
> +        if let Some(error) = self.0.as_ref() {
> +            quote::ToTokens::to_tokens(&error.to_compile_error(), tokens);
> +        }
> +    }
> +}
Re: [PATCH v2 04/15] rust: pin-init: internal: add utility API for syn error handling
Posted by Benno Lossin 3 weeks, 5 days ago
On Mon Jan 12, 2026 at 5:14 PM CET, Gary Guo wrote:
> I don't think it's a good idea to call a type `Error` when it could just be no
> error.
>
> I think what I propose in https://lore.kernel.org/all/DFK7ITVQ97RL.2SZ2ANDIQ39H3@garyguo.net
> is better than what's implemented here.

I don't use the fatal error, so I opted to make `emit` return unit:

    https://github.com/Rust-for-Linux/pin-init/pull/89/changes/b0a242334802695342f2095657dbe62c8841eb0c

What do you think?

Cheers,
Benno