diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 428873b8d3d..8f247184e88 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 00000000000..b151a9d3ab6 --- /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); +}