You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Running cargo test with proc macro that uses main fails #62127
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
Built-in attribute #[x] will conflict with any other attribute named x in scope due to issues that are hard to fix.
This rule was in effect since macro modularization and stabilization of attribute macros.
So, the regression here is that something injects a use of built-in attribute #[main] where it wasn't used previously.
This may be an acceptable regression though, since it's equivalent to introducing a new built-in attribute and we are probably not going to stop introducing new built-in attributes.
@petrochenkov So does the issue boils down to resolution of attribute then?
Or should any usage of builtin attribute fail regardless of used path to access it
@DoumanAsh
Only single-segment paths are ambiguous, an attribute macro named main can still be used if it's referred to by a module-relative path or renamed.
// Ok.#[tokio_macros::main]fnfoo(){}// Ok.use tokio_macros::main as pain;#[pain]fnfoo(){}// Not Ok, ambiguity.use tokio_macros::main;#[main]fnfoo(){}
I suspect that #[tokio_macros::main] is irrelevant and the ambiguous #[main] is generated by the test harness, so this minimized example should also fail:
As described above, the behavior is expected - the test harness uses the built-in #[main] attribute which becomes ambiguous if any other attribute named main is in scope.
Activity
Centril commentedon Jun 27, 2019
cc @petrochenkov
petrochenkov commentedon Jun 27, 2019
Built-in attribute
#[x]will conflict with any other attribute namedxin scope due to issues that are hard to fix.This rule was in effect since macro modularization and stabilization of attribute macros.
So, the regression here is that something injects a use of built-in attribute
#[main]where it wasn't used previously.This may be an acceptable regression though, since it's equivalent to introducing a new built-in attribute and we are probably not going to stop introducing new built-in attributes.
DoumanAsh commentedon Jun 27, 2019
I wasn't aware, but is there
mainattribute which used by compiler already?But I see the problem with name resolution despite using absolute path, if it is difficult/impossible to resolve it is not really critical
petrochenkov commentedon Jun 27, 2019
@DoumanAsh
Yes, it turns an arbitrary function into
main.https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=e53c20462ac85fb770709af2551bde4f
DoumanAsh commentedon Jun 27, 2019
@petrochenkov So does the issue boils down to resolution of attribute then?
Or should any usage of builtin attribute fail regardless of used path to access it
petrochenkov commentedon Jun 27, 2019
@DoumanAsh
Only single-segment paths are ambiguous, an attribute macro named
maincan still be used if it's referred to by a module-relative path or renamed.DoumanAsh commentedon Jun 27, 2019
Ah but in my doctest it was
#[tokio_macros::main]and it fails?So I assume by
Okyou mean how it is supposed to be?petrochenkov commentedon Jun 27, 2019
I suspect that
#[tokio_macros::main]is irrelevant and the ambiguous#[main]is generated by the test harness, so this minimized example should also fail:DoumanAsh commentedon Jun 28, 2019
Yes, you are right, it seems the problem is with using
mainas name of attribute to define rather than with doc test itselfpnkfelix commentedon Jul 4, 2019
@rustbot assign @petrochenkov
petrochenkov commentedon Jul 8, 2019
Not a regression, the issue reproduces on Rust 1.30 already (the release that stabilized procedural macro attributes).
Minimized example:
As described above, the behavior is expected - the test harness uses the built-in
#[main]attribute which becomes ambiguous if any other attribute namedmainis in scope.change/uucore_procs ~ add work-around for attribute/macro name collis…
change/uucore_procs ~ add work-around for attribute/macro name collis…
change/uucore_procs ~ add work-around for attribute/macro name collis…
Rollup merge of rust-lang#93753 - jeremyBanks:main-conflict, r=petroc…
Rollup merge of rust-lang#93753 - jeremyBanks:main-conflict, r=petroc…