External Publication
Visit Post

Parser error recovery in `syn` for better IDE support with proc-macros

Rust Internals [Unofficial] May 31, 2026
Source

What I wish for from syn is an easy way to treat bodies opaquely.

#[async_trait]
pub trait MyTrait {
    async fn my_fn(&self) {
        let = self. // currently typing here
    }
}

There should be no reason why the proc macro should struggle with this. The parts it cares about are completely valid. The body, which it doesn't really care about, is delimited in the Group. It should just forward it to the output.

Some function attribute macros like tracing::instrument manually implement something like this.

github.com/tokio-rs/tracing

tracing-attributes/src/lib.rs

d9d4c542d

    652.             vis: &self.vis,


    653.             sig: &self.sig,


    654.             brace_token: &self.brace_token,


    655.             block: &self.block,


    656.         }


    657.     }


    658. }


    659.

    660. /// This parses a `TokenStream` into a `MaybeItemFn`


    661. /// (just like `ItemFn`, but skips parsing the body).


    662. impl Parse for MaybeItemFn {


    663.     fn parse(input: ParseStream<'_>) -> syn::Result<Self> {


    664.         let outer_attrs = input.call(Attribute::parse_outer)?;


    665.         let vis: Visibility = input.parse()?;


    666.         let sig: Signature = input.parse()?;


    667.         let inner_attrs = input.call(Attribute::parse_inner)?;


    668.         let block;


    669.         let brace_token = syn::braced!(block in input);


    670.         let block: TokenStream = block.call(|buffer| buffer.parse())?;


    671.         Ok(Self {


    672.             outer_attrs,

Discussion in the ATmosphere

Loading comments...