From 8841204cc66f66e0e090b4a0c876e861e4cc75f7 Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Wed, 3 Nov 2021 23:37:50 +0800 Subject: [PATCH] Erase regions within `vtable_trait_first_method_offset`. --- .../rustc_trait_selection/src/traits/mod.rs | 5 ++- src/test/ui/hrtb/issue-90177.rs | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/hrtb/issue-90177.rs diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 428873b8d3dda..8f247184e8808 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -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; @@ -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); diff --git a/src/test/ui/hrtb/issue-90177.rs b/src/test/ui/hrtb/issue-90177.rs new file mode 100644 index 0000000000000..b151a9d3ab659 --- /dev/null +++ b/src/test/ui/hrtb/issue-90177.rs @@ -0,0 +1,32 @@ +// check-pass + +trait Base<'f> { + type Assoc; + + fn do_something(&self); +} + +trait ForAnyLifetime: for<'f> Base<'f> {} + +impl 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); +}