Skip to content

Commit 305524d

Browse files
Anonymize binders for refining_impl_trait check
1 parent 21627d6 commit 305524d

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_infer::infer::{outlives::env::OutlivesEnvironment, TyCtxtInferExt};
55
use rustc_lint_defs::builtin::REFINING_IMPL_TRAIT;
66
use rustc_middle::traits::{ObligationCause, Reveal};
77
use rustc_middle::ty::{
8-
self, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable, TypeVisitor,
8+
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor,
99
};
1010
use rustc_span::{Span, DUMMY_SP};
1111
use rustc_trait_selection::traits::{
@@ -176,9 +176,13 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>(
176176
return;
177177
};
178178

179-
// For quicker lookup, use an `IndexSet`
180-
// (we don't use one earlier because it's not foldable..)
181-
let trait_bounds = FxIndexSet::from_iter(trait_bounds);
179+
// For quicker lookup, use an `IndexSet` (we don't use one earlier because
180+
// it's not foldable..).
181+
// Also, We have to anonymize binders in these types because they may contain
182+
// `BrNamed` bound vars, which contain unique `DefId`s which correspond to syntax
183+
// locations that we don't care about when checking bound equality.
184+
let trait_bounds = FxIndexSet::from_iter(trait_bounds.fold_with(&mut Anonymize { tcx }));
185+
let impl_bounds = impl_bounds.fold_with(&mut Anonymize { tcx });
182186

183187
// Find any clauses that are present in the impl's RPITITs that are not
184188
// 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<ty::Visibili
309313
_ => None,
310314
}
311315
}
316+
317+
struct Anonymize<'tcx> {
318+
tcx: TyCtxt<'tcx>,
319+
}
320+
321+
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for Anonymize<'tcx> {
322+
fn interner(&self) -> TyCtxt<'tcx> {
323+
self.tcx
324+
}
325+
326+
fn fold_binder<T>(&mut self, t: ty::Binder<'tcx, T>) -> ty::Binder<'tcx, T>
327+
where
328+
T: TypeFoldable<TyCtxt<'tcx>>,
329+
{
330+
self.tcx.anonymize_bound_vars(t)
331+
}
332+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// compile-flags: --crate-type=lib
2+
// check-pass
3+
4+
#![feature(return_position_impl_trait_in_trait)]
5+
#![deny(refining_impl_trait)]
6+
7+
pub trait Tr<T> {
8+
fn foo() -> impl for<'a> Tr<&'a Self>;
9+
}
10+
11+
impl<T> Tr<T> for () {
12+
fn foo() -> impl for<'a> Tr<&'a Self> {}
13+
}

0 commit comments

Comments
 (0)