rust-lang / rust-lang/rust

Error when unsizing types with thin pointers that turn fat

Open
#130,740 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use std::fmt::Debug;

struct ListItem<T: ?Sized> {
    next: Option<Box<Self>>,
    item: T,
}

fn main() {
    let _ = Box::new(ListItem {
        next: None,
        item: [42, 24],
    }) as Box<ListItem<dyn Debug>>;
}
Current output
error[E0605]: non-primitive cast: `Box<ListItem<[i32; 2]>>` as `Box<ListItem<dyn Debug>>`
  --> src/main.rs:9:13
   |
9  |       let _ = Box::new(ListItem {
   |  _____________^
10 | |         next: None,
11 | |         item: [42, 24],
12 | |     }) as Box<ListItem<dyn Debug>>;
   | |__________________________________^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

For more information about this error, try `rustc --explain E0605`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
error[E????]: type `Box<ListItem<[i32; 2]>>` cannot be unsized to `Box<ListItem<dyn Debug>>`
  --> src/main.rs:9:13
   |
9  |       let _ = Box::new(ListItem {
   |  _____________^
10 | |         next: None,
11 | |         item: [42, 24],
12 | |     }) as Box<ListItem<dyn Debug>>;
   | |__________________________________^
note: the struct `ListItem<[i32; 2]>` would change size if unsized to `ListItem<dyn Debug>`
3  │ struct ListItem<T: ?Sized> {
4  │     next: Option<Box<Self>>,
   |     ^^^^^^^^^^^^^^^^^^^^^^^ this field's type would change from a thin pointer to a fat pointer
5  │     item: T,
6  │ }

For more information about this error, try `rustc --explain E????`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Rationale and extra context

No response

Other cases
// Removing the `next` field makes the code compile:

use std::fmt::Debug;

struct ListItem<T: ?Sized> {
    // next: Option<Box<Self>>,
    item: T,
}

fn main() {
    let _ = Box::new(ListItem {
        // next: None,
        item: [42, 24],
    }) as Box<ListItem<dyn Debug>>;
}
Rust Version
rustc 1.83.0-nightly (6c6d21008 2024-09-22)
binary: rustc
commit-hash: 6c6d210089e4589afee37271862b9f88ba1d7755
commit-date: 2024-09-22
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
Anything else?

If more information/explanation is needed, I can provide it, just ask.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic using the example in src/main.rs and the Rust 1.83.0-nightly version reported in the issue. Compare the current E0605 output with the desired unsizing error, including the note about the next field changing from a thin pointer to a fat pointer. Done when this case reports the more specific diagnostic without regressing the variant where the next field is removed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.