1
Fork 0

Auto merge of #105717 - compiler-errors:anonymize, r=jackh726

always use `anonymize_bound_vars`

Unless this is perf-sensitive, it's probably best to always use one anonymize function that does the right thing for all bound vars.

r? types
This commit is contained in:
bors 2022-12-16 06:45:08 +00:00
commit a803f313fd
4 changed files with 6 additions and 36 deletions

View file

@ -489,7 +489,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
format!( format!(
"{}::", "{}::",
// Erase named lt, we want `<A as B<'_>::C`, not `<A as B<'a>::C`. // Erase named lt, we want `<A as B<'_>::C`, not `<A as B<'a>::C`.
self.tcx.anonymize_late_bound_regions(poly_trait_ref).skip_binder(), self.tcx.anonymize_bound_vars(poly_trait_ref).skip_binder(),
), ),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );

View file

@ -426,7 +426,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// `deduce_expectations_from_expected_type` introduces // `deduce_expectations_from_expected_type` introduces
// late-bound lifetimes defined elsewhere, which we now // late-bound lifetimes defined elsewhere, which we now
// anonymize away, so as not to confuse the user. // anonymize away, so as not to confuse the user.
let bound_sig = self.tcx.anonymize_late_bound_regions(bound_sig); let bound_sig = self.tcx.anonymize_bound_vars(bound_sig);
let closure_sigs = self.closure_sigs(expr_def_id, body, bound_sig); let closure_sigs = self.closure_sigs(expr_def_id, body, bound_sig);

View file

@ -583,36 +583,6 @@ impl<'tcx> TyCtxt<'tcx> {
self.replace_late_bound_regions(value, |_| self.lifetimes.re_erased).0 self.replace_late_bound_regions(value, |_| self.lifetimes.re_erased).0
} }
/// Rewrite any late-bound regions so that they are anonymous. Region numbers are
/// assigned starting at 0 and increasing monotonically in the order traversed
/// by the fold operation.
///
/// The chief purpose of this function is to canonicalize regions so that two
/// `FnSig`s or `TraitRef`s which are equivalent up to region naming will become
/// structurally identical. For example, `for<'a, 'b> fn(&'a isize, &'b isize)` and
/// `for<'a, 'b> fn(&'b isize, &'a isize)` will become identical after anonymization.
pub fn anonymize_late_bound_regions<T>(self, sig: Binder<'tcx, T>) -> Binder<'tcx, T>
where
T: TypeFoldable<'tcx>,
{
let mut counter = 0;
let inner = self
.replace_late_bound_regions(sig, |_| {
let br = ty::BoundRegion {
var: ty::BoundVar::from_u32(counter),
kind: ty::BrAnon(counter, None),
};
let r = self.mk_region(ty::ReLateBound(ty::INNERMOST, br));
counter += 1;
r
})
.0;
let bound_vars = self.mk_bound_variable_kinds(
(0..counter).map(|i| ty::BoundVariableKind::Region(ty::BrAnon(i, None))),
);
Binder::bind_with_vars(inner, bound_vars)
}
/// Anonymize all bound variables in `value`, this is mostly used to improve caching. /// Anonymize all bound variables in `value`, this is mostly used to improve caching.
pub fn anonymize_bound_vars<T>(self, value: Binder<'tcx, T>) -> Binder<'tcx, T> pub fn anonymize_bound_vars<T>(self, value: Binder<'tcx, T>) -> Binder<'tcx, T>
where where

View file

@ -1812,10 +1812,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
&& self.tcx.is_fn_trait(trait_pred.def_id()) && self.tcx.is_fn_trait(trait_pred.def_id())
{ {
let expected_self = let expected_self =
self.tcx.anonymize_late_bound_regions(pred.kind().rebind(trait_pred.self_ty())); self.tcx.anonymize_bound_vars(pred.kind().rebind(trait_pred.self_ty()));
let expected_substs = self let expected_substs = self
.tcx .tcx
.anonymize_late_bound_regions(pred.kind().rebind(trait_pred.trait_ref.substs)); .anonymize_bound_vars(pred.kind().rebind(trait_pred.trait_ref.substs));
// Find another predicate whose self-type is equal to the expected self type, // Find another predicate whose self-type is equal to the expected self type,
// but whose substs don't match. // but whose substs don't match.
@ -1828,12 +1828,12 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Make sure that the self type matches // Make sure that the self type matches
// (i.e. constraining this closure) // (i.e. constraining this closure)
&& expected_self && expected_self
== self.tcx.anonymize_late_bound_regions( == self.tcx.anonymize_bound_vars(
pred.kind().rebind(trait_pred.self_ty()), pred.kind().rebind(trait_pred.self_ty()),
) )
// But the substs don't match (i.e. incompatible args) // But the substs don't match (i.e. incompatible args)
&& expected_substs && expected_substs
!= self.tcx.anonymize_late_bound_regions( != self.tcx.anonymize_bound_vars(
pred.kind().rebind(trait_pred.trait_ref.substs), pred.kind().rebind(trait_pred.trait_ref.substs),
) => ) =>
{ {