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

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

Open
HKalbasi opened this issue May 9, 2023 · 1 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.

Comments

@HKalbasi
Copy link
Member

HKalbasi commented May 9, 2023

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

@HKalbasi HKalbasi 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 9, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 11, 2023
…r=b-naber

Note user-facing types of coercion failure

When coercing, for example, `Box<A>` into `Box<dyn B>`, make sure that any failure notes mention *those* specific types, rather than mentioning inner types, like "the cast from `A` to `dyn B`".

I expect end-users are often confused when we skip layers of types and only mention the "innermost" part of a coercion, especially when other notes point at HIR, e.g. rust-lang#111406.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 11, 2023
…r=b-naber

Note user-facing types of coercion failure

When coercing, for example, `Box<A>` into `Box<dyn B>`, make sure that any failure notes mention *those* specific types, rather than mentioning inner types, like "the cast from `A` to `dyn B`".

I expect end-users are often confused when we skip layers of types and only mention the "innermost" part of a coercion, especially when other notes point at HIR, e.g. rust-lang#111406.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 11, 2023
…r=b-naber

Note user-facing types of coercion failure

When coercing, for example, `Box<A>` into `Box<dyn B>`, make sure that any failure notes mention *those* specific types, rather than mentioning inner types, like "the cast from `A` to `dyn B`".

I expect end-users are often confused when we skip layers of types and only mention the "innermost" part of a coercion, especially when other notes point at HIR, e.g. rust-lang#111406.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue May 11, 2023
…r=b-naber

Note user-facing types of coercion failure

When coercing, for example, `Box<A>` into `Box<dyn B>`, make sure that any failure notes mention *those* specific types, rather than mentioning inner types, like "the cast from `A` to `dyn B`".

I expect end-users are often confused when we skip layers of types and only mention the "innermost" part of a coercion, especially when other notes point at HIR, e.g. rust-lang#111406.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue May 13, 2023
…r=b-naber

Note user-facing types of coercion failure

When coercing, for example, `Box<A>` into `Box<dyn B>`, make sure that any failure notes mention *those* specific types, rather than mentioning inner types, like "the cast from `A` to `dyn B`".

I expect end-users are often confused when we skip layers of types and only mention the "innermost" part of a coercion, especially when other notes point at HIR, e.g. rust-lang#111406.
@Zoybean
Copy link

Zoybean commented May 17, 2024

I can see how this pointer could have been especially misleading, given x is &[u8] and therefore Sized:

9 |     f(x);
  |       ^ doesn't have a size known at compile-time

Maybe if it instead said:

...
9 |     f(x);
  |       ^ casting this value from `&[u8]` to the object type `&dyn Debug`
...

but that might risk being too much repetition of similar-but-distinct terms ([u8],&[u8],dyn Debug,&dyn Debug) in the context of the full error

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

No branches or pull requests

2 participants