From 305524d1d655f744e739f45850bc6f74918ac0c9 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 25 Sep 2023 15:00:38 +0000 Subject: [PATCH] Anonymize binders for refining_impl_trait check --- .../src/check/compare_impl_item/refine.rs | 29 ++++++++++++++++--- .../in-trait/anonymize-binders-for-refine.rs | 13 +++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 tests/ui/impl-trait/in-trait/anonymize-binders-for-refine.rs diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs index a8149b634ef..d9e0e87eb47 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs @@ -5,7 +5,7 @@ use rustc_infer::infer::{outlives::env::OutlivesEnvironment, TyCtxtInferExt}; use rustc_lint_defs::builtin::REFINING_IMPL_TRAIT; use rustc_middle::traits::{ObligationCause, Reveal}; use rustc_middle::ty::{ - self, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitor, + self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor, }; use rustc_span::{Span, DUMMY_SP}; use rustc_trait_selection::traits::{ @@ -176,9 +176,13 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( return; }; - // For quicker lookup, use an `IndexSet` - // (we don't use one earlier because it's not foldable..) - let trait_bounds = FxIndexSet::from_iter(trait_bounds); + // For quicker lookup, use an `IndexSet` (we don't use one earlier because + // it's not foldable..). + // Also, We have to anonymize binders in these types because they may contain + // `BrNamed` bound vars, which contain unique `DefId`s which correspond to syntax + // locations that we don't care about when checking bound equality. + let trait_bounds = FxIndexSet::from_iter(trait_bounds.fold_with(&mut Anonymize { tcx })); + let impl_bounds = impl_bounds.fold_with(&mut Anonymize { tcx }); // Find any clauses that are present in the impl's RPITITs that are not // present in the trait's RPITITs. This will trigger on trivial predicates, @@ -309,3 +313,20 @@ fn type_visibility<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option None, } } + +struct Anonymize<'tcx> { + tcx: TyCtxt<'tcx>, +} + +impl<'tcx> TypeFolder> for Anonymize<'tcx> { + fn interner(&self) -> TyCtxt<'tcx> { + self.tcx + } + + fn fold_binder(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T> + where + T: TypeFoldable>, + { + self.tcx.anonymize_bound_vars(t) + } +} diff --git a/tests/ui/impl-trait/in-trait/anonymize-binders-for-refine.rs b/tests/ui/impl-trait/in-trait/anonymize-binders-for-refine.rs new file mode 100644 index 00000000000..e62662f2f07 --- /dev/null +++ b/tests/ui/impl-trait/in-trait/anonymize-binders-for-refine.rs @@ -0,0 +1,13 @@ +// compile-flags: --crate-type=lib +// check-pass + +#![feature(return_position_impl_trait_in_trait)] +#![deny(refining_impl_trait)] + +pub trait Tr { + fn foo() -> impl for<'a> Tr<&'a Self>; +} + +impl Tr for () { + fn foo() -> impl for<'a> Tr<&'a Self> {} +}