Skip to content

Adding an instance makes suggested fix invalid #160710

Description

@pacak

Code

struct Bytes;

#[diagnostic::do_not_recommend]
impl From<Bytes> for &str {
    fn from(_: Bytes) -> Self {
        ""
    }
}

fn main() {
    let _ = &str::from("value");
}

Current output

Compiling app v0.1.0 (/home/pacak/tmp/app)
error[E0277]: the trait bound `str: From<_>` is not satisfied
  --> src/main.rs:11:14
   |
11 |     let _ = &str::from("value");
   |              ^^^ the trait `From<_>` is not implemented for `str`
   |
help: the trait `From<_>` is not implemented for `_`
      but trait `From<Bytes>` is implemented for `&_`
  --> src/main.rs:4:1
   |
 4 | impl From<Bytes> for &str {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for that trait implementation, expected `&str`, found `str`
help: you likely meant to call the associated function `from` for type `&str`, but the code as written calls associated function `from` on type `str`
   |
11 -     let _ = &str::from("value");
11 +     let _ = <&mut str>::from("value");
   |

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> src/main.rs:11:14
   |
11 |     let _ = &str::from("value");
   |              ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
   = note: the return type of a function must have a statically known size

For more information about this error, try `rustc --explain E0277`.
error: could not compile `app` (bin "app") due to 2 previous errors

Desired output

error[E0277]: the trait bound `str: From<_>` is not satisfied
  --> src/main.rs:11:14
   |
11 |     let _ = &str::from("value");
   |              ^^^ the trait `From<_>` is not implemented for `str`
   |
   = help: `String` implements trait `From<T>`:
             From<&String>
             From<&mut str>
             From<&str>
             From<Box<str>>
             From<Cow<'_, str>>
             From<char>
help: you likely meant to call the associated function `from` for type `&str`, but the code as written calls associated function `from` on type `str`
   |
11 |     let _ = <&str>::from("value");
   |             +    +

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> src/main.rs:11:14
   |
11 |     let _ = &str::from("value");
   |              ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
   = note: the return type of a function must have a statically known size

Rationale and extra context

Let's start with a simple example:

fn main() {
    let _ = &str::from("value");
}

Doesn't compile, but adding </> as compiler suggests fixes the problem (desired error message).

Now let's add somewhat related From impl. Compiler ignores
#[diagnostic::do_not_recommend] unless I'm holding it wrong and suggestion doesn't make things better. I'm not even sure where &mut is coming from...

Other cases

Rust Version

rustc 1.99.0-nightly (84b36a78a 2026-08-06)
binary: rustc
commit-hash: 84b36a78a28a63f134171c670be1932ffa2485f8
commit-date: 2026-08-06
host: x86_64-unknown-linux-gnu
release: 1.99.0-nightly
LLVM version: 23.1.0

Anything else?

I'm trying to revive #109350, and Bytes is something that needs to be added. Got it mostly working, now trying to decide what to do with a few error messages in tests.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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