Skip to content

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

Closed
@wigy-opensource-developer

Description

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions