rust-lang / rust-lang/rust

Confusing error "the trait bound `u8: Termination` is not satisfied" when duplicated function is marked as `#[test]`

Open
#138,367 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-macros D-incorrect D-verbose T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#[cfg(test)]
mod tests{
    fn foo() -> u8 {
        0
    }
    
    #[test]
    fn foo() {}
}
Current output
error[E0428]: the name `foo` is defined multiple times
 --> src/lib.rs:8:5
  |
3 |     fn foo() -> u8 {
  |     -------------- previous definition of the value `foo` here
...
8 |     fn foo() {}
  |     ^^^^^^^^ `foo` redefined here
  |
  = note: `foo` must be defined only once in the value namespace of this module

error[E0277]: the trait bound `u8: Termination` is not satisfied
   --> src/lib.rs:8:13
    |
7   |     #[test]
    |     ------- in this procedural macro expansion
8   |     fn foo() {}
    |             ^ the trait `Termination` is not implemented for `u8`
    |
note: required by a bound in `assert_test_result`
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/test/src/lib.rs:234:30
    |
234 | pub fn assert_test_result<T: Termination>(result: T) -> Result<(), String> {
    |                              ^^^^^^^^^^^ required by this bound in `assert_test_result`
    = note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0277, E0428.
For more information about an error, try `rustc --explain E0277`.
Desired output
error[E0428]: the name `foo` is defined multiple times
 --> src/lib.rs:8:5
  |
3 |     fn foo() -> u8 {
  |     -------------- previous definition of the value `foo` here
...
8 |     fn foo() {}
  |     ^^^^^^^^ `foo` redefined here
  |
  = note: `foo` must be defined only once in the value namespace of this module

Some errors have detailed explanations: E0428.
For more information about an error, try `rustc --explain E0428`.
Rationale and extra context

The second error the trait bound `u8: Termination` is not satisfied is confusing, especially because it's reported last (so if you have multiple errors, you might not see you have duplicate functions.

Other cases

No response

Rust Version
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7
Anything else?

If the non-test function returns nothing, the weird error doesn't show up.

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

Start by compiling the provided src/lib.rs example with rustc 1.85.0 and compare the duplicate-definition and Termination diagnostics with the desired output. Trace the compiler handling involved in the #[test] attribute and diagnostic emission; done means the confusing E0277 message is omitted while E0428 remains.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.