Skip to content

Commit 00bc9c9

Browse files
committed
Add internal attribute and tests.
1 parent f67845a commit 00bc9c9

File tree

7 files changed

+183
-1
lines changed

7 files changed

+183
-1
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
603603
rustc_attr!(TEST, rustc_dump_program_clauses, AssumedUsed, template!(Word)),
604604
rustc_attr!(TEST, rustc_dump_env_program_clauses, AssumedUsed, template!(Word)),
605605
rustc_attr!(TEST, rustc_object_lifetime_default, AssumedUsed, template!(Word)),
606+
rustc_attr!(TEST, rustc_dump_vtable, AssumedUsed, template!(Word)),
606607
rustc_attr!(TEST, rustc_dummy, Normal, template!(Word /* doesn't matter*/)),
607608
gated!(
608609
omit_gdb_pretty_printer_section, AssumedUsed, template!(Word),

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ symbols! {
10251025
rustc_dump_env_program_clauses,
10261026
rustc_dump_program_clauses,
10271027
rustc_dump_user_substs,
1028+
rustc_dump_vtable,
10281029
rustc_error,
10291030
rustc_evaluate_where_clauses,
10301031
rustc_expected_cgu_reuse,

compiler/rustc_trait_selection/src/traits/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_middle::ty::{
3434
self, GenericParamDefKind, ToPredicate, Ty, TyCtxt, VtblEntry, WithConstness,
3535
COMMON_VTABLE_ENTRIES,
3636
};
37-
use rustc_span::Span;
37+
use rustc_span::{sym, Span};
3838
use smallvec::SmallVec;
3939

4040
use std::fmt::Debug;
@@ -614,6 +614,11 @@ fn prepare_vtable_segments<'tcx, T>(
614614
}
615615
}
616616

617+
fn dump_vtable_entries<'tcx>(tcx: TyCtxt<'tcx>, entries: &[VtblEntry<'tcx>]) {
618+
let msg = format!("Vtable Entries: {:#?}", entries);
619+
tcx.sess.struct_span_err(rustc_span::DUMMY_SP, &msg).emit();
620+
}
621+
617622
/// Given a trait `trait_ref`, iterates the vtable entries
618623
/// that come from `trait_ref`, including its supertraits.
619624
fn vtable_entries<'tcx>(
@@ -691,6 +696,11 @@ fn vtable_entries<'tcx>(
691696
};
692697

693698
let _ = prepare_vtable_segments(tcx, trait_ref, vtable_segment_callback);
699+
700+
if tcx.has_attr(trait_ref.def_id(), sym::rustc_dump_vtable) {
701+
dump_vtable_entries(tcx, &entries);
702+
}
703+
694704
tcx.arena.alloc_from_iter(entries.into_iter())
695705
}
696706

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// build-fail
2+
//~^ error Vtable
3+
//~^^ error Vtable
4+
#![feature(rustc_attrs)]
5+
6+
#[rustc_dump_vtable]
7+
trait A {
8+
fn foo_a(&self) {}
9+
}
10+
11+
#[rustc_dump_vtable]
12+
trait B: A {
13+
fn foo_b(&self) {}
14+
}
15+
16+
#[rustc_dump_vtable]
17+
trait C: A {
18+
fn foo_c(&self) {}
19+
}
20+
21+
#[rustc_dump_vtable]
22+
trait D: B + C {
23+
fn foo_d(&self) {}
24+
}
25+
26+
struct S;
27+
28+
impl A for S {}
29+
impl B for S {}
30+
impl C for S {}
31+
impl D for S {}
32+
33+
fn foo(d: &dyn D) {
34+
d.foo_d();
35+
}
36+
37+
fn main() {
38+
foo(&S);
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error: Vtable Entries: [
2+
MetadataDropInPlace,
3+
MetadataSize,
4+
MetadataAlign,
5+
Method(
6+
DefId(0:4 ~ vtable_diamond[4564]::A::foo_a),
7+
[
8+
S,
9+
],
10+
),
11+
Method(
12+
DefId(0:6 ~ vtable_diamond[4564]::B::foo_b),
13+
[
14+
S,
15+
],
16+
),
17+
Method(
18+
DefId(0:8 ~ vtable_diamond[4564]::C::foo_c),
19+
[
20+
S,
21+
],
22+
),
23+
TraitVPtr(
24+
Binder(
25+
C,
26+
[],
27+
),
28+
),
29+
Method(
30+
DefId(0:10 ~ vtable_diamond[4564]::D::foo_d),
31+
[
32+
S,
33+
],
34+
),
35+
]
36+
37+
error: Vtable Entries: [
38+
MetadataDropInPlace,
39+
MetadataSize,
40+
MetadataAlign,
41+
Method(
42+
DefId(0:4 ~ vtable_diamond[4564]::A::foo_a),
43+
[
44+
S,
45+
],
46+
),
47+
Method(
48+
DefId(0:8 ~ vtable_diamond[4564]::C::foo_c),
49+
[
50+
S,
51+
],
52+
),
53+
]
54+
55+
error: aborting due to 2 previous errors
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// build-fail
2+
//~^ error Vtable
3+
//~^^ error Vtable
4+
#![feature(rustc_attrs)]
5+
6+
#[rustc_dump_vtable]
7+
trait A {
8+
fn foo_a(&self) {}
9+
}
10+
11+
#[rustc_dump_vtable]
12+
trait B {
13+
fn foo_b(&self) {}
14+
}
15+
16+
#[rustc_dump_vtable]
17+
trait C: A + B {
18+
fn foo_c(&self) {}
19+
}
20+
21+
struct S;
22+
23+
impl A for S {}
24+
impl B for S {}
25+
impl C for S {}
26+
27+
fn foo(c: &dyn C) {}
28+
29+
fn main() {
30+
foo(&S);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error: Vtable Entries: [
2+
MetadataDropInPlace,
3+
MetadataSize,
4+
MetadataAlign,
5+
Method(
6+
DefId(0:4 ~ vtable_multiple[5246]::A::foo_a),
7+
[
8+
S,
9+
],
10+
),
11+
Method(
12+
DefId(0:6 ~ vtable_multiple[5246]::B::foo_b),
13+
[
14+
S,
15+
],
16+
),
17+
TraitVPtr(
18+
Binder(
19+
B,
20+
[],
21+
),
22+
),
23+
Method(
24+
DefId(0:8 ~ vtable_multiple[5246]::C::foo_c),
25+
[
26+
S,
27+
],
28+
),
29+
]
30+
31+
error: Vtable Entries: [
32+
MetadataDropInPlace,
33+
MetadataSize,
34+
MetadataAlign,
35+
Method(
36+
DefId(0:6 ~ vtable_multiple[5246]::B::foo_b),
37+
[
38+
S,
39+
],
40+
),
41+
]
42+
43+
error: aborting due to 2 previous errors
44+

0 commit comments

Comments
 (0)