8
8
use rustc:: hir:: def_id:: DefId ;
9
9
use rustc:: ty;
10
10
use rustc_data_structures:: fx:: FxHashMap ;
11
- use rustc_data_structures:: sync:: Lrc ;
12
11
13
12
use super :: constraints:: * ;
14
13
use super :: terms:: * ;
@@ -23,7 +22,9 @@ struct SolveContext<'a, 'tcx: 'a> {
23
22
solutions : Vec < ty:: Variance > ,
24
23
}
25
24
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 > {
27
28
let ConstraintContext { terms_cx, constraints, .. } = constraints_cx;
28
29
29
30
let mut solutions = vec ! [ ty:: Bivariant ; terms_cx. inferred_terms. len( ) ] ;
@@ -41,9 +42,8 @@ pub fn solve_constraints(constraints_cx: ConstraintContext<'_, '_>) -> ty::Crate
41
42
} ;
42
43
solutions_cx. solve ( ) ;
43
44
let variances = solutions_cx. create_map ( ) ;
44
- let empty_variance = Lrc :: new ( Vec :: new ( ) ) ;
45
45
46
- ty:: CrateVariancesMap { variances, empty_variance }
46
+ ty:: CrateVariancesMap { variances }
47
47
}
48
48
49
49
impl < ' a , ' tcx > SolveContext < ' a , ' tcx > {
@@ -78,28 +78,32 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
78
78
}
79
79
}
80
80
81
- fn create_map ( & self ) -> FxHashMap < DefId , Lrc < Vec < ty:: Variance > > > {
81
+ fn create_map ( & self ) -> FxHashMap < DefId , & ' tcx [ ty:: Variance ] > {
82
82
let tcx = self . terms_cx . tcx ;
83
83
84
84
let solutions = & self . solutions ;
85
85
self . terms_cx . inferred_starts . iter ( ) . map ( |( & id, & InferredIndex ( start) ) | {
86
86
let def_id = tcx. hir ( ) . local_def_id_from_hir_id ( id) ;
87
87
let generics = tcx. generics_of ( def_id) ;
88
88
89
- let mut variances = solutions[ start..start+generics. count ( ) ] . to_vec ( ) ;
89
+ let variances = solutions[ start..start+generics. count ( ) ] . iter ( ) . cloned ( ) ;
90
90
91
91
debug ! ( "id={} variances={:?}" , id, variances) ;
92
92
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
98
100
}
99
- }
100
- }
101
+ } ) )
102
+ } else {
103
+ tcx. arena . alloc_from_iter ( variances)
104
+ } ;
101
105
102
- ( def_id, Lrc :: new ( variances) )
106
+ ( def_id, & * variances)
103
107
} ) . collect ( )
104
108
}
105
109
0 commit comments