@@ -34,6 +34,7 @@ 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 ;
3738use rustc_data_structures:: stable_hash:: { StableHash , StableHashCtxt , StableHasher } ;
3839use rustc_errors:: { Diag , ErrorGuaranteed } ;
3940use rustc_hir as hir;
@@ -1016,6 +1017,27 @@ struct ClauseCompressor<'tcx> {
10161017}
10171018
10181019impl < ' tcx > ClauseCompressor < ' tcx > {
1020+ /// Construct a `ClauseCompressor` from an existing [`ParamEnv`] without decompressing its
1021+ /// contents. This is useful when adding clauses to an existing environment without
1022+ /// decompressing and re-interning compressed clauses.
1023+ fn from_param_env ( tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Self {
1024+ let mut compressed_clauses = Vec :: new ( ) ;
1025+ let mut uncompressed_clauses = Vec :: new ( ) ;
1026+
1027+ for clause in param_env. caller_bounds {
1028+ match clause. kind ( ) . skip_binder ( ) {
1029+ ty:: ClauseKind :: CompressedTrait ( c) => compressed_clauses. push ( c) ,
1030+ _ => uncompressed_clauses. push ( clause) ,
1031+ }
1032+ }
1033+
1034+ Self {
1035+ tcx,
1036+ compressed_clauses,
1037+ uncompressed_clauses,
1038+ }
1039+ }
1040+
10191041 fn new ( tcx : TyCtxt < ' tcx > ) -> Self {
10201042 Self { tcx, compressed_clauses : Vec :: new ( ) , uncompressed_clauses : Vec :: new ( ) }
10211043 }
@@ -1106,6 +1128,16 @@ impl<'tcx> ParamEnv<'tcx> {
11061128 Self { caller_bounds : ListWithCachedTypeInfo :: empty ( ) }
11071129 }
11081130
1131+ #[ 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 ) ;
1134+ for clause in new_bounds {
1135+ compressor. add_clause ( clause) ;
1136+ }
1137+
1138+ ParamEnv { caller_bounds : compressor. mk_clauses ( ) }
1139+ }
1140+
11091141 #[ inline]
11101142 pub fn caller_bounds (
11111143 self ,
0 commit comments