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

Unhelpful borrowing suggested when wrongly constructing an unsized type #132041

Open
cyrgani opened this issue Oct 22, 2024 · 0 comments · May be fixed by #132469
Open

Unhelpful borrowing suggested when wrongly constructing an unsized type #132041

cyrgani opened this issue Oct 22, 2024 · 0 comments · May be fixed by #132469
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

@cyrgani
Copy link
Contributor

cyrgani commented Oct 22, 2024

Code

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

Current output

error[E0277]: the trait bound `str: From<_>` is not satisfied
 --> src/main.rs:2:15
  |
2 |     let _x = &str::from("value");
  |               ^^^ the trait `From<_>` is not implemented for `str`
  |
help: consider borrowing here
  |
2 |     let _x = &&str::from("value");
  |               +
2 |     let _x = &&mut str::from("value");
  |               ++++

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/main.rs:2:15
  |
2 |     let _x = &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`.

Desired output

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> src/main.rs:2:15
  |
2 |     let _x = &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`.

Rationale and extra context

The first suggestion doesn't help and will just suggest stacking &[mut]s without solving the problem. It should be removed.

Other cases

No response

Rust Version

1.84.0-nightly

(2024-10-21 4392847410ddd67f6734)

Anything else?

No response

@cyrgani cyrgani 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 Oct 22, 2024
estebank added a commit to estebank/rust that referenced this issue Nov 1, 2024
When encountering `&str::from("value")` do not suggest `&&str::from("value")`.

Fix rust-lang#132041.
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.

1 participant