From 309e371f7b062865199c38cfebe00173698c0aad Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 15 Feb 2025 20:38:08 +0000 Subject: [PATCH] Ignore Self in bounds check for associated types with Self:Sized --- .../src/traits/dyn_compatibility.rs | 3 +++ .../associated_type_bound_mentions_self.rs | 14 ++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/ui/dyn-compatibility/associated_type_bound_mentions_self.rs diff --git a/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs b/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs index 617bc87a9d23c..a0df74835bc1c 100644 --- a/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs +++ b/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs @@ -187,7 +187,10 @@ fn predicates_reference_self( fn bounds_reference_self(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span; 1]> { tcx.associated_items(trait_def_id) .in_definition_order() + // We're only looking at associated type bounds .filter(|item| item.kind == ty::AssocKind::Type) + // Ignore GATs with `Self: Sized` + .filter(|item| !tcx.generics_require_sized_self(item.def_id)) .flat_map(|item| tcx.explicit_item_bounds(item.def_id).iter_identity_copied()) .filter_map(|(clause, sp)| { // Item bounds *can* have self projections, since they never get diff --git a/tests/ui/dyn-compatibility/associated_type_bound_mentions_self.rs b/tests/ui/dyn-compatibility/associated_type_bound_mentions_self.rs new file mode 100644 index 0000000000000..0be5fa27b4cc1 --- /dev/null +++ b/tests/ui/dyn-compatibility/associated_type_bound_mentions_self.rs @@ -0,0 +1,14 @@ +// Ensure that we properly ignore the `B` associated type bound on `A::T` +// since that associated type requires `Self: Sized`. + +//@ check-pass + +struct X(&'static dyn A); + +trait A { + type T: B where Self: Sized; +} + +trait B {} + +fn main() {}