Skip to content

Running cargo test with proc macro that uses main fails #62127

Closed

Description

Attempting to run test with proc macro that uses main as identifier fails with following error:

error[E0659]: `main` is ambiguous (built-in attribute vs any other name)
  |
  = note: `main` could refer to a built-in attribute
note: `main` could also refer to the crate-local procedural macro defined here
 --> tokio-macros\src\lib.rs:26:1
  |
26| / pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
27| |     let input = syn::parse_macro_input!(item as syn::ItemFn);
28| |
29| |     let ret = &input.decl.output;
... |
41| |     result.into()
42| | }
  | |_^
  = help: use `crate::main` to refer to this crate-local procedural macro unambiguously

Rustc version:rustc 1.37.0-nightly (8aa42ed7c 2019-06-24)

But it seems it wasn't present in rustc 1.35.0 (3c235d560 2019-05-20)

UPD: It is present since (400b409ef 2019-06-09) too

UPD 2: Might be always present as cargo test seems to inject own main

So something changed in the way such identifier is treated.

Code example:

/// ```
/// #[tokio_macros::main]
/// fn main() {
///     println!("Hello world");
/// }
/// ```
#[proc_macro_attribute]
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(item as syn::ItemFn);

    let ret = &input.decl.output;
    let name = &input.ident;
    let body = &input.block;
    let attrs = &input.attrs;

    let result = quote! {
        #(#attrs)*
        fn #name() #ret {
            #body
        }
    };

    result.into()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

A-resolveArea: Name resolutionP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions