Skip to content

Can't use #[derive] and macro on a generic type at the same time #50676

Open
@tuxzz

Description

@tuxzz

I have this code:

macro_rules! typ {
    () => { i32 };
}

#[derive(Debug)]
struct MyStruct<T> {
    field: typ!(),
}

The compiler gives this error:

error: `derive` cannot be used on items with type macros
 --> src/main.rs:7:12
  |
7 |     field: typ!(),
  |            ^^^^^^

But if I change MyStruct into non-generic type

macro_rules! typ {
    () => { i32 };
}

#[derive(Debug)]
struct MyStruct {
    field: typ!(),
}

It works without any error.

Why can't use #[derive] and macro on a generic type at the same time?
Is it a compiler bug?

-> Mirror issue on StackOverflow

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.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