Skip to content

Commit 58bd0ea

Browse files
committed
deduplicate ty::Instance constructors
1 parent 8af151b commit 58bd0ea

File tree

8 files changed

+53
-125
lines changed

8 files changed

+53
-125
lines changed

src/librustc/ty/instance.rs

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::hir::def_id::DefId;
44
use crate::ty::{self, Ty, PolyFnSig, TypeFoldable, SubstsRef, TyCtxt};
55
use crate::ty::print::{FmtPrinter, Printer};
66
use crate::traits;
7+
use crate::middle::lang_items::DropInPlaceFnLangItem;
78
use rustc_target::spec::abi::Abi;
89
use rustc_macros::HashStable;
910

@@ -325,11 +326,47 @@ impl<'a, 'b, 'tcx> Instance<'tcx> {
325326
let actual_kind = substs.closure_kind(def_id, tcx);
326327

327328
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
328-
Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),
329+
Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs),
329330
_ => Instance::new(def_id, substs.substs)
330331
}
331332
}
332333

334+
pub fn resolve_drop_in_place(
335+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
336+
ty: Ty<'tcx>)
337+
-> ty::Instance<'tcx>
338+
{
339+
let def_id = tcx.require_lang_item(DropInPlaceFnLangItem);
340+
let substs = tcx.intern_substs(&[ty.into()]);
341+
Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap()
342+
}
343+
344+
pub fn fn_once_adapter_instance(
345+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
346+
closure_did: DefId,
347+
substs: ty::ClosureSubsts<'tcx>)
348+
-> Instance<'tcx>
349+
{
350+
debug!("fn_once_adapter_shim({:?}, {:?})",
351+
closure_did,
352+
substs);
353+
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
354+
let call_once = tcx.associated_items(fn_once)
355+
.find(|it| it.kind == ty::AssocKind::Method)
356+
.unwrap().def_id;
357+
let def = ty::InstanceDef::ClosureOnceShim { call_once };
358+
359+
let self_ty = tcx.mk_closure(closure_did, substs);
360+
361+
let sig = substs.closure_sig(closure_did, tcx);
362+
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
363+
assert_eq!(sig.inputs().len(), 1);
364+
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
365+
366+
debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
367+
Instance { def, substs }
368+
}
369+
333370
pub fn is_vtable_shim(&self) -> bool {
334371
if let InstanceDef::VtableShim(..) = self.def {
335372
true
@@ -438,29 +475,3 @@ fn needs_fn_once_adapter_shim<'a, 'tcx>(actual_closure_kind: ty::ClosureKind,
438475
(ty::ClosureKind::FnOnce, _) => Err(())
439476
}
440477
}
441-
442-
fn fn_once_adapter_instance<'a, 'tcx>(
443-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
444-
closure_did: DefId,
445-
substs: ty::ClosureSubsts<'tcx>)
446-
-> Instance<'tcx>
447-
{
448-
debug!("fn_once_adapter_shim({:?}, {:?})",
449-
closure_did,
450-
substs);
451-
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
452-
let call_once = tcx.associated_items(fn_once)
453-
.find(|it| it.kind == ty::AssocKind::Method)
454-
.unwrap().def_id;
455-
let def = ty::InstanceDef::ClosureOnceShim { call_once };
456-
457-
let self_ty = tcx.mk_closure(closure_did, substs);
458-
459-
let sig = substs.closure_sig(closure_did, tcx);
460-
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
461-
assert_eq!(sig.inputs().len(), 1);
462-
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
463-
464-
debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
465-
Instance { def, substs }
466-
}

src/librustc_codegen_ssa/meth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_mir::monomorphize;
44
use crate::callee;
55
use crate::traits::*;
66

7-
use rustc::ty::{self, Ty};
7+
use rustc::ty::{self, Ty, Instance};
88

99
#[derive(Copy, Clone, Debug)]
1010
pub struct VirtualIndex(u64);
@@ -103,7 +103,7 @@ pub fn get_vtable<'tcx, Cx: CodegenMethods<'tcx>>(
103103
// `get_vtable` in rust_mir/interpret/traits.rs
104104
// /////////////////////////////////////////////////////////////////////////////////////////////
105105
let components: Vec<_> = [
106-
cx.get_fn(monomorphize::resolve_drop_in_place(cx.tcx(), ty)),
106+
cx.get_fn(Instance::resolve_drop_in_place(cx.tcx(), ty)),
107107
cx.const_usize(layout.size.bytes()),
108108
cx.const_usize(layout.align.abi.bytes())
109109
].iter().cloned().chain(methods).collect();

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc::middle::lang_items;
2-
use rustc::ty::{self, Ty, TypeFoldable};
2+
use rustc::ty::{self, Ty, TypeFoldable, Instance};
33
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, FnTypeExt};
44
use rustc::mir::{self, Place, PlaceBase, Static, StaticKind};
55
use rustc::mir::interpret::InterpError;
@@ -310,7 +310,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
310310
) {
311311
let ty = location.ty(self.mir, bx.tcx()).ty;
312312
let ty = self.monomorphize(&ty);
313-
let drop_fn = monomorphize::resolve_drop_in_place(bx.tcx(), ty);
313+
let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty);
314314

315315
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
316316
// we don't actually need to drop anything.

src/librustc_codegen_ssa/mir/rvalue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc::ty::{self, Ty, adjustment::{PointerCast}};
1+
use rustc::ty::{self, Ty, adjustment::{PointerCast}, Instance};
22
use rustc::ty::cast::{CastTy, IntTy};
33
use rustc::ty::layout::{self, LayoutOf, HasTyCtxt};
44
use rustc::mir;
@@ -196,7 +196,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
196196
mir::CastKind::Pointer(PointerCast::ClosureFnPointer(_)) => {
197197
match operand.layout.ty.sty {
198198
ty::Closure(def_id, substs) => {
199-
let instance = monomorphize::resolve_closure(
199+
let instance = Instance::resolve_closure(
200200
bx.cx().tcx(), def_id, substs, ty::ClosureKind::FnOnce);
201201
OperandValue::Immediate(bx.cx().get_fn(instance))
202202
}

src/librustc_mir/interpret/terminator.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22

33
use rustc::{mir, ty};
4+
use rustc::ty::Instance;
45
use rustc::ty::layout::{self, TyLayout, LayoutOf};
56
use syntax::source_map::Span;
67
use rustc_target::spec::abi::Abi;
@@ -112,7 +113,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
112113
let ty = place.layout.ty;
113114
trace!("TerminatorKind::drop: {:?}, type {}", location, ty);
114115

115-
let instance = crate::monomorphize::resolve_drop_in_place(*self.tcx, ty);
116+
let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
116117
self.drop_in_place(
117118
place,
118119
instance,

src/librustc_mir/interpret/traits.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc::ty::{self, Ty};
1+
use rustc::ty::{self, Ty, Instance};
22
use rustc::ty::layout::{Size, Align, LayoutOf};
33
use rustc::mir::interpret::{Scalar, Pointer, EvalResult, PointerArithmetic};
44

@@ -55,8 +55,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> InterpretCx<'a, 'mir, 'tcx, M>
5555
);
5656
let tcx = &*self.tcx;
5757

58-
let drop = crate::monomorphize::resolve_drop_in_place(*tcx, ty);
58+
let drop = Instance::resolve_drop_in_place(*tcx, ty);
5959
let drop = self.memory.create_fn_alloc(drop);
60+
6061
// no need to do any alignment checks on the memory accesses below, because we know the
6162
// allocation is correctly aligned as we created it above. Also we're only offsetting by
6263
// multiples of `ptr_align`, which means that it will stay aligned to `ptr_align`.

src/librustc_mir/monomorphize/collector.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ use rustc::hir::def_id::{DefId, LOCAL_CRATE};
181181
use rustc::mir::interpret::{AllocId, ConstValue};
182182
use rustc::middle::lang_items::{ExchangeMallocFnLangItem, StartFnLangItem};
183183
use rustc::ty::subst::{InternalSubsts, SubstsRef};
184-
use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind};
184+
use rustc::ty::{self, TypeFoldable, Ty, TyCtxt, GenericParamDefKind, Instance};
185185
use rustc::ty::adjustment::{CustomCoerceUnsized, PointerCast};
186186
use rustc::session::config::EntryFnType;
187187
use rustc::mir::{self, Location, Place, PlaceBase, Promoted, Static, StaticKind};
188188
use rustc::mir::visit::Visitor as MirVisitor;
189189
use rustc::mir::mono::MonoItem;
190190
use rustc::mir::interpret::{Scalar, GlobalId, GlobalAlloc, ErrorHandled};
191191

192-
use crate::monomorphize::{self, Instance};
192+
use crate::monomorphize;
193193
use rustc::util::nodemap::{FxHashSet, FxHashMap, DefIdMap};
194194
use rustc::util::common::time;
195195

@@ -580,7 +580,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
580580
);
581581
match source_ty.sty {
582582
ty::Closure(def_id, substs) => {
583-
let instance = monomorphize::resolve_closure(
583+
let instance = Instance::resolve_closure(
584584
self.tcx, def_id, substs, ty::ClosureKind::FnOnce);
585585
if should_monomorphize_locally(self.tcx, &instance) {
586586
self.output.push(create_fn_mono_item(instance));
@@ -684,7 +684,7 @@ fn visit_drop_use<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
684684
is_direct_call: bool,
685685
output: &mut Vec<MonoItem<'tcx>>)
686686
{
687-
let instance = monomorphize::resolve_drop_in_place(tcx, ty);
687+
let instance = Instance::resolve_drop_in_place(tcx, ty);
688688
visit_instance_use(tcx, instance, is_direct_call, output);
689689
}
690690

src/librustc_mir/monomorphize/mod.rs

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use rustc::hir::def_id::DefId;
2-
use rustc::middle::lang_items::DropInPlaceFnLangItem;
31
use rustc::traits;
42
use rustc::ty::adjustment::CustomCoerceUnsized;
53
use rustc::ty::{self, Ty, TyCtxt, Instance};
@@ -54,89 +52,6 @@ pub fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>, mon
5452
}
5553
}
5654

57-
fn fn_once_adapter_instance<'a, 'tcx>(
58-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
59-
closure_did: DefId,
60-
substs: ty::ClosureSubsts<'tcx>,
61-
) -> Instance<'tcx> {
62-
debug!("fn_once_adapter_shim({:?}, {:?})",
63-
closure_did,
64-
substs);
65-
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
66-
let call_once = tcx.associated_items(fn_once)
67-
.find(|it| it.kind == ty::AssocKind::Method)
68-
.unwrap().def_id;
69-
let def = ty::InstanceDef::ClosureOnceShim { call_once };
70-
71-
let self_ty = tcx.mk_closure(closure_did, substs);
72-
73-
let sig = substs.closure_sig(closure_did, tcx);
74-
let sig = tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
75-
assert_eq!(sig.inputs().len(), 1);
76-
let substs = tcx.mk_substs_trait(self_ty, &[sig.inputs()[0].into()]);
77-
78-
debug!("fn_once_adapter_shim: self_ty={:?} sig={:?}", self_ty, sig);
79-
Instance { def, substs }
80-
}
81-
82-
fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind,
83-
trait_closure_kind: ty::ClosureKind)
84-
-> Result<bool, ()>
85-
{
86-
match (actual_closure_kind, trait_closure_kind) {
87-
(ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
88-
(ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) |
89-
(ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => {
90-
// No adapter needed.
91-
Ok(false)
92-
}
93-
(ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => {
94-
// The closure fn `llfn` is a `fn(&self, ...)`. We want a
95-
// `fn(&mut self, ...)`. In fact, at codegen time, these are
96-
// basically the same thing, so we can just return llfn.
97-
Ok(false)
98-
}
99-
(ty::ClosureKind::Fn, ty::ClosureKind::FnOnce) |
100-
(ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => {
101-
// The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut
102-
// self, ...)`. We want a `fn(self, ...)`. We can produce
103-
// this by doing something like:
104-
//
105-
// fn call_once(self, ...) { call_mut(&self, ...) }
106-
// fn call_once(mut self, ...) { call_mut(&mut self, ...) }
107-
//
108-
// These are both the same at codegen time.
109-
Ok(true)
110-
}
111-
_ => Err(()),
112-
}
113-
}
114-
115-
pub fn resolve_closure<'a, 'tcx> (
116-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
117-
def_id: DefId,
118-
substs: ty::ClosureSubsts<'tcx>,
119-
requested_kind: ty::ClosureKind)
120-
-> Instance<'tcx>
121-
{
122-
let actual_kind = substs.closure_kind(def_id, tcx);
123-
124-
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
125-
Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),
126-
_ => Instance::new(def_id, substs.substs)
127-
}
128-
}
129-
130-
pub fn resolve_drop_in_place<'a, 'tcx>(
131-
tcx: TyCtxt<'a, 'tcx, 'tcx>,
132-
ty: Ty<'tcx>)
133-
-> ty::Instance<'tcx>
134-
{
135-
let def_id = tcx.require_lang_item(DropInPlaceFnLangItem);
136-
let substs = tcx.intern_substs(&[ty.into()]);
137-
Instance::resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs).unwrap()
138-
}
139-
14055
pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
14156
source_ty: Ty<'tcx>,
14257
target_ty: Ty<'tcx>)

0 commit comments

Comments
 (0)