rust-lang / rust-lang/rust

suggestion for unimportet trait suggests different function from the same unimported trait

Open
#147,817 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
fn main() {
    println!("{}: Io: failed to read file", "error".bright_red().bold());

    match thing("test") {
        _ => todo!(),
    }
}

// fn thing(_: &str) {}

with the following Cargo.toml:

[package]
name = "tmp"
version = "0.1.0"
edition = "2024"

[dependencies]
owo-colors = "4.2.3"
Current output
error[E0599]: no method named `bright_red` found for reference `&'static str` in the current scope
   --> src/main.rs:2:53
    |
  2 |     println!("{}: Io: failed to read file", "error".bright_red().bold());
    |                                                     ^^^^^^^^^^
    |
   ::: /home/may/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/owo-colors-4.2.3/src/lib.rs:337:24
    |
337 |         BrightRed      bright_red      on_bright_red,
    |                        ---------- the method is available for `&'static str` here
    |
    = help: items from traits can only be used if the trait is in scope
help: trait `OwoColorize` which provides `bright_red` is implemented but not in scope; perhaps you want to import it
    |
  1 + use owo_colors::OwoColorize;
    |
help: there is a method `on_bright_red` with a similar name
    |
  2 |     println!("{}: Io: failed to read file", "error".on_bright_red().bold());
    |                                                     +++

error[E0425]: cannot find function `thing` in this scope
 --> src/main.rs:4:11
  |
4 |     match thing("test") {
  |           ^^^^^ not found in this scope
Desired output
error[E0599]: no method named `bright_red` found for reference `&'static str` in the current scope
   --> src/main.rs:2:53
    |
  2 |     println!("{}: Io: failed to read file", "error".bright_red().bold());
    |                                                     ^^^^^^^^^^
    |
   ::: /home/may/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/owo-colors-4.2.3/src/lib.rs:337:24
    |
337 |         BrightRed      bright_red      on_bright_red,
    |                        ---------- the method is available for `&'static str` here
    |
    = help: items from traits can only be used if the trait is in scope
help: trait `OwoColorize` which provides `bright_red` is implemented but not in scope; perhaps you want to import it
    |
  1 + use owo_colors::OwoColorize;
    |

error[E0425]: cannot find function `thing` in this scope
 --> src/main.rs:4:11
  |
4 |     match thing("test") {
  |           ^^^^^ not found in this scope
Rationale and extra context

if i apply the last suggestion for the .bright_red case by changing it to .on_bright_red i get the same error, as on_bright_red is also only available when i import the trait, suggesting me to use the bright_red function again, which still doesn't work:

error[E0599]: no method named `on_bright_red` found for reference `&'static str` in the current scope
   --> src/main.rs:2:53
    |
  2 |     println!("{}: Io: failed to read file", "error".on_bright_red().bold());
    |                                                     ^^^^^^^^^^^^^
    |
   ::: /home/may/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/owo-colors-4.2.3/src/lib.rs:337:40
    |
337 |         BrightRed      bright_red      on_bright_red,
    |                                        ------------- the method is available for `&'static str` here
    |
    = help: items from traits can only be used if the trait is in scope
help: trait `OwoColorize` which provides `on_bright_red` is implemented but not in scope; perhaps you want to import it
    |
  1 + use owo_colors::OwoColorize;
    |
help: there is a method `bright_red` with a similar name
    |
  2 -     println!("{}: Io: failed to read file", "error".on_bright_red().bold());
  2 +     println!("{}: Io: failed to read file", "error".bright_red().bold());
    |

error[E0425]: cannot find function `thing` in this scope
 --> src/main.rs:4:11
  |
4 |     match thing("test") {
  |           ^^^^^ not found in this scope
Rust Version
rustc 1.92.0-nightly (be0ade2b6 2025-10-11)
binary: rustc
commit-hash: be0ade2b602bdfe37a3cc259fcc79e8624dcba94
commit-date: 2025-10-11
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2
Anything else?

additionally i found it quite hard to minimally reproduce this error on it's own: if i uncomment the fn thing(), so that rust stops complaining about not finding a function thing, then the error for the .bright_red not being found shrinks immensely in verbosity and doesn't recognize, where i could import it from or suggest it to me.

error[E0599]: no method named `bright_red` found for reference `&'static str` in the current scope
 --> src/main.rs:2:53
  |
2 |     println!("{}: Io: failed to read file", "error".bright_red().bold());
  |                                                     ^^^^^^^^^^ method not found in `&'static str`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `tmp` (bin "tmp") due to 1 previous error

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 diagnostic with the shown src/main.rs and Cargo.toml, using the reported nightly rustc version, and compare the current and desired suggestions. Trace rustc's diagnostic suggestion handling for missing trait imports and similar method names; done means the unrelated on_bright_red suggestion is no longer emitted while the OwoColorize import suggestion remains, with regression coverage for the interaction with the missing thing function.

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.