Skip to content

Box "doesn't have a size known at compile-time" wrong diagnostic #63143

Closed
@spastorino

Description

@spastorino

If I do ...

fn main() {
    let x = "hello";
    let y = Box::new(*x);
}

I get the following error ...

 --> src/main.rs:3:13
  |
3 |     let y = Box::new(*x);
  |             ^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `str`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required by `std::boxed::Box::<T>::new`

I think the right error should be ...

 --> src/main.rs:3:13
  |
3 |     let y = Box::new(*x);
  |                      ^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `str`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required by `std::boxed::Box::<T>::new`

Is not that Box is not Sized, the problem is that the parameter of the Box's new function is not Sized. So the last note in the error is important and also kind of lost in a lot of noise.

Possibly related to #61860.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-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