@@ -16,29 +16,42 @@ use rustc::middle::const_val::ConstVal;
16
16
use rustc:: mir:: { Mir , Rvalue , Promoted , Location } ;
17
17
use rustc:: mir:: visit:: { Visitor , TyContext } ;
18
18
19
- /// Replace substs which arent used by the function with TyError,
20
- /// so that it doesnt end up in the binary multiple times
19
+ /// Replace substs which aren't used by the function with TyError,
20
+ /// so that it doesn't end up in the binary multiple times
21
+ /// For example in the code
22
+ ///
23
+ /// ```rust
24
+ /// fn foo<T>() { } // here, T is clearly unused =)
25
+ ///
26
+ /// fn main() {
27
+ /// foo::<u32>();
28
+ /// foo::<u64>();
29
+ /// }
30
+ /// ```
31
+ ///
32
+ /// `foo::<u32>` and `foo::<u64>` are collapsed to `foo::<{some dummy}>`,
33
+ /// because codegen for `foo` doesn't depend on the Subst for T.
21
34
pub ( crate ) fn collapse_interchangable_instances < ' a , ' tcx > (
22
35
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
23
- mut inst : Instance < ' tcx >
36
+ mut instance : Instance < ' tcx >
24
37
) -> Instance < ' tcx > {
25
- info ! ( "replace_unused_substs_with_ty_error({:?})" , inst ) ;
38
+ info ! ( "replace_unused_substs_with_ty_error({:?})" , instance ) ;
26
39
27
- if inst . substs . is_noop ( ) || !tcx. is_mir_available ( inst . def_id ( ) ) {
28
- return inst ;
40
+ if instance . substs . is_noop ( ) || !tcx. is_mir_available ( instance . def_id ( ) ) {
41
+ return instance ;
29
42
}
30
- match inst . ty ( tcx) . sty {
43
+ match instance . ty ( tcx) . sty {
31
44
ty:: TyFnDef ( def_id, _) => {
32
45
//let attrs = tcx.item_attrs(def_id);
33
46
if tcx. lang_items ( ) . items ( ) . iter ( ) . find ( |l|* * l == Some ( def_id) ) . is_some ( ) {
34
- return inst ; // Lang items dont work otherwise
47
+ return instance ; // Lang items dont work otherwise
35
48
}
36
49
}
37
- _ => return inst , // Closures dont work otherwise
50
+ _ => return instance , // Closures dont work otherwise
38
51
}
39
52
40
- let used_substs = used_substs_for_instance ( tcx, inst ) ;
41
- inst . substs = tcx. _intern_substs ( & inst . substs . into_iter ( ) . enumerate ( ) . map ( |( i, subst) | {
53
+ let used_substs = used_substs_for_instance ( tcx, instance ) ;
54
+ instance . substs = tcx. _intern_substs ( & instance . substs . into_iter ( ) . enumerate ( ) . map ( |( i, subst) | {
42
55
if let Some ( ty) = subst. as_type ( ) {
43
56
let ty = if used_substs. substs . iter ( ) . find ( |p|p. idx == i as u32 ) . is_some ( ) {
44
57
ty. into ( )
@@ -47,35 +60,35 @@ pub(crate) fn collapse_interchangable_instances<'a, 'tcx>(
47
60
if false /*param.name.as_str().starts_with("<")*/ {
48
61
ty. into ( )
49
62
} else {
50
- tcx. sess . warn ( & format ! ( "Unused subst for {:?}" , inst ) ) ;
63
+ tcx. sess . warn ( & format ! ( "Unused subst for {:?}" , instance ) ) ;
51
64
tcx. mk_ty ( ty:: TyNever )
52
65
}
53
66
} else {
54
67
// Can't use TyError as it gives some ICE in rustc_trans::callee::get_fn
55
- tcx. sess . warn ( & format ! ( "Unused subst for {:?}" , inst ) ) ;
68
+ tcx. sess . warn ( & format ! ( "Unused subst for {:?}" , instance ) ) ;
56
69
tcx. mk_ty ( ty:: TyNever )
57
70
} ;
58
71
Kind :: from ( ty)
59
72
} else {
60
73
( * subst) . clone ( )
61
74
}
62
75
} ) . collect :: < Vec < _ > > ( ) ) ;
63
- info ! ( "replace_unused_substs_with_ty_error(_) -> {:?}" , inst ) ;
64
- inst
76
+ info ! ( "replace_unused_substs_with_ty_error(_) -> {:?}" , instance ) ;
77
+ instance
65
78
}
66
79
67
80
#[ derive( Debug , Default , Clone ) ]
68
- pub struct UsedSubsts {
69
- pub substs : Vec < ParamTy > ,
70
- pub promoted : IndexVec < Promoted , UsedSubsts > ,
81
+ pub struct UsedParameters {
82
+ pub parameters : Vec < ParamTy > ,
83
+ pub promoted : IndexVec < Promoted , UsedParameters > ,
71
84
}
72
85
73
- impl_stable_hash_for ! { struct UsedSubsts { substs, promoted } }
86
+ impl_stable_hash_for ! { struct UsedParameters { substs, promoted } }
74
87
75
88
struct SubstsVisitor < ' a , ' gcx : ' a + ' tcx , ' tcx : ' a > (
76
89
TyCtxt < ' a , ' gcx , ' tcx > ,
77
90
& ' tcx Mir < ' tcx > ,
78
- UsedSubsts
91
+ UsedParameters ,
79
92
) ;
80
93
81
94
impl < ' a , ' gcx : ' a + ' tcx , ' tcx : ' a > Visitor < ' tcx > for SubstsVisitor < ' a , ' gcx , ' tcx > {
@@ -116,7 +129,7 @@ impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> TypeFolder<'gcx, 'tcx> for SubstsVisitor<'a,
116
129
}
117
130
match ty. sty {
118
131
ty:: TyParam ( param) => {
119
- self . 2 . substs . push ( param) ;
132
+ self . 2 . parameters . push ( param) ;
120
133
ty
121
134
}
122
135
ty:: TyFnDef ( _, substs) => {
@@ -143,32 +156,32 @@ impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> TypeFolder<'gcx, 'tcx> for SubstsVisitor<'a,
143
156
fn used_substs_for_instance < ' a , ' tcx : ' a > (
144
157
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
145
158
instance : Instance < ' tcx > ,
146
- ) -> UsedSubsts {
159
+ ) -> UsedParameters {
147
160
let mir = tcx. instance_mir ( instance. def ) ;
148
161
let sig = :: rustc:: ty:: ty_fn_sig ( tcx, instance. ty ( tcx) ) ;
149
162
let sig = tcx. erase_late_bound_regions_and_normalize ( & sig) ;
150
- let mut substs_visitor = SubstsVisitor ( tcx, mir, UsedSubsts :: default ( ) ) ;
163
+ let mut substs_visitor = SubstsVisitor ( tcx, mir, UsedParameters :: default ( ) ) ;
151
164
substs_visitor. visit_mir ( mir) ;
152
165
for ty in sig. inputs ( ) . iter ( ) {
153
166
ty. fold_with ( & mut substs_visitor) ;
154
167
}
155
168
sig. output ( ) . fold_with ( & mut substs_visitor) ;
156
169
let mut used_substs = substs_visitor. 2 ;
157
- used_substs. substs . sort_by_key ( |s|s. idx ) ;
158
- used_substs. substs . dedup_by_key ( |s|s. idx ) ;
170
+ used_substs. parameters . sort_by_key ( |s|s. idx ) ;
171
+ used_substs. parameters . dedup_by_key ( |s|s. idx ) ;
159
172
used_substs. promoted = mir. promoted . iter ( ) . map ( |mir| used_substs_for_mir ( tcx, mir) ) . collect ( ) ;
160
173
used_substs
161
174
}
162
175
163
176
fn used_substs_for_mir < ' a , ' tcx : ' a > (
164
177
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
165
178
mir : & ' tcx Mir < ' tcx > ,
166
- ) -> UsedSubsts {
167
- let mut substs_visitor = SubstsVisitor ( tcx, mir, UsedSubsts :: default ( ) ) ;
179
+ ) -> UsedParameters {
180
+ let mut substs_visitor = SubstsVisitor ( tcx, mir, UsedParameters :: default ( ) ) ;
168
181
substs_visitor. visit_mir ( mir) ;
169
182
let mut used_substs = substs_visitor. 2 ;
170
- used_substs. substs . sort_by_key ( |s|s. idx ) ;
171
- used_substs. substs . dedup_by_key ( |s|s. idx ) ;
183
+ used_substs. parameters . sort_by_key ( |s|s. idx ) ;
184
+ used_substs. parameters . dedup_by_key ( |s|s. idx ) ;
172
185
used_substs. promoted = mir. promoted . iter ( ) . map ( |mir| used_substs_for_mir ( tcx, mir) ) . collect ( ) ;
173
186
used_substs
174
187
}
0 commit comments