@@ -34,7 +34,6 @@ use rustc_attr_ir::lang_items::LangItem;
3434use rustc_attr_ir:: { self as attr, find_attr} ;
3535use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap } ;
3636use rustc_data_structures:: intern:: Interned ;
37- use rustc_data_structures:: snapshot_vec:: VecLike ;
3837use rustc_data_structures:: stable_hash:: { StableHash , StableHashCtxt , StableHasher } ;
3938use rustc_errors:: { Diag , ErrorGuaranteed } ;
4039use 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.
0 commit comments