Skip to content

decl_macro: scoping and other issues #42448

Closed
@r3c0d3x

Description

@r3c0d3x

The current implementation of decl_macro seems to have some issues, as shown in this example (playground):

#![feature(decl_macro)]

macro macro_ {
    () => {
        #[derive(Debug)]
        struct CreatedStruct;

        impl From<()> for CreatedStruct {
            fn from(_: ()) -> CreatedStruct {
                CreatedStruct
            }
        }
    };
}

macro_!();

fn main() {
    println!("{:?}", CreatedStruct);
}

I expected this to compile and just print out "CreatedStruct" (from its derived Debug implementation), but it throws the following errors instead:

error[E0425]: cannot find value `CreatedStruct` in this scope
  --> <anon>:19:22
   |
19 |     println!("{:?}", CreatedStruct);
   |                      ^^^^^^^^^^^^^ not found in this scope
   |
help: possible candidate is found in another module, you can import it into scope
   | fn main() use CreatedStruct;

error[E0046]: not all trait items implemented, missing: `from`
  --> <anon>:8:9
   |
8  | /         impl From<()> for CreatedStruct {
9  | |             fn from(_: ()) -> CreatedStruct {
10 | |                 CreatedStruct
11 | |             }
12 | |         }
   | |_________^ missing `from` in implementation
...
16 |   macro_!();
   |   ---------- in this macro invocation
   |
   = note: `from` from trait: `fn(T) -> Self`

error: aborting due to previous error(s)

That's strange because our example should be equivalent to this (playground):

macro_rules! macro_ {
    () => {
        #[derive(Debug)]
        struct CreatedStruct;

        impl From<()> for CreatedStruct {
            fn from(_: ()) -> CreatedStruct {
                CreatedStruct
            }
        }
    };
}

macro_!();

fn main() {
    println!("{:?}", CreatedStruct);
}

which works just fine.

The scoping issue is likely caused by whatever is causing #42337, but I'm not sure what is happening with the trait implementation.

CC @jseyfried

Metadata

Metadata

Assignees

Labels

A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions