Skip to content

Commit c3fce8e

Browse files
committed
anonymize all bound vars, not just regions
1 parent fd59d05 commit c3fce8e

File tree

7 files changed

+95
-13
lines changed

7 files changed

+95
-13
lines changed

compiler/rustc_infer/src/infer/canonical/substitute.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ where
7171
if var_values.var_values.is_empty() {
7272
value
7373
} else {
74-
let delegate = FnMutDelegate {
74+
let mut delegate = FnMutDelegate {
7575
regions: |br: ty::BoundRegion| match var_values.var_values[br.var].unpack() {
7676
GenericArgKind::Lifetime(l) => l,
7777
r => bug!("{:?} is a region but value is {:?}", br, r),
@@ -86,6 +86,6 @@ where
8686
},
8787
};
8888

89-
tcx.replace_escaping_bound_vars_uncached(value, delegate)
89+
tcx.replace_escaping_bound_vars_uncached(value, &mut delegate)
9090
}
9191
}

compiler/rustc_infer/src/traits/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn anonymize_predicate<'tcx>(
1111
tcx: TyCtxt<'tcx>,
1212
pred: ty::Predicate<'tcx>,
1313
) -> ty::Predicate<'tcx> {
14-
let new = tcx.anonymize_late_bound_regions(pred.kind());
14+
let new = tcx.anonymize_bound_vars(pred.kind());
1515
tcx.reuse_or_mk_predicate(pred, new)
1616
}
1717

@@ -334,7 +334,7 @@ pub fn transitive_bounds_that_define_assoc_type<'tcx>(
334334

335335
std::iter::from_fn(move || {
336336
while let Some(trait_ref) = stack.pop() {
337-
let anon_trait_ref = tcx.anonymize_late_bound_regions(trait_ref);
337+
let anon_trait_ref = tcx.anonymize_bound_vars(trait_ref);
338338
if visited.insert(anon_trait_ref) {
339339
let super_predicates = tcx.super_predicates_that_define_assoc_type((
340340
trait_ref.def_id(),

compiler/rustc_middle/src/ty/erase_regions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'tcx> TypeFolder<'tcx> for RegionEraserVisitor<'tcx> {
4949
where
5050
T: TypeFoldable<'tcx>,
5151
{
52-
let u = self.tcx.anonymize_late_bound_regions(t);
52+
let u = self.tcx.anonymize_bound_vars(t);
5353
u.super_fold_with(self)
5454
}
5555

compiler/rustc_middle/src/ty/fold.rs

+51-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
//! - u.fold_with(folder)
4545
//! ```
4646
use crate::mir;
47-
use crate::ty::{self, Binder, Ty, TyCtxt, TypeVisitable};
47+
use crate::ty::{self, Binder, BoundTy, Ty, TyCtxt, TypeVisitable};
48+
use rustc_data_structures::fx::FxIndexMap;
4849
use rustc_hir::def_id::DefId;
4950

5051
use std::collections::BTreeMap;
@@ -533,12 +534,12 @@ impl<'tcx> TyCtxt<'tcx> {
533534
pub fn replace_escaping_bound_vars_uncached<T: TypeFoldable<'tcx>>(
534535
self,
535536
value: T,
536-
mut delegate: impl BoundVarReplacerDelegate<'tcx>,
537+
delegate: &mut impl BoundVarReplacerDelegate<'tcx>,
537538
) -> T {
538539
if !value.has_escaping_bound_vars() {
539540
value
540541
} else {
541-
let mut replacer = BoundVarReplacer::new(self, &mut delegate);
542+
let mut replacer = BoundVarReplacer::new(self, delegate);
542543
value.fold_with(&mut replacer)
543544
}
544545
}
@@ -549,9 +550,9 @@ impl<'tcx> TyCtxt<'tcx> {
549550
pub fn replace_bound_vars_uncached<T: TypeFoldable<'tcx>>(
550551
self,
551552
value: Binder<'tcx, T>,
552-
delegate: impl BoundVarReplacerDelegate<'tcx>,
553+
mut delegate: impl BoundVarReplacerDelegate<'tcx>,
553554
) -> T {
554-
self.replace_escaping_bound_vars_uncached(value.skip_binder(), delegate)
555+
self.replace_escaping_bound_vars_uncached(value.skip_binder(), &mut delegate)
555556
}
556557

557558
/// Replaces any late-bound regions bound in `value` with
@@ -579,7 +580,7 @@ impl<'tcx> TyCtxt<'tcx> {
579580
let shift_bv = |bv: ty::BoundVar| ty::BoundVar::from_usize(bv.as_usize() + bound_vars);
580581
self.replace_escaping_bound_vars_uncached(
581582
value,
582-
FnMutDelegate {
583+
&mut FnMutDelegate {
583584
regions: |r: ty::BoundRegion| {
584585
self.mk_region(ty::ReLateBound(
585586
ty::INNERMOST,
@@ -640,6 +641,50 @@ impl<'tcx> TyCtxt<'tcx> {
640641
);
641642
Binder::bind_with_vars(inner, bound_vars)
642643
}
644+
645+
/// Anonymize all bound variables in `value`, this is mostly used to improve caching.
646+
pub fn anonymize_bound_vars<T>(self, value: Binder<'tcx, T>) -> Binder<'tcx, T>
647+
where
648+
T: TypeFoldable<'tcx>,
649+
{
650+
struct Anonymize<'tcx> {
651+
tcx: TyCtxt<'tcx>,
652+
map: FxIndexMap<ty::BoundVar, ty::BoundVariableKind>,
653+
}
654+
impl<'tcx> BoundVarReplacerDelegate<'tcx> for Anonymize<'tcx> {
655+
fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
656+
let entry = self.map.entry(br.var);
657+
let index = entry.index();
658+
let var = ty::BoundVar::from_usize(index);
659+
let kind = entry
660+
.or_insert_with(|| ty::BoundVariableKind::Region(ty::BrAnon(index as u32)))
661+
.expect_region();
662+
let br = ty::BoundRegion { var, kind };
663+
self.tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br))
664+
}
665+
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
666+
let entry = self.map.entry(bt.var);
667+
let index = entry.index();
668+
let var = ty::BoundVar::from_usize(index);
669+
let kind = entry
670+
.or_insert_with(|| ty::BoundVariableKind::Ty(ty::BoundTyKind::Anon))
671+
.expect_ty();
672+
self.tcx.mk_ty(ty::Bound(ty::INNERMOST, BoundTy { var, kind }))
673+
}
674+
fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
675+
let entry = self.map.entry(bv);
676+
let index = entry.index();
677+
let var = ty::BoundVar::from_usize(index);
678+
let () = entry.or_insert_with(|| ty::BoundVariableKind::Const).expect_const();
679+
self.tcx.mk_const(ty::ConstS { ty, kind: ty::ConstKind::Bound(ty::INNERMOST, var) })
680+
}
681+
}
682+
683+
let mut delegate = Anonymize { tcx: self, map: Default::default() };
684+
let inner = self.replace_escaping_bound_vars_uncached(value.skip_binder(), &mut delegate);
685+
let bound_vars = self.mk_bound_variable_kinds(delegate.map.into_values());
686+
Binder::bind_with_vars(inner, bound_vars)
687+
}
643688
}
644689

645690
///////////////////////////////////////////////////////////////////////////

compiler/rustc_middle/src/ty/sty.rs

+23
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,29 @@ pub enum BoundVariableKind {
976976
Const,
977977
}
978978

979+
impl BoundVariableKind {
980+
pub fn expect_region(self) -> BoundRegionKind {
981+
match self {
982+
BoundVariableKind::Region(lt) => lt,
983+
_ => bug!("expected a region, but found another kind"),
984+
}
985+
}
986+
987+
pub fn expect_ty(self) -> BoundTyKind {
988+
match self {
989+
BoundVariableKind::Ty(ty) => ty,
990+
_ => bug!("expected a type, but found another kind"),
991+
}
992+
}
993+
994+
pub fn expect_const(self) {
995+
match self {
996+
BoundVariableKind::Const => (),
997+
_ => bug!("expected a const, but found another kind"),
998+
}
999+
}
1000+
}
1001+
9791002
/// Binder is a binder for higher-ranked lifetimes or types. It is part of the
9801003
/// compiler's representation for things like `for<'a> Fn(&'a isize)`
9811004
/// (which would be represented by the type `PolyTraitRef ==

compiler/rustc_typeck/src/check/dropck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ impl<'tcx> TypeRelation<'tcx> for SimpleEqRelation<'tcx> {
318318

319319
// Anonymizing the LBRs is necessary to solve (Issue #59497).
320320
// After we do so, it should be totally fine to skip the binders.
321-
let anon_a = self.tcx.anonymize_late_bound_regions(a);
322-
let anon_b = self.tcx.anonymize_late_bound_regions(b);
321+
let anon_a = self.tcx.anonymize_bound_vars(a);
322+
let anon_b = self.tcx.anonymize_bound_vars(b);
323323
self.relate(anon_a.skip_binder(), anon_b.skip_binder())?;
324324

325325
Ok(a)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
//
3+
// regression test for #98702
4+
#![feature(generic_associated_types)]
5+
6+
trait Foo {
7+
type Assoc<T>;
8+
}
9+
10+
impl Foo for () {
11+
type Assoc<T> = [T; 2*2];
12+
}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)