Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nicholasbishop opened this issue Mar 27, 2023 · 1 comment
Labels

Comments

@nicholasbishop
Copy link

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.

@loganmzz
Copy link

loganmzz commented Jan 1, 2024

@dtolnay dtolnay added the docs label May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants