Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler suggests adding illegal where bounds #97576

Closed
Patryk27 opened this issue May 31, 2022 · 0 comments · Fixed by #97640
Closed

Compiler suggests adding illegal where bounds #97576

Patryk27 opened this issue May 31, 2022 · 0 comments · Fixed by #97640
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Patryk27
Copy link
Contributor

Patryk27 commented May 31, 2022

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).

@Patryk27 Patryk27 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 31, 2022
@TaKO8Ki TaKO8Ki self-assigned this May 31, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 2, 2022
…ding-where-clauses, r=lcnr

Fix wrong suggestion for adding where clauses

closes rust-lang#97576
@bors bors closed this as completed in 0e33e97 Jun 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants