9
9
// except according to those terms.
10
10
11
11
use rustc_data_structures:: indexed_vec:: IndexVec ;
12
- use rustc:: ty:: { self , TyCtxt , Ty , TypeFoldable , Instance , ParamTy } ;
12
+ use rustc:: ty:: { self , TyCtxt , Ty , TypeFoldable , Instance } ;
13
13
use rustc:: ty:: fold:: TypeFolder ;
14
14
use rustc:: ty:: subst:: Kind ;
15
15
use rustc:: middle:: const_val:: ConstVal ;
16
- use rustc:: mir:: { Mir , Rvalue , Promoted , Location } ;
16
+ use rustc:: mir:: { Mir , Rvalue , Location } ;
17
17
use rustc:: mir:: visit:: { Visitor , TyContext } ;
18
18
19
19
/// Replace substs which aren't used by the function with TyError,
@@ -53,7 +53,26 @@ pub(crate) fn collapse_interchangable_instances<'a, 'tcx>(
53
53
let used_substs = used_substs_for_instance ( tcx, instance) ;
54
54
instance. substs = tcx. _intern_substs ( & instance. substs . into_iter ( ) . enumerate ( ) . map ( |( i, subst) | {
55
55
if let Some ( ty) = subst. as_type ( ) {
56
- let ty = if used_substs. substs . iter ( ) . find ( |p|p. idx == i as u32 ) . is_some ( ) {
56
+ /*let ty = if let ty::TyParam(ref _param) = ty.sty {
57
+ match used_substs.parameters[ParamIdx(i as u32)] {
58
+ ParamUsage::Unused => ty.into(),
59
+ ParamUsage::LayoutUsed | ParamUsage::Used => {
60
+ //^ Dont replace <closure_kind> and other internal params
61
+ if false /*param.name.as_str().starts_with("<")*/ {
62
+ ty.into()
63
+ } else {
64
+ tcx.sess.warn(&format!("Unused subst for {:?}", instance));
65
+ tcx.mk_ty(ty::TyNever)
66
+ }
67
+ }
68
+ }
69
+ } else {
70
+ tcx.sess.fatal("efjiofefio");
71
+ // Can't use TyError as it gives some ICE in rustc_trans::callee::get_fn
72
+ tcx.sess.warn(&format!("Unused subst for {:?}", instance));
73
+ tcx.mk_ty(ty::TyNever)
74
+ };*/
75
+ let ty = if used_substs. parameters [ ParamIdx ( i as u32 ) ] != ParamUsage :: Unused {
57
76
ty. into ( )
58
77
} else if let ty:: TyParam ( ref _param) = ty. sty {
59
78
//^ Dont replace <closure_kind> and other internal params
@@ -77,21 +96,59 @@ pub(crate) fn collapse_interchangable_instances<'a, 'tcx>(
77
96
instance
78
97
}
79
98
99
+ #[ derive( Debug , Copy , Clone , Eq , PartialEq ) ]
100
+ struct ParamIdx ( u32 ) ;
101
+
102
+ impl :: rustc_data_structures:: indexed_vec:: Idx for ParamIdx {
103
+ fn new ( idx : usize ) -> Self {
104
+ assert ! ( idx < :: std:: u32 :: MAX as usize ) ;
105
+ ParamIdx ( idx as u32 )
106
+ }
107
+
108
+ fn index ( self ) -> usize {
109
+ self . 0 as usize
110
+ }
111
+ }
112
+
113
+ #[ derive( Debug , Copy , Clone , Eq , PartialEq , Hash ) ]
114
+ enum ParamUsage {
115
+ Unused = 0 ,
116
+ #[ allow( dead_code) ]
117
+ LayoutUsed = 1 ,
118
+ Used = 2 ,
119
+ }
120
+
121
+ impl_stable_hash_for ! { enum self :: ParamUsage { Unused , LayoutUsed , Used } }
122
+
80
123
#[ derive( Debug , Default , Clone ) ]
81
- pub struct UsedParameters {
82
- pub parameters : Vec < ParamTy > ,
83
- pub promoted : IndexVec < Promoted , UsedParameters > ,
124
+ pub struct ParamsUsage {
125
+ parameters : IndexVec < ParamIdx , ParamUsage > ,
84
126
}
85
127
86
- impl_stable_hash_for ! { struct UsedParameters { substs, promoted } }
128
+ impl_stable_hash_for ! { struct ParamsUsage { parameters } }
129
+
130
+ impl ParamsUsage {
131
+ fn new ( len : usize ) -> ParamsUsage {
132
+ ParamsUsage {
133
+ parameters : IndexVec :: from_elem_n ( ParamUsage :: Unused , len) ,
134
+ }
135
+ }
136
+ }
87
137
88
138
struct SubstsVisitor < ' a , ' gcx : ' a + ' tcx , ' tcx : ' a > (
89
139
TyCtxt < ' a , ' gcx , ' tcx > ,
90
140
& ' tcx Mir < ' tcx > ,
91
- UsedParameters ,
141
+ ParamsUsage ,
92
142
) ;
93
143
94
144
impl < ' a , ' gcx : ' a + ' tcx , ' tcx : ' a > Visitor < ' tcx > for SubstsVisitor < ' a , ' gcx , ' tcx > {
145
+ fn visit_mir ( & mut self , mir : & Mir < ' tcx > ) {
146
+ for promoted in & mir. promoted {
147
+ self . visit_mir ( promoted) ;
148
+ }
149
+ self . super_mir ( mir) ;
150
+ }
151
+
95
152
fn visit_ty ( & mut self , ty : & Ty < ' tcx > , _: TyContext ) {
96
153
self . fold_ty ( ty) ;
97
154
}
@@ -129,7 +186,7 @@ impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> TypeFolder<'gcx, 'tcx> for SubstsVisitor<'a,
129
186
}
130
187
match ty. sty {
131
188
ty:: TyParam ( param) => {
132
- self . 2 . parameters . push ( param) ;
189
+ self . 2 . parameters [ ParamIdx ( param. idx ) ] = ParamUsage :: Used ;
133
190
ty
134
191
}
135
192
ty:: TyFnDef ( _, substs) => {
@@ -156,32 +213,15 @@ impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> TypeFolder<'gcx, 'tcx> for SubstsVisitor<'a,
156
213
fn used_substs_for_instance < ' a , ' tcx : ' a > (
157
214
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
158
215
instance : Instance < ' tcx > ,
159
- ) -> UsedParameters {
216
+ ) -> ParamsUsage {
160
217
let mir = tcx. instance_mir ( instance. def ) ;
161
218
let sig = :: rustc:: ty:: ty_fn_sig ( tcx, instance. ty ( tcx) ) ;
162
219
let sig = tcx. erase_late_bound_regions_and_normalize ( & sig) ;
163
- let mut substs_visitor = SubstsVisitor ( tcx, mir, UsedParameters :: default ( ) ) ;
220
+ let mut substs_visitor = SubstsVisitor ( tcx, mir, ParamsUsage :: new ( instance . substs . len ( ) ) ) ;
164
221
substs_visitor. visit_mir ( mir) ;
165
222
for ty in sig. inputs ( ) . iter ( ) {
166
223
ty. fold_with ( & mut substs_visitor) ;
167
224
}
168
225
sig. output ( ) . fold_with ( & mut substs_visitor) ;
169
- let mut used_substs = substs_visitor. 2 ;
170
- used_substs. parameters . sort_by_key ( |s|s. idx ) ;
171
- used_substs. parameters . dedup_by_key ( |s|s. idx ) ;
172
- used_substs. promoted = mir. promoted . iter ( ) . map ( |mir| used_substs_for_mir ( tcx, mir) ) . collect ( ) ;
173
- used_substs
174
- }
175
-
176
- fn used_substs_for_mir < ' a , ' tcx : ' a > (
177
- tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
178
- mir : & ' tcx Mir < ' tcx > ,
179
- ) -> UsedParameters {
180
- let mut substs_visitor = SubstsVisitor ( tcx, mir, UsedParameters :: default ( ) ) ;
181
- substs_visitor. visit_mir ( mir) ;
182
- let mut used_substs = substs_visitor. 2 ;
183
- used_substs. parameters . sort_by_key ( |s|s. idx ) ;
184
- used_substs. parameters . dedup_by_key ( |s|s. idx ) ;
185
- used_substs. promoted = mir. promoted . iter ( ) . map ( |mir| used_substs_for_mir ( tcx, mir) ) . collect ( ) ;
186
- used_substs
226
+ substs_visitor. 2
187
227
}
0 commit comments