Skip to content

Resolving static method on a type is not consistent in macros #58731

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

Closed
wigy-opensource-developer opened this issue Feb 25, 2019 · 3 comments
Closed

Comments

@wigy-opensource-developer
Copy link
Contributor

wigy-opensource-developer commented Feb 25, 2019

It is a bit counter-intuitive that I can call an inherent method inside a macro expansion, but I cannot do so on a type that was deduced by a macro expansion. (rustc 1.32.0)

macro_rules! choose_type {
    (a) => { Concrete };
}

macro_rules! create_type {
    ($some:ident) => { $some::create() }
}

struct Concrete {}

impl Concrete {
    fn create() -> Self {
        println!("Concrete::execute was called");
        Concrete {}
    }
}

fn main() {
    let _t1 = create_type!(Concrete);
    let _t2 = choose_type!(a)::create();
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error: expected one of `.`, `;`, `?`, or an operator, found `::`
  --> src/main.rs:20:30
   |
20 |     let _t2 = choose_type!(a)::create();
   |                              ^^ expected one of `.`, `;`, `?`, or an operator here
...

How is the create_type macro more hygienic than the choose_type one?

@jonas-schievink
Copy link
Contributor

You have to do <choose_type!(a)>::create(); to parse the macro as a type.

@petrochenkov
Copy link
Contributor

Duplicate of #52307.

For some reason the note recommending to rewrite things like [x; N]::method() into <[x; N]>::method is not reported for mac!(x)::method().

@wigy-opensource-developer
Copy link
Contributor Author

wigy-opensource-developer commented Feb 25, 2019

Wow. Thanks for the prompt answers. So basically I can use the more verbose <Type>::method() format anywhere and in some cases even the Type::method() works, but that is more like an exception than the rule 😉

Playground

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

3 participants