syn icon indicating copy to clipboard operation
syn copied to clipboard

`parse_nested_meta` docs: mention that error will be returned if closure doesn't parse non-path parts

Open nicholasbishop opened this issue 2 years ago • 3 comments
trafficstars

I tried using parse_nested_meta for something very similar to the repr-parsing example given in the docs, except that I didn't need all the information. I was confused why parse_nested_meta returned an error sometimes, even though the closure I passed in did not return an error, and the repr being parsed was valid. I eventually realized that this is because I wasn't parsing the (N) part of align(N). Simplified example:

use anyhow::Result;
use syn::{parenthesized, parse_quote, ItemStruct, LitInt};

fn main() -> Result<()> {
    let input: ItemStruct = parse_quote! {
        #[repr(C, align(4))]
        pub struct MyStruct(u16, u32);
    };

    let mut repr_align = false;
    for attr in &input.attrs {
        if attr.path().is_ident("repr") {
            attr.parse_nested_meta(|meta| {
                if meta.path.is_ident("align") {
                    // With this uncommented, no error occurs:
                    //    let content;
                    //    parenthesized!(content in meta.input);
                    //    let lit: LitInt = content.parse()?;

                    repr_align = true;
                    return Ok(());
                }

                Ok(())
            })
            .unwrap();
        }
    }

    Ok(())
}

Assuming that this is the intended behavior of parse_nested_meta, it would be helpful to describe in the docs exactly what the closure needs to handle itself in order to avoid parse_nested_meta returning an error.

nicholasbishop avatar Mar 27 '23 02:03 nicholasbishop

Will also be nice to improve error message: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=143d258548b205268c85cd1e38dedae9

loganmzz avatar Jan 01 '24 11:01 loganmzz