rust-lang / rust-lang/rust

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

Open
#148,277 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-libtest A-macros C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the report with the examples in dep/src/lib.rs and src/lib.rs, starting with cargo test and the shown nightly toolchain. Trace how #[test] and dep::discard interact when the function disappears, including the same-name const case mentioned in the report. Done should establish the intended behavior and provide coverage for the agreed handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.