Skip to content

Type:of does not support types without a size #151018

@BD103

Description

@BD103

I tried this code:

#![feature(type_info)]

use std::mem::type_info::Type;

fn main() {
    let type_ = const { Type::of::<str>() };
}

I expected this to compile successfully.

Instead, I received an error that Type::of doesn't support unsized types:

error[E0277]: the size for values of type `str` cannot be known at compilation time
  --> ~/dev/rust-lang/rust/tests/ui/reflection/size.rs:11:37
   |
LL |     let _type_ = const { Type::of::<str>() };
   |                                     ^^^ doesn't have a size known at compile-time
   |
   = help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Type::of`

I believe this occurs because Type::of doesn't have an ?Sized bound. As a temporary workaround, I used TypeId::of first, and then accessed the info from there:

let type_ = const { TypeId::of::<str>().info() };

Meta

This is on the main branch (commit 1377169). This uses the type_info feature (#146922), and is related to #146923.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemC-bugCategory: This is a bug.F-type_info#![feature(type_info)]T-libsRelevant to the library team, which will review and decide on the PR/issue.T-typesRelevant to the types 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