88use rustc:: hir:: def_id:: DefId ;
99use rustc:: ty;
1010use rustc_data_structures:: fx:: FxHashMap ;
11- use rustc_data_structures:: sync:: Lrc ;
1211
1312use super :: constraints:: * ;
1413use super :: terms:: * ;
@@ -23,7 +22,9 @@ struct SolveContext<'a, 'tcx: 'a> {
2322 solutions : Vec < ty:: Variance > ,
2423}
2524
26- pub fn solve_constraints ( constraints_cx : ConstraintContext < ' _ , ' _ > ) -> ty:: CrateVariancesMap {
25+ pub fn solve_constraints < ' tcx > (
26+ constraints_cx : ConstraintContext < ' _ , ' tcx >
27+ ) -> ty:: CrateVariancesMap < ' tcx > {
2728 let ConstraintContext { terms_cx, constraints, .. } = constraints_cx;
2829
2930 let mut solutions = vec ! [ ty:: Bivariant ; terms_cx. inferred_terms. len( ) ] ;
@@ -41,9 +42,8 @@ pub fn solve_constraints(constraints_cx: ConstraintContext<'_, '_>) -> ty::Crate
4142 } ;
4243 solutions_cx. solve ( ) ;
4344 let variances = solutions_cx. create_map ( ) ;
44- let empty_variance = Lrc :: new ( Vec :: new ( ) ) ;
4545
46- ty:: CrateVariancesMap { variances, empty_variance }
46+ ty:: CrateVariancesMap { variances }
4747}
4848
4949impl < ' a , ' tcx > SolveContext < ' a , ' tcx > {
@@ -78,28 +78,32 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
7878 }
7979 }
8080
81- fn create_map ( & self ) -> FxHashMap < DefId , Lrc < Vec < ty:: Variance > > > {
81+ fn create_map ( & self ) -> FxHashMap < DefId , & ' tcx [ ty:: Variance ] > {
8282 let tcx = self . terms_cx . tcx ;
8383
8484 let solutions = & self . solutions ;
8585 self . terms_cx . inferred_starts . iter ( ) . map ( |( & id, & InferredIndex ( start) ) | {
8686 let def_id = tcx. hir ( ) . local_def_id_from_hir_id ( id) ;
8787 let generics = tcx. generics_of ( def_id) ;
8888
89- let mut variances = solutions[ start..start+generics. count ( ) ] . to_vec ( ) ;
89+ let variances = solutions[ start..start+generics. count ( ) ] . iter ( ) . cloned ( ) ;
9090
9191 debug ! ( "id={} variances={:?}" , id, variances) ;
9292
93- // Functions can have unused type parameters: make those invariant.
94- if let ty:: FnDef ( ..) = tcx. type_of ( def_id) . sty {
95- for variance in & mut variances {
96- if * variance == ty:: Bivariant {
97- * variance = ty:: Invariant ;
93+ let variances = if let ty:: FnDef ( ..) = tcx. type_of ( def_id) . sty {
94+ // Functions can have unused type parameters: make those invariant.
95+ tcx. arena . alloc_from_iter ( variances. map ( |variance| {
96+ if variance == ty:: Bivariant {
97+ ty:: Invariant
98+ } else {
99+ variance
98100 }
99- }
100- }
101+ } ) )
102+ } else {
103+ tcx. arena . alloc_from_iter ( variances)
104+ } ;
101105
102- ( def_id, Lrc :: new ( variances) )
106+ ( def_id, & * variances)
103107 } ) . collect ( )
104108 }
105109
0 commit comments