Re: [PATCH 05/12] rust: pin-init: rewrite the `#[pinned_drop]` attribute macro using `syn`
From: Gary Guo
Date: Fri Jan 09 2026 - 11:42:33 EST
On Fri Jan 9, 2026 at 3:34 PM GMT, Benno Lossin wrote:
> On Fri Jan 9, 2026 at 1:12 PM CET, Gary Guo wrote:
>> On Thu Jan 8, 2026 at 1:50 PM GMT, Benno Lossin wrote:
>>> diff --git a/rust/pin-init/internal/src/pinned_drop.rs b/rust/pin-init/internal/src/pinned_drop.rs
>>> index cf8cd1c42984..4df2cb9959fb 100644
>>> --- a/rust/pin-init/internal/src/pinned_drop.rs
>>> +++ b/rust/pin-init/internal/src/pinned_drop.rs
>>> @@ -1,49 +1,56 @@
>>> // SPDX-License-Identifier: Apache-2.0 OR MIT
>>>
>>> -use proc_macro2::{TokenStream, TokenTree};
>>> -use quote::quote;
>>> +use proc_macro2::TokenStream;
>>> +use quote::{quote, quote_spanned};
>>> +use syn::{parse::Nothing, parse_quote, spanned::Spanned, ImplItem, ItemImpl, Token};
>>>
>>> -pub(crate) fn pinned_drop(_args: TokenStream, input: TokenStream) -> TokenStream {
>>> - let mut toks = input.into_iter().collect::<Vec<_>>();
>>> - assert!(!toks.is_empty());
>>> - // Ensure that we have an `impl` item.
>>> - assert!(matches!(&toks[0], TokenTree::Ident(i) if i == "impl"));
>>> - // Ensure that we are implementing `PinnedDrop`.
>>> - let mut nesting: usize = 0;
>>> - let mut pinned_drop_idx = None;
>>> - for (i, tt) in toks.iter().enumerate() {
>>> - match tt {
>>> - TokenTree::Punct(p) if p.as_char() == '<' => {
>>> - nesting += 1;
>>> +pub(crate) fn pinned_drop(_args: Nothing, mut input: ItemImpl) -> TokenStream {
>>> + let mut errors = vec![];
>>
>> Any reason to not make this `Vec<Error>` and use `syn::Error::combine`?
>
> Is there a way to have an "empty" error? I dislike using `Option<Error>`
> here... If not I'll just create my own helper type instead.
Why do you need `empty` error?
You can turn a `Vec<Error>` into errors by
errors.into_iter().reduce(|mut acc, e| { acc.combine(e); acc })
If you want helpers, this would be my preferred approach (similar to how rustc
handles things):
struct DiagCtxt {
errors: Vec<Error>,
}
struct ErrorGuaranteed;
impl DiagCtxt {
fn emit(err: Error) -> ErrorGuaranteed {
self.errors.push(err);
ErrorGuaranteed
}
fn with<R: ToTokens>(f: impl FnOnce(&mut DiagCtxt) -> Result<T, ErrorGuaranteed>) -> TokenStream {
let mut dcx = DiagCtxt { errors: Vec::new() };
let mut result = match f(&mut dcx) {
Ok(r) => r.into_token_stream(),
Err(_) => quote!(),
};
for err in dcx.errors {
result.extend(err.into_token_stream());
}
result
}
}
(untested)
and now you can
DiagCtxt::with(|dcx| generate_xxx(dcx));
// Non-fatal error
dcx.emit(...)
// Fatal-error
Err(dcx.emit(...))?
Best,
Gary
>
> Cheers,
> Benno
>
>>> + if let Some(unsafety) = input.unsafety {
>>> + errors.push(quote_spanned! {unsafety.span=>
>>> + ::core::compile_error!("implementing `PinnedDrop` is safe");
>>> + });
>>> + }
>>> + input.unsafety = Some(Token);
>>> + match &mut input.trait_ {
>>> + Some((not, path, _for)) => {
>>> + if let Some(not) = not {
>>> + errors.push(quote_spanned! {not.span=>
>>> + ::core::compile_error!("cannot implement `!PinnedDrop`");
>>> + });