Skip to content

Commit

Permalink
Anonymize binders for refining_impl_trait check
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Sep 25, 2023
1 parent af68593 commit 9f47d13
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
22 changes: 20 additions & 2 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -178,7 +178,8 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(

// 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);
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,
Expand Down Expand Up @@ -309,3 +310,20 @@ fn type_visibility<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<ty::Visibili
_ => None,
}
}

struct Anonymize<'tcx> {
tcx: TyCtxt<'tcx>,
}

impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Anonymize<'tcx> {
fn interner(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
where
T: TypeFoldable<TyCtxt<'tcx>>,
{
self.tcx.anonymize_bound_vars(t)
}
}
13 changes: 13 additions & 0 deletions tests/ui/impl-trait/in-trait/anonymize-binders-for-refine.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
fn foo() -> impl for<'a> Tr<&'a Self>;
}

impl<T> Tr<T> for () {
fn foo() -> impl for<'a> Tr<&'a Self> {}
}

0 comments on commit 9f47d13

Please sign in to comment.