Skip to content

Imperfect vtable layout with an empty super trait comming after non-empty one #114942

Closed
@WaffleLapkin

Description

@WaffleLapkin

I tried this code:

#![feature(rustc_attrs)]
#![allow(internal_features)]

#[rustc_dump_vtable]
trait Marker {}
trait Pencil { fn f(&self) {} }

#[rustc_dump_vtable]
trait Imperfection: Pencil + Marker {}

struct T;
impl Marker for T {}
impl Pencil for T {}
impl Imperfection for T {}

fn main() {
    (&T as &dyn Imperfection).f();
    let _a = &T as &dyn Marker;
}

(play)

I expected Imperfection to have vtable layout which is DSA, Pencil::f.

Instead, it also includes vptr(Marker). Note that this is unnecessary as Marker's vtable only contains DSA triplet and Imperfection's vtable can be casted to Marker's. Ideally we should never emit vptrs to empty traits.

error: vtable entries for `<T as Imperfection>`: [
           MetadataDropInPlace,
           MetadataSize,
           MetadataAlign,
           Method(<T as Pencil>::f),
           TraitVPtr(<T as Marker>),
       ]
 --> src/main.rs:9:1
  |
9 | trait Imperfection: Pencil + Marker {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: vtable entries for `<T as Marker>`: [
           MetadataDropInPlace,
           MetadataSize,
           MetadataAlign,
       ]
 --> src/main.rs:5:1
  |
5 | trait Marker {}
  | ^^^^^^^^^^^^

This is a continuation of #113840, inspired by #113856 (comment); #113856 missed this case.

Meta

rustc version:

1.73.0-nightly (2023-08-16 07438b0928c6691d6ee7)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-dyn-traitArea: trait objects, vtable layoutC-enhancementCategory: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchF-trait_upcasting`#![feature(trait_upcasting)]`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