Skip to content

Compiler suggests adding illegal where bounds #97576

Closed
@Patryk27

Description

@Patryk27

Hi! 👋

Given the following code:

struct Foo {
    bar: String,
}

impl Foo {
    pub fn new(bar: impl ToString) -> Self {
        Self {
            bar: bar.into(),
        }
    }
}

(https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=248f8c6fd510d2abf74cb29e90ad8588)

The current output (stable / beta / nightly) is:

error[E0277]: the trait bound `String: From<impl ToString>` is not satisfied
 --> src/lib.rs:8:22
  |
8 |             bar: bar.into(),
  |                      ^^^^ the trait `From<impl ToString>` is not implemented for `String`
  |
  = note: required because of the requirements on the impl of `Into<String>` for `impl ToString`
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
  |
5 | impl Foo where String: From<impl ToString> {
  |          +++++++++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.

The issue is that I've used bar.into() instead of bar.to_string() -- and while the compiler suggesting to replace .into() with .to_string() would be pretty cool, at the very least I think it shouldn't propose adding where String: From<impl ToString> (which is both illegal & pretty pointless).

Metadata

Metadata

Assignees

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