Skip to content

Old vtable example fails with: "error: untyped pointers are not allowed in constant" #77995

Open
@dimpolo

Description

@dimpolo

While trying to play around with raw::TraitObject I found this thread internals.rust-lang.org/... and in particular this code example play.rust-lang.org/... which does not work anymore.

Code

I tried this code:

#![feature(raw)]

use std::{mem, ptr, raw};

pub trait Trait {}

struct Struct;
impl Trait for Struct {}

#[allow(dead_code)]
const STRUCT_AS_DYN_TRAIT_VTABLE: *mut () =
    unsafe { mem::transmute::<*const dyn Trait, raw::TraitObject>(ptr::null::<Struct>()).vtable };

I expected to see this happen:
I get the vtable as a const.

Instead, this happened:
error: untyped pointers are not allowed in constant

Version it worked on

Probably before c400f75
Edit: added PR number #71665

Version with regression

rustc 1.49.0-nightly (e160e5c 2020-10-14)

Any chance we can get the example code to compile again?
Is there a different way to get the vtable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-raw-pointersArea: raw pointers, MaybeUninit, NonNullA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions