Skip to content

Cannot hygienically create test functions in macro #28335

Closed
@tbu-

Description

@tbu-

Code:

macro_rules! macro_ {
    ($t:ty) => {
        #[test]
        fn test() { }
    }
}

struct Foo;
struct Bar;
macro_!(Foo);
macro_!(Bar);

Error:

a.rs:4:9: 4:22 error: duplicate definition of value `test` [E0428]
a.rs:4         fn test() { }
               ^~~~~~~~~~~~~
a.rs:1:1: 6:2 note: in expansion of macro_!
a.rs:11:1: 11:14 note: expansion site
a.rs:4:9: 4:22 help: run `rustc --explain E0428` to see a detailed explanation
a.rs:4:9: 4:22 note: first definition of value `test` here
a.rs:4         fn test() { }
               ^~~~~~~~~~~~~
a.rs:1:1: 6:2 note: in expansion of macro_!
a.rs:10:1: 10:14 note: expansion site
error: aborting due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions