Skip to content

#[test] interacts strangely with attribute macros that discard the function #148277

@theemathas

Description

@theemathas

I'm not sure if this is a bug or not.

dep/src/lib.rs:

use proc_macro::TokenStream; #[proc_macro_attribute] pub fn discard(_: TokenStream, _: TokenStream) -> TokenStream { TokenStream::new() }

src/lib.rs:

#![allow(dead_code)] fn lol() { panic!("why are you calling me"); } #[test] #[dep::discard] fn lol() {}

Running cargo test on this gives the following output:

 Finished `test` profile [unoptimized + debuginfo] target(s) in 0.00s Running unittests src/lib.rs (target/debug/deps/foo-ebbeb6822a5b805f) running 1 test test lol ... FAILED failures: ---- lol stdout ---- thread 'lol' (37963) panicked at src/lib.rs:4:5: why are you calling me note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: lol test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s error: test failed, to rerun pass `--lib` 

It seems that a#[test] attribute will attempt to call a function with the same name as the annotated function, whether it's the same function or not. It will even try to call a const with the same name, if one is available. This seems rather strange. Not sure what the correct behavior is though.

Meta

rustc --version --verbose:

rustc 1.93.0-nightly (292be5c7c 2025-10-29) binary: rustc commit-hash: 292be5c7c05138d753bbd4b30db7a3f1a5c914f7 commit-date: 2025-10-29 host: aarch64-apple-darwin release: 1.93.0-nightly LLVM version: 21.1.3 

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions