Skip to content

Commit 5b0b763

Browse files
committed
stop decompressing during certain paramenv construction and introduce builder api
1 parent 4ca960a commit 5b0b763

3 files changed

Lines changed: 41 additions & 20 deletions

File tree

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_infer::infer::{self, BoundRegionConversionTime, InferCtxt, TyCtxtInfer
1414
use rustc_infer::traits::{TraitErrors, util};
1515
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1616
use rustc_middle::ty::{
17-
self, BottomUpFolder, CallerBoundsIterator, GenericArgs, GenericParamDefKind, Generics, Ty,
17+
self, BottomUpFolder, GenericArgs, GenericParamDefKind, Generics, Ty,
1818
TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, TypeVisitableExt,
1919
TypeVisitor, TypingMode, Unnormalized, Upcast,
2020
};
@@ -2775,9 +2775,8 @@ fn param_env_with_gat_bounds<'tcx>(
27752775
impl_ty: ty::AssocItem,
27762776
impl_trait_ref: ty::TraitRef<'tcx>,
27772777
) -> ty::ParamEnv<'tcx> {
2778-
let param_env = tcx.param_env(impl_ty.def_id);
2778+
let mut param_env = tcx.param_env(impl_ty.def_id).into_builder(tcx);
27792779
let container_id = impl_ty.container_id(tcx);
2780-
let mut clauses = param_env.caller_bounds().all_clauses(tcx).iter().collect::<Vec<_>>();
27812780

27822781
// for RPITITs, we should install predicates that allow us to project all
27832782
// of the RPITITs associated with the same body. This is because checking
@@ -2874,7 +2873,7 @@ fn param_env_with_gat_bounds<'tcx>(
28742873
//
28752874
// impl<T> X for T where T: X { type Y = <T as X>::Y; }
28762875
}
2877-
_ => clauses.push(
2876+
_ => param_env.add_clause(
28782877
ty::Binder::bind_with_vars(
28792878
ty::ProjectionClause {
28802879
projection_term: ty::AliasTerm::new(
@@ -2891,7 +2890,7 @@ fn param_env_with_gat_bounds<'tcx>(
28912890
};
28922891
}
28932892

2894-
ty::ParamEnv::new(tcx, clauses)
2893+
param_env.build()
28952894
}
28962895

28972896
/// Manually check here that `async fn foo()` wasn't matched against `fn foo()`,

‎compiler/rustc_middle/src/ty/mod.rs‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use rustc_attr_ir::lang_items::LangItem;
3434
use rustc_attr_ir::{self as attr, find_attr};
3535
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
3636
use rustc_data_structures::intern::Interned;
37-
use rustc_data_structures::snapshot_vec::VecLike;
3837
use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher};
3938
use rustc_errors::{Diag, ErrorGuaranteed};
4039
use rustc_hir as hir;
@@ -1009,14 +1008,15 @@ impl<'tcx> rustc_type_ir::Flags for Clauses<'tcx> {
10091008
}
10101009
}
10111010

1012-
/// Manages compression of clauses to form a `ParamEnv`.
1013-
struct ClauseCompressor<'tcx> {
1011+
/// Efficiently builds a [`ParamEnv`], compressing trait clauses as it is being built,
1012+
/// and interning only once at the end.
1013+
pub struct ParamenvBuilder<'tcx> {
10141014
tcx: TyCtxt<'tcx>,
10151015
compressed_clauses: Vec<CompressedTraitClause<TyCtxt<'tcx>>>,
10161016
uncompressed_clauses: Vec<Clause<'tcx>>,
10171017
}
10181018

1019-
impl<'tcx> ClauseCompressor<'tcx> {
1019+
impl<'tcx> ParamenvBuilder<'tcx> {
10201020
/// Construct a `ClauseCompressor` from an existing [`ParamEnv`] without decompressing its
10211021
/// contents. This is useful when adding clauses to an existing environment without
10221022
/// decompressing and re-interning compressed clauses.
@@ -1042,7 +1042,7 @@ impl<'tcx> ClauseCompressor<'tcx> {
10421042
Self { tcx, compressed_clauses: Vec::new(), uncompressed_clauses: Vec::new() }
10431043
}
10441044

1045-
fn add_clause(&mut self, clause: Clause<'tcx>) {
1045+
pub fn add_clause(&mut self, clause: Clause<'tcx>) {
10461046
// Try to construct a common clause, if the given clause is one of the common clauses
10471047
let Some(new_compressed_trait_clause) =
10481048
CompressedTraitClause::from_clause(self.tcx, clause)
@@ -1084,6 +1084,10 @@ impl<'tcx> ClauseCompressor<'tcx> {
10841084

10851085
self.tcx.mk_clauses(&res)
10861086
}
1087+
1088+
pub fn build(self) -> ParamEnv<'tcx> {
1089+
ParamEnv {caller_bounds: self.mk_clauses()}
1090+
}
10871091
}
10881092

10891093
/// When interacting with the type system we must provide information about the
@@ -1129,15 +1133,33 @@ impl<'tcx> ParamEnv<'tcx> {
11291133
}
11301134

11311135
#[inline]
1132-
pub fn extend(self, tcx: TyCtxt<'tcx>, new_bounds: impl Iterator<Item = ty::Clause<'tcx>>) -> Self {
1133-
let mut compressor = ClauseCompressor::from_param_env(tcx, self);
1136+
pub fn builder(tcx: TyCtxt<'tcx>) -> ParamenvBuilder<'tcx> {
1137+
ParamenvBuilder::new(tcx)
1138+
}
1139+
1140+
/// Turn an existing `ParamEnv` into a builder to add bounds to it efficiently.
1141+
///
1142+
/// There's also [`extend`](Self::extend) for a single addition.
1143+
#[inline]
1144+
pub fn into_builder(self, tcx: TyCtxt<'tcx>) -> ParamenvBuilder<'tcx> {
1145+
ParamenvBuilder::from_param_env(tcx, self)
1146+
}
1147+
1148+
/// Add clauses to an existing `ParamEnv`, without decompressing existing trait bounds.
1149+
/// Note: it's more efficient to use [`ParamEnv::builder`](Self::builder) if you want to
1150+
/// add bounds more than once.
1151+
#[inline]
1152+
pub fn extend(self, tcx: TyCtxt<'tcx>, new_bounds: impl IntoIterator<Item = ty::Clause<'tcx>>) -> Self {
1153+
let mut builder = self.into_builder(tcx);
11341154
for clause in new_bounds {
1135-
compressor.add_clause(clause);
1155+
builder.add_clause(clause);
11361156
}
11371157

1138-
ParamEnv { caller_bounds: compressor.mk_clauses() }
1158+
builder.build()
11391159
}
11401160

1161+
/// Get a [`CallerBoundsAccessor`], which allows customizing the desired bounds
1162+
/// from the `ParamEnv` and iterating over *just* those.
11411163
#[inline]
11421164
pub fn caller_bounds(
11431165
self,
@@ -1156,12 +1178,12 @@ impl<'tcx> ParamEnv<'tcx> {
11561178
tcx: TyCtxt<'tcx>,
11571179
caller_bounds: impl IntoIterator<Item = ty::Clause<'tcx>>,
11581180
) -> Self {
1159-
let mut compressor = ClauseCompressor::new(tcx);
1181+
let mut builder = Self::builder(tcx);
11601182
for clause in caller_bounds {
1161-
compressor.add_clause(clause);
1183+
builder.add_clause(clause);
11621184
}
11631185

1164-
ParamEnv { caller_bounds: compressor.mk_clauses() }
1186+
builder.build()
11651187
}
11661188

11671189
/// Creates a pair of param-env and value for use in queries.

‎compiler/rustc_trait_selection/src/traits/auto_trait.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
252252
})
253253
.collect::<Vec<ty::Clause<'tcx>>>();
254254

255-
let full_user_env = orig_env.extend(field_clauses);
255+
let full_user_env = orig_env.extend(tcx, field_clauses);
256256

257257
let fresh_args = infcx.fresh_args_for_item(DUMMY_SP, adt_def.did());
258258
let fresh_ty = ty::EarlyBinder::bind(tcx, ty).instantiate(tcx, fresh_args).skip_norm_wip();
@@ -340,7 +340,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
340340
}));
341341

342342
let computed_clauses =
343-
param_env;
343+
param_env.caller_bounds().all_clauses(infcx.tcx).iter().collect::<Vec<_>>();
344344
let mut user_computed_clauses: FxIndexSet<_> =
345345
user_env.caller_bounds().all_clauses(infcx.tcx).iter().collect();
346346
let mut prev_user_computed_clauses = None::<FxIndexSet<_>>;
@@ -428,7 +428,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
428428

429429
let normalized_preds = elaborate(
430430
tcx,
431-
computed_clauses.extend(tcx, user_computed_clauses),
431+
computed_clauses.iter().copied().chain(user_computed_clauses.iter().cloned()),
432432
);
433433
new_env = ty::ParamEnv::new(tcx, normalized_preds);
434434
}

0 commit comments

Comments
 (0)