Skip to content

Add a compiler check that std::mem::size_of isn’t called on unsized types? #80795

Closed
@steffahn

Description

@steffahn

While re-discovering #57893 (in #80783) and in particular while poking around in the world of all the wonderful ICEs that you can get once you’ve convinced the compiler that a trait object is actually Sized, I noticed that calling size_of does actually still compile without any error. It returns 0, e.g. see this example:

trait IsEqual {
    type To: ?Sized;
}
impl<A: ?Sized> IsEqual for A {
    type To = A;
}

fn main() {
    argument_equal_to_sized_type::<dyn IsEqual<To = u64>>();
}

fn argument_equal_to_sized_type<T: ?Sized>()
where
    <T as IsEqual>::To: Sized,
{
    argument_sized::<T>();
}

fn argument_sized<T>() {
    println!("{}", std::any::type_name::<T>());
    println!("{}", std::mem::size_of::<T>());
}
dyn playground::IsEqual+To = u64
0

I wonder what that value of 0 is good for! Couldn’t it raise an ICE instead?

@rustbot modify labels: T-compiler, C-enhancement, A-dst

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-DSTsArea: Dynamically-sized types (DSTs)C-enhancementCategory: An issue proposing an enhancement or a PR with one.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