Skip to content

Commit 211637d

Browse files
committed
Auto merge of #99730 - lcnr:bound-vars-anon, r=jackh726
anonymize all bound vars, not just regions fixes #98702 r? types
2 parents 3924dac + 1436fa9 commit 211637d

File tree

10 files changed

+260
-138
lines changed

10 files changed

+260
-138
lines changed

Diff for: compiler/rustc_infer/src/infer/canonical/substitute.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! [c]: https://rust-lang.github.io/chalk/book/canonical_queries/canonicalization.html
88
99
use crate::infer::canonical::{Canonical, CanonicalVarValues};
10-
use rustc_middle::ty::fold::TypeFoldable;
10+
use rustc_middle::ty::fold::{FnMutDelegate, TypeFoldable};
1111
use rustc_middle::ty::subst::GenericArgKind;
1212
use rustc_middle::ty::{self, TyCtxt};
1313

@@ -71,21 +71,21 @@ where
7171
if var_values.var_values.is_empty() {
7272
value
7373
} else {
74-
let fld_r = |br: ty::BoundRegion| match var_values.var_values[br.var].unpack() {
75-
GenericArgKind::Lifetime(l) => l,
76-
r => bug!("{:?} is a region but value is {:?}", br, r),
74+
let delegate = FnMutDelegate {
75+
regions: |br: ty::BoundRegion| match var_values.var_values[br.var].unpack() {
76+
GenericArgKind::Lifetime(l) => l,
77+
r => bug!("{:?} is a region but value is {:?}", br, r),
78+
},
79+
types: |bound_ty: ty::BoundTy| match var_values.var_values[bound_ty.var].unpack() {
80+
GenericArgKind::Type(ty) => ty,
81+
r => bug!("{:?} is a type but value is {:?}", bound_ty, r),
82+
},
83+
consts: |bound_ct: ty::BoundVar, _| match var_values.var_values[bound_ct].unpack() {
84+
GenericArgKind::Const(ct) => ct,
85+
c => bug!("{:?} is a const but value is {:?}", bound_ct, c),
86+
},
7787
};
7888

79-
let fld_t = |bound_ty: ty::BoundTy| match var_values.var_values[bound_ty.var].unpack() {
80-
GenericArgKind::Type(ty) => ty,
81-
r => bug!("{:?} is a type but value is {:?}", bound_ty, r),
82-
};
83-
84-
let fld_c = |bound_ct: ty::BoundVar, _| match var_values.var_values[bound_ct].unpack() {
85-
GenericArgKind::Const(ct) => ct,
86-
c => bug!("{:?} is a const but value is {:?}", bound_ct, c),
87-
};
88-
89-
tcx.replace_escaping_bound_vars_uncached(value, fld_r, fld_t, fld_c)
89+
tcx.replace_escaping_bound_vars_uncached(value, delegate)
9090
}
9191
}

Diff for: compiler/rustc_infer/src/infer/higher_ranked/mod.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use super::combine::CombineFields;
55
use super::{HigherRankedType, InferCtxt};
66
use crate::infer::CombinedSnapshot;
7+
use rustc_middle::ty::fold::FnMutDelegate;
78
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
89
use rustc_middle::ty::{self, Binder, TypeFoldable};
910

@@ -79,31 +80,31 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
7980

8081
let next_universe = self.create_next_universe();
8182

82-
let fld_r = |br: ty::BoundRegion| {
83-
self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
84-
universe: next_universe,
85-
name: br.kind,
86-
}))
87-
};
88-
89-
let fld_t = |bound_ty: ty::BoundTy| {
90-
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
91-
universe: next_universe,
92-
name: bound_ty.var,
93-
}))
94-
};
95-
96-
let fld_c = |bound_var: ty::BoundVar, ty| {
97-
self.tcx.mk_const(ty::ConstS {
98-
kind: ty::ConstKind::Placeholder(ty::PlaceholderConst {
83+
let delegate = FnMutDelegate {
84+
regions: |br: ty::BoundRegion| {
85+
self.tcx.mk_region(ty::RePlaceholder(ty::PlaceholderRegion {
86+
universe: next_universe,
87+
name: br.kind,
88+
}))
89+
},
90+
types: |bound_ty: ty::BoundTy| {
91+
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
9992
universe: next_universe,
100-
name: ty::BoundConst { var: bound_var, ty },
101-
}),
102-
ty,
103-
})
93+
name: bound_ty.var,
94+
}))
95+
},
96+
consts: |bound_var: ty::BoundVar, ty| {
97+
self.tcx.mk_const(ty::ConstS {
98+
kind: ty::ConstKind::Placeholder(ty::PlaceholderConst {
99+
universe: next_universe,
100+
name: ty::BoundConst { var: bound_var, ty },
101+
}),
102+
ty,
103+
})
104+
},
104105
};
105106

106-
let result = self.tcx.replace_bound_vars_uncached(binder, fld_r, fld_t, fld_c);
107+
let result = self.tcx.replace_bound_vars_uncached(binder, delegate);
107108
debug!(?next_universe, ?result);
108109
result
109110
}

Diff for: compiler/rustc_infer/src/infer/mod.rs

+50-25
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
2323
use rustc_middle::traits::select;
2424
use rustc_middle::ty::abstract_const::{AbstractConst, FailureKind};
2525
use rustc_middle::ty::error::{ExpectedFound, TypeError};
26+
use rustc_middle::ty::fold::BoundVarReplacerDelegate;
2627
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
2728
use rustc_middle::ty::relate::RelateResult;
2829
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, SubstsRef};
@@ -1564,32 +1565,56 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15641565
return inner;
15651566
}
15661567

1567-
let mut region_map = FxHashMap::default();
1568-
let fld_r = |br: ty::BoundRegion| {
1569-
*region_map
1570-
.entry(br)
1571-
.or_insert_with(|| self.next_region_var(LateBoundRegion(span, br.kind, lbrct)))
1572-
};
1568+
struct ToFreshVars<'a, 'tcx> {
1569+
infcx: &'a InferCtxt<'a, 'tcx>,
1570+
span: Span,
1571+
lbrct: LateBoundRegionConversionTime,
1572+
map: FxHashMap<ty::BoundVar, ty::GenericArg<'tcx>>,
1573+
}
15731574

1574-
let mut ty_map = FxHashMap::default();
1575-
let fld_t = |bt: ty::BoundTy| {
1576-
*ty_map.entry(bt).or_insert_with(|| {
1577-
self.next_ty_var(TypeVariableOrigin {
1578-
kind: TypeVariableOriginKind::MiscVariable,
1579-
span,
1580-
})
1581-
})
1582-
};
1583-
let mut ct_map = FxHashMap::default();
1584-
let fld_c = |bc: ty::BoundVar, ty| {
1585-
*ct_map.entry(bc).or_insert_with(|| {
1586-
self.next_const_var(
1587-
ty,
1588-
ConstVariableOrigin { kind: ConstVariableOriginKind::MiscVariable, span },
1589-
)
1590-
})
1591-
};
1592-
self.tcx.replace_bound_vars_uncached(value, fld_r, fld_t, fld_c)
1575+
impl<'tcx> BoundVarReplacerDelegate<'tcx> for ToFreshVars<'_, 'tcx> {
1576+
fn replace_region(&mut self, br: ty::BoundRegion) -> ty::Region<'tcx> {
1577+
self.map
1578+
.entry(br.var)
1579+
.or_insert_with(|| {
1580+
self.infcx
1581+
.next_region_var(LateBoundRegion(self.span, br.kind, self.lbrct))
1582+
.into()
1583+
})
1584+
.expect_region()
1585+
}
1586+
fn replace_ty(&mut self, bt: ty::BoundTy) -> Ty<'tcx> {
1587+
self.map
1588+
.entry(bt.var)
1589+
.or_insert_with(|| {
1590+
self.infcx
1591+
.next_ty_var(TypeVariableOrigin {
1592+
kind: TypeVariableOriginKind::MiscVariable,
1593+
span: self.span,
1594+
})
1595+
.into()
1596+
})
1597+
.expect_ty()
1598+
}
1599+
fn replace_const(&mut self, bv: ty::BoundVar, ty: Ty<'tcx>) -> ty::Const<'tcx> {
1600+
self.map
1601+
.entry(bv)
1602+
.or_insert_with(|| {
1603+
self.infcx
1604+
.next_const_var(
1605+
ty,
1606+
ConstVariableOrigin {
1607+
kind: ConstVariableOriginKind::MiscVariable,
1608+
span: self.span,
1609+
},
1610+
)
1611+
.into()
1612+
})
1613+
.expect_const()
1614+
}
1615+
}
1616+
let delegate = ToFreshVars { infcx: self, span, lbrct, map: Default::default() };
1617+
self.tcx.replace_bound_vars_uncached(value, delegate)
15931618
}
15941619

15951620
/// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.

Diff for: 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(),

Diff for: 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

0 commit comments

Comments
 (0)