Skip to content

Commit e140750

Browse files
committed
Fix it
1 parent 149aa05 commit e140750

File tree

4 files changed

+88
-38
lines changed

4 files changed

+88
-38
lines changed

src/librustc/ty/maps/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,8 @@ define_maps! { <'tcx>
441441
[] fn wasm_import_module_map: WasmImportModuleMap(CrateNum)
442442
-> Lrc<FxHashMap<DefId, String>>,
443443

444-
[] fn collapse_interchangable_instances: collapse_interchangable_instances_dep_node(ty::Instance<'tcx>) -> ty::Instance<'tcx>,
444+
[] fn collapse_interchangable_instances:
445+
collapse_interchangable_instances_dep_node(ty::Instance<'tcx>) -> ty::Instance<'tcx>,
445446
}
446447

447448
//////////////////////////////////////////////////////////////////////
@@ -605,7 +606,9 @@ fn instance_def_size_estimate_dep_node<'tcx>(instance_def: ty::InstanceDef<'tcx>
605606
}
606607
}
607608

608-
fn collapse_interchangable_instances_dep_node<'tcx>(instance: ty::Instance<'tcx>) -> DepConstructor<'tcx> {
609+
fn collapse_interchangable_instances_dep_node<'tcx>(
610+
instance: ty::Instance<'tcx>
611+
) -> DepConstructor<'tcx> {
609612
DepConstructor::CollapseInterchangableInstances {
610613
instance,
611614
}

src/librustc_mir/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ pub fn provide(providers: &mut Providers) {
7474
transform::provide(providers);
7575
providers.const_eval = interpret::const_eval_provider;
7676
providers.check_match = hair::pattern::check_match;
77-
providers.collapse_interchangable_instances = monomorphize::deduplicate_instances::collapse_interchangable_instances;
77+
providers.collapse_interchangable_instances =
78+
monomorphize::deduplicate_instances::collapse_interchangable_instances; Fix it
7879
}
7980

8081
__build_diagnostic_array! { librustc_mir, DIAGNOSTICS }

src/librustc_mir/monomorphize/collector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,7 @@ fn create_mono_items_for_vtable_methods<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
920920
ty::ParamEnv::reveal_all(),
921921
def_id,
922922
substs).unwrap())
923+
.map(|instance|tcx.collapse_interchangable_instances(instance))
923924
.filter(|&instance| should_monomorphize_locally(tcx, &instance))
924925
.map(|instance| create_fn_mono_item(instance));
925926
output.extend(methods);

src/librustc_mir/monomorphize/deduplicate_instances.rs

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
use rustc_data_structures::indexed_vec::IndexVec;
212
use rustc::ty::{self, TyCtxt, Ty, TypeFoldable, Instance, ParamTy};
313
use rustc::ty::fold::TypeFolder;
414
use rustc::ty::subst::Kind;
5-
use rustc::mir::Promoted;
15+
use rustc::middle::const_val::ConstVal;
16+
use rustc::mir::{Mir, Rvalue, Promoted, Location};
617
use rustc::mir::visit::{Visitor, TyContext};
718

819
/// Replace substs which arent used by the function with TyError,
920
/// so that it doesnt end up in the binary multiple times
10-
pub(crate) fn collapse_interchangable_instances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, mut inst: Instance<'tcx>) -> Instance<'tcx> {
21+
pub(crate) fn collapse_interchangable_instances<'a, 'tcx>(
22+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
23+
mut inst: Instance<'tcx>
24+
) -> Instance<'tcx> {
1125
info!("replace_unused_substs_with_ty_error({:?})", inst);
1226

1327
if inst.substs.is_noop() || !tcx.is_mir_available(inst.def_id()) {
@@ -28,14 +42,16 @@ pub(crate) fn collapse_interchangable_instances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx,
2842
if let Some(ty) = subst.as_type() {
2943
let ty = if used_substs.substs.iter().find(|p|p.idx == i as u32).is_some() {
3044
ty.into()
31-
} else if let ty::TyParam(ref _param) = ty.sty { // Dont replace <closure_kind> and other internal params
45+
} else if let ty::TyParam(ref _param) = ty.sty {
46+
//^ Dont replace <closure_kind> and other internal params
3247
if false /*param.name.as_str().starts_with("<")*/ {
3348
ty.into()
3449
} else {
3550
tcx.mk_ty(ty::TyNever)
3651
}
3752
} else {
38-
tcx.mk_ty(ty::TyNever) // Can't use TyError as it gives some ICE in rustc_trans::callee::get_fn
53+
// Can't use TyError as it gives some ICE in rustc_trans::callee::get_fn
54+
tcx.mk_ty(ty::TyNever)
3955
};
4056
Kind::from(ty)
4157
} else {
@@ -54,59 +70,88 @@ pub struct UsedSubsts {
5470

5571
impl_stable_hash_for! { struct UsedSubsts { substs, promoted } }
5672

57-
fn used_substs_for_instance<'a, 'tcx: 'a>(tcx: TyCtxt<'a ,'tcx, 'tcx>, instance: Instance<'tcx>) -> UsedSubsts {
58-
struct SubstsVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a>(TyCtxt<'a, 'gcx, 'tcx>, UsedSubsts);
73+
struct SubstsVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a>(
74+
TyCtxt<'a, 'gcx, 'tcx>,
75+
&'tcx Mir<'tcx>,
76+
UsedSubsts
77+
);
78+
79+
impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> Visitor<'tcx> for SubstsVisitor<'a, 'gcx, 'tcx> {
80+
fn visit_ty(&mut self, ty: &Ty<'tcx>, _: TyContext) {
81+
self.fold_ty(ty);
82+
}
83+
84+
fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, _location: Location) {
85+
if let ConstVal::Unevaluated(_def_id, substs) = constant.val {
86+
for subst in substs {
87+
if let Some(ty) = subst.as_type() {
88+
ty.fold_with(self);
89+
}
90+
}
91+
}
92+
}
5993

60-
impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> Visitor<'tcx> for SubstsVisitor<'a, 'gcx, 'tcx> {
61-
fn visit_ty(&mut self, ty: &Ty<'tcx>, _: TyContext) {
62-
self.fold_ty(ty);
94+
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
95+
let tcx = self.0;
96+
match *rvalue {
97+
Rvalue::Cast(_kind, ref op, ty) => {
98+
self.fold_ty(op.ty(&self.1.local_decls, tcx));
99+
self.fold_ty(ty);
100+
}
101+
_ => {}
63102
}
103+
self.super_rvalue(rvalue, location);
64104
}
105+
}
65106

66-
impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> TypeFolder<'gcx, 'tcx> for SubstsVisitor<'a, 'gcx, 'tcx> {
67-
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
68-
self.0
107+
impl<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> TypeFolder<'gcx, 'tcx> for SubstsVisitor<'a, 'gcx, 'tcx> {
108+
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'gcx, 'tcx> {
109+
self.0
110+
}
111+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
112+
if !ty.needs_subst() {
113+
return ty;
69114
}
70-
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
71-
if !ty.needs_subst() {
72-
return ty;
115+
match ty.sty {
116+
ty::TyParam(param) => {
117+
self.2.substs.push(param);
118+
ty
73119
}
74-
match ty.sty {
75-
ty::TyParam(param) => {
76-
self.1.substs.push(param);
77-
ty
78-
}
79-
ty::TyFnDef(_, substs) => {
80-
for subst in substs {
81-
if let Some(ty) = subst.as_type() {
82-
ty.fold_with(self);
83-
}
120+
ty::TyFnDef(_, substs) => {
121+
for subst in substs {
122+
if let Some(ty) = subst.as_type() {
123+
ty.fold_with(self);
84124
}
85-
ty.super_fold_with(self)
86125
}
87-
ty::TyClosure(_, closure_substs) => {
88-
for subst in closure_substs.substs {
89-
if let Some(ty) = subst.as_type() {
90-
ty.fold_with(self);
91-
}
126+
ty.super_fold_with(self)
127+
}
128+
ty::TyClosure(_, closure_substs) => {
129+
for subst in closure_substs.substs {
130+
if let Some(ty) = subst.as_type() {
131+
ty.fold_with(self);
92132
}
93-
ty.super_fold_with(self)
94133
}
95-
_ => ty.super_fold_with(self)
134+
ty.super_fold_with(self)
96135
}
136+
_ => ty.super_fold_with(self)
97137
}
98138
}
139+
}
99140

141+
fn used_substs_for_instance<'a, 'tcx: 'a>(
142+
tcx: TyCtxt<'a ,'tcx, 'tcx>,
143+
instance: Instance<'tcx>
144+
) -> UsedSubsts {
100145
let mir = tcx.instance_mir(instance.def);
101146
let sig = ::rustc::ty::ty_fn_sig(tcx, instance.ty(tcx));
102147
let sig = tcx.erase_late_bound_regions_and_normalize(&sig);
103-
let mut substs_visitor = SubstsVisitor(tcx, UsedSubsts::default());
148+
let mut substs_visitor = SubstsVisitor(tcx, mir, UsedSubsts::default());
104149
substs_visitor.visit_mir(mir);
105150
for ty in sig.inputs().iter() {
106151
ty.fold_with(&mut substs_visitor);
107152
}
108153
sig.output().fold_with(&mut substs_visitor);
109-
let mut used_substs = substs_visitor.1;
154+
let mut used_substs = substs_visitor.2;
110155
used_substs.substs.sort_by_key(|s|s.idx);
111156
used_substs.substs.dedup_by_key(|s|s.idx);
112157
used_substs

0 commit comments

Comments
 (0)