Skip to content

Commit

Permalink
Auto merge of #90536 - crlf0710:fix_vtable_hrtb, r=jackh726
Browse files Browse the repository at this point in the history
Erase regions within `vtable_trait_first_method_offset`

Fixes #90177 .

r? `@jackh726`
  • Loading branch information
bors committed Nov 4, 2021
2 parents 4961b10 + 8841204 commit 2cff30b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/rustc_trait_selection/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@ fn vtable_trait_first_method_offset<'tcx>(
) -> usize {
let (trait_to_be_found, trait_owning_vtable) = key;

// #90177
let trait_to_be_found_erased = tcx.erase_regions(trait_to_be_found);

let vtable_segment_callback = {
let mut vtable_base = 0;

Expand All @@ -757,7 +760,7 @@ fn vtable_trait_first_method_offset<'tcx>(
vtable_base += COMMON_VTABLE_ENTRIES.len();
}
VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
if trait_ref == trait_to_be_found {
if tcx.erase_regions(trait_ref) == trait_to_be_found_erased {
return ControlFlow::Break(vtable_base);
}
vtable_base += util::count_own_vtable_entries(tcx, trait_ref);
Expand Down
32 changes: 32 additions & 0 deletions src/test/ui/hrtb/issue-90177.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// check-pass

trait Base<'f> {
type Assoc;

fn do_something(&self);
}

trait ForAnyLifetime: for<'f> Base<'f> {}

impl<T> ForAnyLifetime for T where T: for<'f> Base<'f> {}

trait CanBeDynamic: ForAnyLifetime + for<'f> Base<'f, Assoc = ()> {}

fn foo(a: &dyn CanBeDynamic) {
a.do_something();
}

struct S;

impl<'a> Base<'a> for S {
type Assoc = ();

fn do_something(&self) {}
}

impl CanBeDynamic for S {}

fn main() {
let s = S;
foo(&s);
}

0 comments on commit 2cff30b

Please sign in to comment.