Skip to content

Can't implement traits in declarative macros. #44128

Closed
@Lymia

Description

@Lymia

Implementing traits doesn't seem to work either in declarative macros. This code looks like it should compile:

#![feature(decl_macro)]

pub macro create_struct($a:ident) {
    struct $a;
    impl Clone for $a {
        fn clone(&self) -> Self {
            $a
        }
    }
}

fn main() {
    create_struct!(Test);
    Test.clone();
}

But it does this instead:

   Compiling playground v0.0.1 (file:///playground)
error[E0046]: not all trait items implemented, missing: `clone`
  --> src/main.rs:5:5
   |
5  | /     impl Clone for $a {
6  | |         fn clone(&self) -> Self {
7  | |             $a
8  | |         }
9  | |     }
   | |_____^ missing `clone` in implementation
...
13 |       create_struct!(Test);
   |       --------------------- in this macro invocation
   |
   = note: `clone` from trait: `fn(&Self) -> Self`

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

Labels

A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-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