Skip to content

Commit cc1f62d

Browse files
committed
Address some review comments
1 parent 9a8a3a6 commit cc1f62d

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

src/librustc_mir/monomorphize/deduplicate_instances.rs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,42 @@ use rustc::middle::const_val::ConstVal;
1616
use rustc::mir::{Mir, Rvalue, Promoted, Location};
1717
use rustc::mir::visit::{Visitor, TyContext};
1818

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.
2134
pub(crate) fn collapse_interchangable_instances<'a, 'tcx>(
2235
tcx: TyCtxt<'a, 'tcx, 'tcx>,
23-
mut inst: Instance<'tcx>
36+
mut instance: Instance<'tcx>
2437
) -> Instance<'tcx> {
25-
info!("replace_unused_substs_with_ty_error({:?})", inst);
38+
info!("replace_unused_substs_with_ty_error({:?})", instance);
2639

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;
2942
}
30-
match inst.ty(tcx).sty {
43+
match instance.ty(tcx).sty {
3144
ty::TyFnDef(def_id, _) => {
3245
//let attrs = tcx.item_attrs(def_id);
3346
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
3548
}
3649
}
37-
_ => return inst, // Closures dont work otherwise
50+
_ => return instance, // Closures dont work otherwise
3851
}
3952

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)| {
4255
if let Some(ty) = subst.as_type() {
4356
let ty = if used_substs.substs.iter().find(|p|p.idx == i as u32).is_some() {
4457
ty.into()
@@ -47,35 +60,35 @@ pub(crate) fn collapse_interchangable_instances<'a, 'tcx>(
4760
if false /*param.name.as_str().starts_with("<")*/ {
4861
ty.into()
4962
} else {
50-
tcx.sess.warn(&format!("Unused subst for {:?}", inst));
63+
tcx.sess.warn(&format!("Unused subst for {:?}", instance));
5164
tcx.mk_ty(ty::TyNever)
5265
}
5366
} else {
5467
// 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));
5669
tcx.mk_ty(ty::TyNever)
5770
};
5871
Kind::from(ty)
5972
} else {
6073
(*subst).clone()
6174
}
6275
}).collect::<Vec<_>>());
63-
info!("replace_unused_substs_with_ty_error(_) -> {:?}", inst);
64-
inst
76+
info!("replace_unused_substs_with_ty_error(_) -> {:?}", instance);
77+
instance
6578
}
6679

6780
#[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>,
7184
}
7285

73-
impl_stable_hash_for! { struct UsedSubsts { substs, promoted } }
86+
impl_stable_hash_for! { struct UsedParameters { substs, promoted } }
7487

7588
struct SubstsVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a>(
7689
TyCtxt<'a, 'gcx, 'tcx>,
7790
&'tcx Mir<'tcx>,
78-
UsedSubsts
91+
UsedParameters,
7992
);
8093

8194
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,
116129
}
117130
match ty.sty {
118131
ty::TyParam(param) => {
119-
self.2.substs.push(param);
132+
self.2.parameters.push(param);
120133
ty
121134
}
122135
ty::TyFnDef(_, substs) => {
@@ -143,32 +156,32 @@ impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> TypeFolder<'gcx, 'tcx> for SubstsVisitor<'a,
143156
fn used_substs_for_instance<'a, 'tcx: 'a>(
144157
tcx: TyCtxt<'a ,'tcx, 'tcx>,
145158
instance: Instance<'tcx>,
146-
) -> UsedSubsts {
159+
) -> UsedParameters {
147160
let mir = tcx.instance_mir(instance.def);
148161
let sig = ::rustc::ty::ty_fn_sig(tcx, instance.ty(tcx));
149162
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());
151164
substs_visitor.visit_mir(mir);
152165
for ty in sig.inputs().iter() {
153166
ty.fold_with(&mut substs_visitor);
154167
}
155168
sig.output().fold_with(&mut substs_visitor);
156169
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);
159172
used_substs.promoted = mir.promoted.iter().map(|mir| used_substs_for_mir(tcx, mir)).collect();
160173
used_substs
161174
}
162175

163176
fn used_substs_for_mir<'a, 'tcx: 'a>(
164177
tcx: TyCtxt<'a ,'tcx, 'tcx>,
165178
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());
168181
substs_visitor.visit_mir(mir);
169182
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);
172185
used_substs.promoted = mir.promoted.iter().map(|mir| used_substs_for_mir(tcx, mir)).collect();
173186
used_substs
174187
}

0 commit comments

Comments
 (0)