Skip to content

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

Open
@karolzwolak

Description

@karolzwolak

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions