Add internal attribute and tests.

This commit is contained in:
Charles Lew 2021-07-17 15:44:19 +08:00
parent d2dc4276fd
commit ab171c5279
7 changed files with 183 additions and 1 deletions

View File

@ -603,6 +603,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
rustc_attr!(TEST, rustc_dump_program_clauses, AssumedUsed, template!(Word)),
rustc_attr!(TEST, rustc_dump_env_program_clauses, AssumedUsed, template!(Word)),
rustc_attr!(TEST, rustc_object_lifetime_default, AssumedUsed, template!(Word)),
rustc_attr!(TEST, rustc_dump_vtable, AssumedUsed, template!(Word)),
rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)),
gated!(
omit_gdb_pretty_printer_section, AssumedUsed, template!(Word),

View File

@ -1047,6 +1047,7 @@ symbols! {
rustc_dump_env_program_clauses,
rustc_dump_program_clauses,
rustc_dump_user_substs,
rustc_dump_vtable,
rustc_error,
rustc_evaluate_where_clauses,
rustc_expected_cgu_reuse,

View File

@ -34,7 +34,7 @@ use rustc_middle::ty::{
self, GenericParamDefKind, ToPredicate, Ty, TyCtxt, VtblEntry, WithConstness,
COMMON_VTABLE_ENTRIES,
};
use rustc_span::Span;
use rustc_span::{sym, Span};
use smallvec::SmallVec;
use std::fmt::Debug;
@ -614,6 +614,11 @@ fn prepare_vtable_segments<'tcx, T>(
}
}
fn dump_vtable_entries<'tcx>(tcx: TyCtxt<'tcx>, entries: &[VtblEntry<'tcx>]) {
let msg = format!("Vtable Entries: {:#?}", entries);
tcx.sess.struct_span_err(rustc_span::DUMMY_SP, &msg).emit();
}
/// Given a trait `trait_ref`, iterates the vtable entries
/// that come from `trait_ref`, including its supertraits.
fn vtable_entries<'tcx>(
@ -691,6 +696,11 @@ fn vtable_entries<'tcx>(
};
let _ = prepare_vtable_segments(tcx, trait_ref, vtable_segment_callback);
if tcx.has_attr(trait_ref.def_id(), sym::rustc_dump_vtable) {
dump_vtable_entries(tcx, &entries);
}
tcx.arena.alloc_from_iter(entries.into_iter())
}

View File

@ -0,0 +1,39 @@
// build-fail
//~^ error Vtable
//~^^ error Vtable
#![feature(rustc_attrs)]
#[rustc_dump_vtable]
trait A {
fn foo_a(&self) {}
}
#[rustc_dump_vtable]
trait B: A {
fn foo_b(&self) {}
}
#[rustc_dump_vtable]
trait C: A {
fn foo_c(&self) {}
}
#[rustc_dump_vtable]
trait D: B + C {
fn foo_d(&self) {}
}
struct S;
impl A for S {}
impl B for S {}
impl C for S {}
impl D for S {}
fn foo(d: &dyn D) {
d.foo_d();
}
fn main() {
foo(&S);
}

View File

@ -0,0 +1,56 @@
error: Vtable Entries: [
MetadataDropInPlace,
MetadataSize,
MetadataAlign,
Method(
DefId(0:4 ~ vtable_diamond[4564]::A::foo_a),
[
S,
],
),
Method(
DefId(0:6 ~ vtable_diamond[4564]::B::foo_b),
[
S,
],
),
Method(
DefId(0:8 ~ vtable_diamond[4564]::C::foo_c),
[
S,
],
),
TraitVPtr(
Binder(
C,
[],
),
),
Method(
DefId(0:10 ~ vtable_diamond[4564]::D::foo_d),
[
S,
],
),
]
error: Vtable Entries: [
MetadataDropInPlace,
MetadataSize,
MetadataAlign,
Method(
DefId(0:4 ~ vtable_diamond[4564]::A::foo_a),
[
S,
],
),
Method(
DefId(0:8 ~ vtable_diamond[4564]::C::foo_c),
[
S,
],
),
]
error: aborting due to 2 previous errors

View File

@ -0,0 +1,31 @@
// build-fail
//~^ error Vtable
//~^^ error Vtable
#![feature(rustc_attrs)]
#[rustc_dump_vtable]
trait A {
fn foo_a(&self) {}
}
#[rustc_dump_vtable]
trait B {
fn foo_b(&self) {}
}
#[rustc_dump_vtable]
trait C: A + B {
fn foo_c(&self) {}
}
struct S;
impl A for S {}
impl B for S {}
impl C for S {}
fn foo(c: &dyn C) {}
fn main() {
foo(&S);
}

View File

@ -0,0 +1,44 @@
error: Vtable Entries: [
MetadataDropInPlace,
MetadataSize,
MetadataAlign,
Method(
DefId(0:4 ~ vtable_multiple[5246]::A::foo_a),
[
S,
],
),
Method(
DefId(0:6 ~ vtable_multiple[5246]::B::foo_b),
[
S,
],
),
TraitVPtr(
Binder(
B,
[],
),
),
Method(
DefId(0:8 ~ vtable_multiple[5246]::C::foo_c),
[
S,
],
),
]
error: Vtable Entries: [
MetadataDropInPlace,
MetadataSize,
MetadataAlign,
Method(
DefId(0:6 ~ vtable_multiple[5246]::B::foo_b),
[
S,
],
),
]
error: aborting due to 2 previous errors