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

Parsing keywords in Meta-path positions fails with syn v2 #1414

Closed
jonasbb opened this issue Mar 20, 2023 · 4 comments
Closed

Parsing keywords in Meta-path positions fails with syn v2 #1414

jonasbb opened this issue Mar 20, 2023 · 4 comments

Comments

@jonasbb
Copy link

jonasbb commented Mar 20, 2023

I have an attribute like #[serde_as(as = "FooBar")]. While trying to use syn v2 in serde_with, I see many errors like this (log):

error: expected path
  --> serde_with_test/tests/serde_as_crate_path.rs:14:16
   |
14 |     #[serde_as(as = "_")]
   |                ^^

The issue seems to be that the Path types in MetaNameValue does not allow keywords.
This does prevent me from upgrading syn in serde_with, which I tried TedDriggs/darling#226 and jonasbb/serde_with#578.

This parses with syn v1, but panics with expected path for v2.

fn main() {
    let attr: syn::MetaNameValue = syn::parse_quote!{
        as = "FooBar"
    };
    dbg!(attr);
}
@dtolnay
Copy link
Owner

dtolnay commented Mar 20, 2023

I think this is behaving correctly, as Rust macro_rules also do not consider as = "FooBar" to be a valid "meta" (which MetaNameValue corresponds to a variant of).

macro_rules! meta {
    ($m:meta) => {};
}

meta!(as = "FooBar");
error: expected identifier, found keyword `as`
 --> src/lib.rs:5:7
  |
5 | meta!(as = "FooBar");
  |       ^^ expected identifier, found keyword
  |
help: escape `as` to use it as an identifier
  |
5 | meta!(r#as = "FooBar");
  |       ++

@JelteF
Copy link

JelteF commented Apr 3, 2023

I don't think the example you gives proves that this is behaving correctly, because it's not actually parsing the same thing. The following is accepted by macro_rules:

macro_rules! meta {
    ($m:meta) => {};
}

meta!(serde_as(as = "FooBar"));

@dtolnay
Copy link
Owner

dtolnay commented Apr 3, 2023

Yes, that is accepted by syn::Meta as well.

@JelteF
Copy link

JelteF commented Apr 3, 2023

Okay, so the problem I guess is that we're trying to parse the content of the element as syn::Meta again. And because it can contain keyword arguments that fails.

Repository owner locked and limited conversation to collaborators Apr 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants