{"action":"created","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/28335","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/28335/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/28335/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/28335/events","html_url":"https://github.com/rust-lang/rust/issues/28335","id":105876643,"number":28335,"title":"Cannot hygienically create test functions in macro","user":{"login":"tbu-","id":6666593,"avatar_url":"https://avatars.githubusercontent.com/u/6666593?v=3","gravatar_id":"","url":"https://api.github.com/users/tbu-","html_url":"https://github.com/tbu-","followers_url":"https://api.github.com/users/tbu-/followers","following_url":"https://api.github.com/users/tbu-/following{/other_user}","gists_url":"https://api.github.com/users/tbu-/gists{/gist_id}","starred_url":"https://api.github.com/users/tbu-/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tbu-/subscriptions","organizations_url":"https://api.github.com/users/tbu-/orgs","repos_url":"https://api.github.com/users/tbu-/repos","events_url":"https://api.github.com/users/tbu-/events{/privacy}","received_events_url":"https://api.github.com/users/tbu-/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-09-10T19:04:53Z","updated_at":"2015-09-13T00:11:28Z","closed_at":null,"body":"Code:\r\n```\r\nmacro_rules! macro_ {\r\n    ($t:ty) => {\r\n        #[test]\r\n        fn test() { }\r\n    }\r\n}\r\n\r\nstruct Foo;\r\nstruct Bar;\r\nmacro_!(Foo);\r\nmacro_!(Bar);\r\n```\r\n\r\nError:\r\n```\r\na.rs:4:9: 4:22 error: duplicate definition of value `test` [E0428]\r\na.rs:4         fn test() { }\r\n               ^~~~~~~~~~~~~\r\na.rs:1:1: 6:2 note: in expansion of macro_!\r\na.rs:11:1: 11:14 note: expansion site\r\na.rs:4:9: 4:22 help: run `rustc --explain E0428` to see a detailed explanation\r\na.rs:4:9: 4:22 note: first definition of value `test` here\r\na.rs:4         fn test() { }\r\n               ^~~~~~~~~~~~~\r\na.rs:1:1: 6:2 note: in expansion of macro_!\r\na.rs:10:1: 10:14 note: expansion site\r\nerror: aborting due to previous error\r\n```"},"comment":{"url":"https://api.github.com/repos/rust-lang/rust/issues/comments/139830486","html_url":"https://github.com/rust-lang/rust/issues/28335#issuecomment-139830486","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/28335","id":139830486,"user":{"login":"lfairy","id":1572323,"avatar_url":"https://avatars.githubusercontent.com/u/1572323?v=3","gravatar_id":"","url":"https://api.github.com/users/lfairy","html_url":"https://github.com/lfairy","followers_url":"https://api.github.com/users/lfairy/followers","following_url":"https://api.github.com/users/lfairy/following{/other_user}","gists_url":"https://api.github.com/users/lfairy/gists{/gist_id}","starred_url":"https://api.github.com/users/lfairy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lfairy/subscriptions","organizations_url":"https://api.github.com/users/lfairy/orgs","repos_url":"https://api.github.com/users/lfairy/repos","events_url":"https://api.github.com/users/lfairy/events{/privacy}","received_events_url":"https://api.github.com/users/lfairy/received_events","type":"User","site_admin":false},"created_at":"2015-09-13T00:11:28Z","updated_at":"2015-09-13T00:11:28Z","body":"One workaround is to nest the function in a module:\r\n\r\n```rust\r\nmacro_rules! generate_test {\r\n    ($m:ident, $t:ty) => {\r\n        #[cfg(test)]\r\n        mod $m {\r\n            #[test]\r\n            fn test() { }\r\n        }\r\n    }\r\n}\r\n\r\nstruct Foo;\r\ngenerate_test!(foo, Foo);\r\n```"}}