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

Can't parse type defined inside unnamed constant #1378

Closed
davidpdrsn opened this issue Feb 8, 2023 · 1 comment
Closed

Can't parse type defined inside unnamed constant #1378

davidpdrsn opened this issue Feb 8, 2023 · 1 comment

Comments

@davidpdrsn
Copy link

For proc-macros is quite common to put the generated code inside unnamed constants, to not pollute the surrounding namespace. However the name of types defined inside such constants cannot be parsed by syn::Type:

use std::any::type_name;
use syn::Type;

trait A {
    type T;
}

struct Foo;

const _: () = {
    struct Bar;

    impl A for Foo {
        type T = Bar;
    }
};

fn main() {
    syn::parse_str::<Type>(type_name::<<Foo as A>::T>()).unwrap();
}

This panics with

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("expected identifier")', src/main.rs:6:58
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The value of type_name::<<Foo as A>::T>() is foo::_::Bar (where foo is the crate), so maybe syn doesn't support ::_::. I hadn't seen that before myself either 😅

@dtolnay
Copy link
Owner

dtolnay commented Feb 8, 2023

This is behaving correctly as far as I can tell.

@dtolnay dtolnay closed this as completed Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants