Skip to content

Commit 504f2e9

Browse files
committed
Auto merge of rust-lang#140814 - compiler-errors:erase, r=<try>
Use a full cache for erasing regions r? lcnr
2 parents e964cca + e5daab6 commit 504f2e9

File tree

7 files changed

+12
-33
lines changed

7 files changed

+12
-33
lines changed

compiler/rustc_hir_typeck/src/cast.rs

-4
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
490490
&& let Some(from_trait) = fcx.tcx.get_diagnostic_item(sym::From)
491491
{
492492
let ty = fcx.resolve_vars_if_possible(self.cast_ty);
493-
// Erase regions to avoid panic in `prove_value` when calling
494-
// `type_implements_trait`.
495-
let ty = fcx.tcx.erase_regions(ty);
496493
let expr_ty = fcx.resolve_vars_if_possible(self.expr_ty);
497-
let expr_ty = fcx.tcx.erase_regions(expr_ty);
498494
if fcx
499495
.infcx
500496
.type_implements_trait(from_trait, [ty, expr_ty], fcx.param_env)

compiler/rustc_middle/src/query/mod.rs

-14
Original file line numberDiff line numberDiff line change
@@ -678,20 +678,6 @@ rustc_queries! {
678678
separate_provide_extern
679679
}
680680

681-
/// Erases regions from `ty` to yield a new type.
682-
/// Normally you would just use `tcx.erase_regions(value)`,
683-
/// however, which uses this query as a kind of cache.
684-
query erase_regions_ty(ty: Ty<'tcx>) -> Ty<'tcx> {
685-
// This query is not expected to have input -- as a result, it
686-
// is not a good candidates for "replay" because it is essentially a
687-
// pure function of its input (and hence the expectation is that
688-
// no caller would be green **apart** from just these
689-
// queries). Making it anonymous avoids hashing the result, which
690-
// may save a bit of time.
691-
anon
692-
desc { "erasing regions from `{}`", ty }
693-
}
694-
695681
query wasm_import_module_map(_: CrateNum) -> &'tcx DefIdMap<String> {
696682
arena_cache
697683
desc { "getting wasm import module map" }

compiler/rustc_middle/src/ty/context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,8 @@ pub struct GlobalCtxt<'tcx> {
14541454

14551455
/// A jobserver reference used to release then acquire a token while waiting on a query.
14561456
pub jobserver_proxy: Arc<Proxy>,
1457+
1458+
pub erased_ty_cache: Lock<FxHashMap<Ty<'tcx>, Ty<'tcx>>>,
14571459
}
14581460

14591461
impl<'tcx> GlobalCtxt<'tcx> {
@@ -1694,6 +1696,7 @@ impl<'tcx> TyCtxt<'tcx> {
16941696
alloc_map: interpret::AllocMap::new(),
16951697
current_gcx,
16961698
jobserver_proxy,
1699+
erased_ty_cache: Default::default(),
16971700
});
16981701

16991702
// This is a separate function to work around a crash with parallel rustc (#135870)

compiler/rustc_middle/src/ty/erase_regions.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
use tracing::debug;
22

3-
use crate::query::Providers;
43
use crate::ty::{
54
self, Ty, TyCtxt, TypeFlags, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
65
};
76

8-
pub(super) fn provide(providers: &mut Providers) {
9-
*providers = Providers { erase_regions_ty, ..*providers };
10-
}
11-
127
fn erase_regions_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
13-
// N.B., use `super_fold_with` here. If we used `fold_with`, it
14-
// could invoke the `erase_regions_ty` query recursively.
15-
ty.super_fold_with(&mut RegionEraserVisitor { tcx })
8+
if let Some(erased) = tcx.erased_ty_cache.lock().get(&ty) {
9+
*erased
10+
} else {
11+
let erased = ty.super_fold_with(&mut RegionEraserVisitor { tcx });
12+
tcx.erased_ty_cache.lock().insert(ty, erased);
13+
erased
14+
}
1615
}
1716

1817
impl<'tcx> TyCtxt<'tcx> {
@@ -46,10 +45,8 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for RegionEraserVisitor<'tcx> {
4645
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
4746
if !ty.has_type_flags(TypeFlags::HAS_BINDER_VARS | TypeFlags::HAS_FREE_REGIONS) {
4847
ty
49-
} else if ty.has_infer() {
50-
ty.super_fold_with(self)
5148
} else {
52-
self.tcx.erase_regions_ty(ty)
49+
erase_regions_ty(self.tcx, ty)
5350
}
5451
}
5552

compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2242,7 +2242,6 @@ pub fn ast_uint_ty(uty: UintTy) -> ast::UintTy {
22422242
pub fn provide(providers: &mut Providers) {
22432243
closure::provide(providers);
22442244
context::provide(providers);
2245-
erase_regions::provide(providers);
22462245
inhabitedness::provide(providers);
22472246
util::provide(providers);
22482247
print::provide(providers);

compiler/rustc_middle/src/ty/print/pretty.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1453,9 +1453,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
14531453
// contain named regions. So we erase and anonymize everything
14541454
// here to compare the types modulo regions below.
14551455
let proj = cx.tcx().erase_regions(proj);
1456-
let proj = cx.tcx().anonymize_bound_vars(proj);
14571456
let super_proj = cx.tcx().erase_regions(super_proj);
1458-
let super_proj = cx.tcx().anonymize_bound_vars(super_proj);
14591457

14601458
proj == super_proj
14611459
});

compiler/rustc_mir_transform/src/add_subtyping_projections.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for SubTypeChecker<'a, 'tcx> {
3232
let mut rval_ty = rvalue.ty(self.local_decls, self.tcx);
3333
// Not erasing this causes `Free Regions` errors in validator,
3434
// when rval is `ReStatic`.
35-
rval_ty = self.tcx.erase_regions_ty(rval_ty);
35+
rval_ty = self.tcx.erase_regions(rval_ty);
3636
place_ty = self.tcx.erase_regions(place_ty);
3737
if place_ty != rval_ty {
3838
let temp = self

0 commit comments

Comments
 (0)