Skip to content

casting variable with type &Unsized to &dyn Trait incorrectly says that the variable is unsized. #111406

Description

@hkalbasi

Code

use std::fmt::Debug;

fn f(x: &dyn Debug) {
    println!("{:?}", x)
}

fn main() {
    let x: &[u8] = &[1, 2, 3];
    f(x);
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
 --> src/lib.rs:9:7
  |
9 |     f(x);
  |       ^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[u8]`
  = note: required for the cast from `[u8]` to the object type `dyn Debug`
help: consider borrowing the value, since `&[u8]` can be coerced into `dyn Debug`
  |
9 |     f(&x);
  |       +

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to previous error

Desired output

No response

Rationale and extra context

This error confused me as it was in a context that I thought the type of x is actually [u8], but it was &[u8] and the problem was about dyn cast.

Other cases

No response

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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