Skip to content

Unhelpful borrowing suggested when wrongly constructing an unsized type #132041

@cyrgani

Description

@cyrgani

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

Metadata

Metadata

Assignees

No one assigned

    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