Skip to content

Commit fbb353a

Browse files
committed
Add comment and more tests.
1 parent 6346387 commit fbb353a

File tree

5 files changed

+390
-0
lines changed

5 files changed

+390
-0
lines changed

compiler/rustc_middle/src/ty/vtable.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ use rustc_ast::Mutability;
77

88
#[derive(Clone, Copy, PartialEq, HashStable)]
99
pub enum VtblEntry<'tcx> {
10+
/// destructor of this type (used in vtable header)
1011
MetadataDropInPlace,
12+
/// layout size of this type (used in vtable header)
1113
MetadataSize,
14+
/// layout align of this type (used in vtable header)
1215
MetadataAlign,
16+
/// non-dispatchable associated function that is excluded from trait object
1317
Vacant,
18+
/// dispatchable associated function
1419
Method(Instance<'tcx>),
20+
/// pointer to a separate supertrait vtable, can be used by trait upcasting coercion
1521
TraitVPtr(PolyTraitRef<'tcx>),
1622
}
1723

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// build-fail
2+
#![feature(rustc_attrs)]
3+
4+
// O --> G --> C --> A
5+
// \ \ \-> B
6+
// | |-> F --> D
7+
// | \-> E
8+
// |-> N --> J --> H
9+
// \ \-> I
10+
// |-> M --> K
11+
// \-> L
12+
13+
#[rustc_dump_vtable]
14+
trait A {
15+
fn foo_a(&self) {}
16+
}
17+
18+
#[rustc_dump_vtable]
19+
trait B {
20+
//~^ error Vtable
21+
fn foo_b(&self) {}
22+
}
23+
24+
#[rustc_dump_vtable]
25+
trait C: A + B {
26+
fn foo_c(&self) {}
27+
}
28+
29+
#[rustc_dump_vtable]
30+
trait D {
31+
//~^ error Vtable
32+
fn foo_d(&self) {}
33+
}
34+
35+
#[rustc_dump_vtable]
36+
trait E {
37+
//~^ error Vtable
38+
fn foo_e(&self) {}
39+
}
40+
41+
#[rustc_dump_vtable]
42+
trait F: D + E {
43+
//~^ error Vtable
44+
fn foo_f(&self) {}
45+
}
46+
47+
#[rustc_dump_vtable]
48+
trait G: C + F {
49+
fn foo_g(&self) {}
50+
}
51+
52+
#[rustc_dump_vtable]
53+
trait H {
54+
//~^ error Vtable
55+
fn foo_h(&self) {}
56+
}
57+
58+
#[rustc_dump_vtable]
59+
trait I {
60+
//~^ error Vtable
61+
fn foo_i(&self) {}
62+
}
63+
64+
#[rustc_dump_vtable]
65+
trait J: H + I {
66+
//~^ error Vtable
67+
fn foo_j(&self) {}
68+
}
69+
70+
#[rustc_dump_vtable]
71+
trait K {
72+
//~^ error Vtable
73+
fn foo_k(&self) {}
74+
}
75+
76+
#[rustc_dump_vtable]
77+
trait L {
78+
//~^ error Vtable
79+
fn foo_l(&self) {}
80+
}
81+
82+
#[rustc_dump_vtable]
83+
trait M: K + L {
84+
//~^ error Vtable
85+
fn foo_m(&self) {}
86+
}
87+
88+
#[rustc_dump_vtable]
89+
trait N: J + M {
90+
//~^ error Vtable
91+
fn foo_n(&self) {}
92+
}
93+
94+
#[rustc_dump_vtable]
95+
trait O: G + N {
96+
//~^ error Vtable
97+
fn foo_o(&self) {}
98+
}
99+
100+
struct S;
101+
102+
impl A for S {}
103+
impl B for S {}
104+
impl C for S {}
105+
impl D for S {}
106+
impl E for S {}
107+
impl F for S {}
108+
impl G for S {}
109+
impl H for S {}
110+
impl I for S {}
111+
impl J for S {}
112+
impl K for S {}
113+
impl L for S {}
114+
impl M for S {}
115+
impl N for S {}
116+
impl O for S {}
117+
118+
fn foo(_: &dyn O) {}
119+
120+
fn main() {
121+
foo(&S);
122+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
error: Vtable entries for `<S as O>`: [
2+
MetadataDropInPlace,
3+
MetadataSize,
4+
MetadataAlign,
5+
Method(<S as A>::foo_a),
6+
Method(<S as B>::foo_b),
7+
TraitVPtr(<S as B>),
8+
Method(<S as C>::foo_c),
9+
Method(<S as D>::foo_d),
10+
TraitVPtr(<S as D>),
11+
Method(<S as E>::foo_e),
12+
TraitVPtr(<S as E>),
13+
Method(<S as F>::foo_f),
14+
TraitVPtr(<S as F>),
15+
Method(<S as G>::foo_g),
16+
Method(<S as H>::foo_h),
17+
TraitVPtr(<S as H>),
18+
Method(<S as I>::foo_i),
19+
TraitVPtr(<S as I>),
20+
Method(<S as J>::foo_j),
21+
TraitVPtr(<S as J>),
22+
Method(<S as K>::foo_k),
23+
TraitVPtr(<S as K>),
24+
Method(<S as L>::foo_l),
25+
TraitVPtr(<S as L>),
26+
Method(<S as M>::foo_m),
27+
TraitVPtr(<S as M>),
28+
Method(<S as N>::foo_n),
29+
TraitVPtr(<S as N>),
30+
Method(<S as O>::foo_o),
31+
]
32+
--> $DIR/vtable-multi-level.rs:95:1
33+
|
34+
LL | / trait O: G + N {
35+
LL | |
36+
LL | | fn foo_o(&self) {}
37+
LL | | }
38+
| |_^
39+
40+
error: Vtable entries for `<S as B>`: [
41+
MetadataDropInPlace,
42+
MetadataSize,
43+
MetadataAlign,
44+
Method(<S as B>::foo_b),
45+
]
46+
--> $DIR/vtable-multi-level.rs:19:1
47+
|
48+
LL | / trait B {
49+
LL | |
50+
LL | | fn foo_b(&self) {}
51+
LL | | }
52+
| |_^
53+
54+
error: Vtable entries for `<S as D>`: [
55+
MetadataDropInPlace,
56+
MetadataSize,
57+
MetadataAlign,
58+
Method(<S as D>::foo_d),
59+
]
60+
--> $DIR/vtable-multi-level.rs:30:1
61+
|
62+
LL | / trait D {
63+
LL | |
64+
LL | | fn foo_d(&self) {}
65+
LL | | }
66+
| |_^
67+
68+
error: Vtable entries for `<S as E>`: [
69+
MetadataDropInPlace,
70+
MetadataSize,
71+
MetadataAlign,
72+
Method(<S as E>::foo_e),
73+
]
74+
--> $DIR/vtable-multi-level.rs:36:1
75+
|
76+
LL | / trait E {
77+
LL | |
78+
LL | | fn foo_e(&self) {}
79+
LL | | }
80+
| |_^
81+
82+
error: Vtable entries for `<S as F>`: [
83+
MetadataDropInPlace,
84+
MetadataSize,
85+
MetadataAlign,
86+
Method(<S as D>::foo_d),
87+
Method(<S as E>::foo_e),
88+
TraitVPtr(<S as E>),
89+
Method(<S as F>::foo_f),
90+
]
91+
--> $DIR/vtable-multi-level.rs:42:1
92+
|
93+
LL | / trait F: D + E {
94+
LL | |
95+
LL | | fn foo_f(&self) {}
96+
LL | | }
97+
| |_^
98+
99+
error: Vtable entries for `<S as H>`: [
100+
MetadataDropInPlace,
101+
MetadataSize,
102+
MetadataAlign,
103+
Method(<S as H>::foo_h),
104+
]
105+
--> $DIR/vtable-multi-level.rs:53:1
106+
|
107+
LL | / trait H {
108+
LL | |
109+
LL | | fn foo_h(&self) {}
110+
LL | | }
111+
| |_^
112+
113+
error: Vtable entries for `<S as I>`: [
114+
MetadataDropInPlace,
115+
MetadataSize,
116+
MetadataAlign,
117+
Method(<S as I>::foo_i),
118+
]
119+
--> $DIR/vtable-multi-level.rs:59:1
120+
|
121+
LL | / trait I {
122+
LL | |
123+
LL | | fn foo_i(&self) {}
124+
LL | | }
125+
| |_^
126+
127+
error: Vtable entries for `<S as J>`: [
128+
MetadataDropInPlace,
129+
MetadataSize,
130+
MetadataAlign,
131+
Method(<S as H>::foo_h),
132+
Method(<S as I>::foo_i),
133+
TraitVPtr(<S as I>),
134+
Method(<S as J>::foo_j),
135+
]
136+
--> $DIR/vtable-multi-level.rs:65:1
137+
|
138+
LL | / trait J: H + I {
139+
LL | |
140+
LL | | fn foo_j(&self) {}
141+
LL | | }
142+
| |_^
143+
144+
error: Vtable entries for `<S as K>`: [
145+
MetadataDropInPlace,
146+
MetadataSize,
147+
MetadataAlign,
148+
Method(<S as K>::foo_k),
149+
]
150+
--> $DIR/vtable-multi-level.rs:71:1
151+
|
152+
LL | / trait K {
153+
LL | |
154+
LL | | fn foo_k(&self) {}
155+
LL | | }
156+
| |_^
157+
158+
error: Vtable entries for `<S as L>`: [
159+
MetadataDropInPlace,
160+
MetadataSize,
161+
MetadataAlign,
162+
Method(<S as L>::foo_l),
163+
]
164+
--> $DIR/vtable-multi-level.rs:77:1
165+
|
166+
LL | / trait L {
167+
LL | |
168+
LL | | fn foo_l(&self) {}
169+
LL | | }
170+
| |_^
171+
172+
error: Vtable entries for `<S as M>`: [
173+
MetadataDropInPlace,
174+
MetadataSize,
175+
MetadataAlign,
176+
Method(<S as K>::foo_k),
177+
Method(<S as L>::foo_l),
178+
TraitVPtr(<S as L>),
179+
Method(<S as M>::foo_m),
180+
]
181+
--> $DIR/vtable-multi-level.rs:83:1
182+
|
183+
LL | / trait M: K + L {
184+
LL | |
185+
LL | | fn foo_m(&self) {}
186+
LL | | }
187+
| |_^
188+
189+
error: Vtable entries for `<S as N>`: [
190+
MetadataDropInPlace,
191+
MetadataSize,
192+
MetadataAlign,
193+
Method(<S as H>::foo_h),
194+
Method(<S as I>::foo_i),
195+
TraitVPtr(<S as I>),
196+
Method(<S as J>::foo_j),
197+
Method(<S as K>::foo_k),
198+
TraitVPtr(<S as K>),
199+
Method(<S as L>::foo_l),
200+
TraitVPtr(<S as L>),
201+
Method(<S as M>::foo_m),
202+
TraitVPtr(<S as M>),
203+
Method(<S as N>::foo_n),
204+
]
205+
--> $DIR/vtable-multi-level.rs:89:1
206+
|
207+
LL | / trait N: J + M {
208+
LL | |
209+
LL | | fn foo_n(&self) {}
210+
LL | | }
211+
| |_^
212+
213+
error: aborting due to 12 previous errors
214+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// build-fail
2+
#![feature(rustc_attrs)]
3+
4+
// B --> A
5+
6+
#[rustc_dump_vtable]
7+
trait A {
8+
fn foo_a1(&self) {}
9+
fn foo_a2(&self) where Self: Sized {}
10+
}
11+
12+
#[rustc_dump_vtable]
13+
trait B: A {
14+
//~^ error Vtable
15+
fn foo_b1(&self) {}
16+
fn foo_b2() where Self: Sized {}
17+
}
18+
19+
struct S;
20+
21+
impl A for S {}
22+
impl B for S {}
23+
24+
fn foo(_: &dyn B) {}
25+
26+
fn main() {
27+
foo(&S);
28+
}

0 commit comments

Comments
 (0)