Skip to content

Commit 76c4711

Browse files
committed
Never emit vptr for empty/auto traits
1 parent 88f3114 commit 76c4711

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/vtable.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,17 @@ fn prepare_vtable_segments_inner<'tcx, T>(
154154

155155
// emit innermost item, move to next sibling and stop there if possible, otherwise jump to outer level.
156156
while let Some((inner_most_trait_ref, emit_vptr, mut siblings)) = stack.pop() {
157+
let has_entries =
158+
has_own_existential_vtable_entries(tcx, inner_most_trait_ref.def_id());
159+
157160
segment_visitor(VtblSegment::TraitOwnEntries {
158161
trait_ref: inner_most_trait_ref,
159-
emit_vptr: emit_vptr && !tcx.sess.opts.unstable_opts.no_trait_vptr,
162+
emit_vptr: emit_vptr && has_entries && !tcx.sess.opts.unstable_opts.no_trait_vptr,
160163
})?;
161164

162165
// If we've emitted (fed to `segment_visitor`) a trait that has methods present in the vtable,
163166
// we'll need to emit vptrs from now on.
164-
if !emit_vptr_on_new_entry
165-
&& has_own_existential_vtable_entries(tcx, inner_most_trait_ref.def_id())
166-
{
167-
emit_vptr_on_new_entry = true;
168-
}
167+
emit_vptr_on_new_entry |= has_entries;
169168

170169
if let Some(next_inner_most_trait_ref) =
171170
siblings.find(|&sibling| visited.insert(sibling.upcast(tcx)))

Diff for: tests/ui/traits/upcast_reorder.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-pass
2+
//
3+
// issue: <https://github.com/rust-lang/rust/issues/131813>
4+
5+
#![feature(trait_upcasting)]
6+
7+
trait Pollable {
8+
#[allow(unused)]
9+
fn poll(&self) {}
10+
}
11+
trait FileIo: Pollable + Send + Sync {
12+
fn read(&self) {}
13+
}
14+
trait Terminal: Send + Sync + FileIo {}
15+
16+
struct A;
17+
18+
impl Pollable for A {}
19+
impl FileIo for A {}
20+
impl Terminal for A {}
21+
22+
fn main() {
23+
let a = A;
24+
25+
let b = &a as &dyn Terminal;
26+
let c = b as &dyn FileIo;
27+
28+
c.read();
29+
}

Diff for: tests/ui/traits/vtable/multiple-markers.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ error: vtable entries for `<S as B>`: [
1414
MetadataSize,
1515
MetadataAlign,
1616
Method(<S as T>::method),
17-
TraitVPtr(<S as M2>),
1817
]
1918
--> $DIR/multiple-markers.rs:24:1
2019
|
@@ -26,8 +25,6 @@ error: vtable entries for `<S as C>`: [
2625
MetadataSize,
2726
MetadataAlign,
2827
Method(<S as T>::method),
29-
TraitVPtr(<S as M1>),
30-
TraitVPtr(<S as M2>),
3128
]
3229
--> $DIR/multiple-markers.rs:27:1
3330
|
@@ -39,9 +36,6 @@ error: vtable entries for `<S as D>`: [
3936
MetadataSize,
4037
MetadataAlign,
4138
Method(<S as T>::method),
42-
TraitVPtr(<S as M0>),
43-
TraitVPtr(<S as M1>),
44-
TraitVPtr(<S as M2>),
4539
]
4640
--> $DIR/multiple-markers.rs:30:1
4741
|

0 commit comments

Comments
 (0)