Skip to content

Commit b76f224

Browse files
committed
Auto merge of #45525 - MaikKlein:collector, r=eddyb
Move collector to librustc_mir::monomorphize cc #44334 and #45276 * I moved the collector to rustc_mir * I renamed `TransItem` to `MonoItem`. _(I still need to fix up comments and variable names)_ * I got rid of `common.rs` and `monomorphize.rs` from `librustc_trans_utils`. I moved most of the functionality into `TyCtxt`. I realized that the `librustc_trans_utils::common.rs` was just copy pasted from `librustc_trans::common.rs`. Should I also get rid of the `librustc_trans::common.rs` in this PR? Most of the functionality seems a bit useless, I decided to put some of it into `TyCtxt` but maybe that is not the correct action here. Should I also get rid of `librustc_trans_utils` completely here? Or should I do it in a separate PR?
2 parents e7db42f + 6e78b66 commit b76f224

File tree

31 files changed

+372
-438
lines changed

31 files changed

+372
-438
lines changed

src/Cargo.lock

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ pub mod middle {
136136
pub mod recursion_limit;
137137
pub mod resolve_lifetime;
138138
pub mod stability;
139-
pub mod trans;
140139
pub mod weak_lang_items;
141140
}
142141

src/librustc/mir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub mod tcx;
4444
pub mod visit;
4545
pub mod traversal;
4646
pub mod interpret;
47+
pub mod mono;
4748

4849
/// Types for locals
4950
type LocalDecls<'tcx> = IndexVec<Local, LocalDecl<'tcx>>;

src/librustc/middle/trans.rs renamed to src/librustc/mir/mono.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasherResult,
1717
use ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};
1818

1919
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
20-
pub enum TransItem<'tcx> {
20+
pub enum MonoItem<'tcx> {
2121
Fn(Instance<'tcx>),
2222
Static(NodeId),
2323
GlobalAsm(NodeId),
2424
}
2525

26-
impl<'tcx> HashStable<StableHashingContext<'tcx>> for TransItem<'tcx> {
26+
impl<'tcx> HashStable<StableHashingContext<'tcx>> for MonoItem<'tcx> {
2727
fn hash_stable<W: StableHasherResult>(&self,
2828
hcx: &mut StableHashingContext<'tcx>,
2929
hasher: &mut StableHasher<W>) {
3030
::std::mem::discriminant(self).hash_stable(hcx, hasher);
3131

3232
match *self {
33-
TransItem::Fn(ref instance) => {
33+
MonoItem::Fn(ref instance) => {
3434
instance.hash_stable(hcx, hasher);
3535
}
36-
TransItem::Static(node_id) |
37-
TransItem::GlobalAsm(node_id) => {
36+
MonoItem::Static(node_id) |
37+
MonoItem::GlobalAsm(node_id) => {
3838
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
3939
node_id.hash_stable(hcx, hasher);
4040
})
@@ -49,7 +49,7 @@ pub struct CodegenUnit<'tcx> {
4949
/// contain something unique to this crate (e.g., a module path)
5050
/// as well as the crate name and disambiguator.
5151
name: InternedString,
52-
items: FxHashMap<TransItem<'tcx>, (Linkage, Visibility)>,
52+
items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>,
5353
}
5454

5555
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
@@ -110,12 +110,12 @@ impl<'tcx> CodegenUnit<'tcx> {
110110
self.name = name;
111111
}
112112

113-
pub fn items(&self) -> &FxHashMap<TransItem<'tcx>, (Linkage, Visibility)> {
113+
pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> {
114114
&self.items
115115
}
116116

117117
pub fn items_mut(&mut self)
118-
-> &mut FxHashMap<TransItem<'tcx>, (Linkage, Visibility)>
118+
-> &mut FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>
119119
{
120120
&mut self.items
121121
}

src/librustc/traits/trans/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use dep_graph::{DepKind, DepTrackingMapConfig};
1717
use infer::TransNormalize;
1818
use std::marker::PhantomData;
1919
use syntax_pos::DUMMY_SP;
20+
use hir::def_id::DefId;
2021
use traits::{FulfillmentContext, Obligation, ObligationCause, SelectionContext, Vtable};
2122
use ty::{self, Ty, TyCtxt};
2223
use ty::subst::{Subst, Substs};
@@ -119,6 +120,12 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
119120
let substituted = self.erase_regions(&substituted);
120121
AssociatedTypeNormalizerEnv::new(self, param_env).fold(&substituted)
121122
}
123+
124+
pub fn trans_impl_self_ty(&self, def_id: DefId, substs: &'tcx Substs<'tcx>)
125+
-> Ty<'tcx>
126+
{
127+
self.trans_apply_param_substs(substs, &self.type_of(def_id))
128+
}
122129
}
123130

124131
struct AssociatedTypeNormalizer<'a, 'gcx: 'a> {
@@ -214,4 +221,3 @@ impl<'gcx> DepTrackingMapConfig for ProjectionCache<'gcx> {
214221
DepKind::TraitSelect
215222
}
216223
}
217-

src/librustc/ty/instance.rs

+46-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ pub enum InstanceDef<'tcx> {
4545
CloneShim(DefId, Ty<'tcx>),
4646
}
4747

48+
impl<'a, 'tcx> Instance<'tcx> {
49+
pub fn ty(&self,
50+
tcx: TyCtxt<'a, 'tcx, 'tcx>)
51+
-> Ty<'tcx>
52+
{
53+
let ty = tcx.type_of(self.def.def_id());
54+
tcx.trans_apply_param_substs(self.substs, &ty)
55+
}
56+
}
57+
4858
impl<'tcx> InstanceDef<'tcx> {
4959
#[inline]
5060
pub fn def_id(&self) -> DefId {
@@ -59,15 +69,46 @@ impl<'tcx> InstanceDef<'tcx> {
5969
}
6070
}
6171

62-
#[inline]
63-
pub fn def_ty<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
64-
tcx.type_of(self.def_id())
65-
}
66-
6772
#[inline]
6873
pub fn attrs<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::Attributes<'tcx> {
6974
tcx.get_attrs(self.def_id())
7075
}
76+
77+
pub fn is_inline<'a>(
78+
&self,
79+
tcx: TyCtxt<'a, 'tcx, 'tcx>
80+
) -> bool {
81+
use hir::map::DefPathData;
82+
let def_id = match *self {
83+
ty::InstanceDef::Item(def_id) => def_id,
84+
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
85+
_ => return true
86+
};
87+
match tcx.def_key(def_id).disambiguated_data.data {
88+
DefPathData::StructCtor |
89+
DefPathData::EnumVariant(..) |
90+
DefPathData::ClosureExpr => true,
91+
_ => false
92+
}
93+
}
94+
95+
pub fn requires_local<'a>(
96+
&self,
97+
tcx: TyCtxt<'a, 'tcx, 'tcx>
98+
) -> bool {
99+
use syntax::attr::requests_inline;
100+
if self.is_inline(tcx) {
101+
return true
102+
}
103+
if let ty::InstanceDef::DropGlue(..) = *self {
104+
// Drop glue wants to be instantiated at every translation
105+
// unit, but without an #[inline] hint. We should make this
106+
// available to normal end-users.
107+
return true
108+
}
109+
requests_inline(&self.attrs(tcx)[..]) ||
110+
tcx.is_const_fn(self.def_id())
111+
}
71112
}
72113

73114
impl<'tcx> fmt::Display for Instance<'tcx> {

src/librustc/ty/maps/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use middle::resolve_lifetime::{ResolveLifetimes, Region, ObjectLifetimeDefault};
2727
use middle::stability::{self, DeprecationEntry};
2828
use middle::lang_items::{LanguageItems, LangItem};
2929
use middle::exported_symbols::SymbolExportLevel;
30-
use middle::trans::{CodegenUnit, Stats};
30+
use mir::mono::{CodegenUnit, Stats};
3131
use mir;
3232
use session::{CompileResult, CrateDisambiguator};
3333
use session::config::OutputFilenames;

src/librustc_mir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ syntax = { path = "../libsyntax" }
2323
syntax_pos = { path = "../libsyntax_pos" }
2424
byteorder = { version = "1.1", features = ["i128"] }
2525
rustc_apfloat = { path = "../librustc_apfloat" }
26+
rustc_trans_utils = { path = "../librustc_trans_utils" }

src/librustc_mir/interpret/const_eval.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ pub fn eval_body<'a, 'tcx>(
5757
if ecx.tcx.has_attr(instance.def_id(), "linkage") {
5858
return Err(ConstEvalError::NotConst("extern global".to_string()).into());
5959
}
60-
// FIXME(eddyb) use `Instance::ty` when it becomes available.
61-
let instance_ty =
62-
ecx.monomorphize(instance.def.def_ty(tcx), instance.substs);
60+
let instance_ty = instance.ty(tcx);
6361
if tcx.interpret_interner.borrow().get_cached(cid).is_none() {
6462
let mir = ecx.load_mir(instance.def)?;
6563
let layout = ecx.layout_of(instance_ty)?;

src/librustc_mir/interpret/step.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
172172
M::global_item_with_linkage(self, cid.instance, mutability)?;
173173
return Ok(false);
174174
}
175-
// FIXME(eddyb) use `Instance::ty` when it becomes available.
176-
let instance_ty =
177-
self.monomorphize(instance.def.def_ty(self.tcx), instance.substs);
175+
let instance_ty = instance.ty(self.tcx);
178176
let layout = self.layout_of(instance_ty)?;
179177
assert!(!layout.is_unsized());
180178
let ptr = self.memory.allocate(

src/librustc_mir/interpret/terminator/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
7272
ty::TyFnPtr(sig) => {
7373
let fn_ptr = self.value_to_primval(func)?.to_ptr()?;
7474
let instance = self.memory.get_fn(fn_ptr)?;
75-
// FIXME(eddyb) use `Instance::ty` when it becomes available.
76-
let instance_ty =
77-
self.monomorphize(instance.def.def_ty(self.tcx), instance.substs);
75+
let instance_ty = instance.ty(self.tcx);
7876
match instance_ty.sty {
7977
ty::TyFnDef(..) => {
8078
let real_sig = instance_ty.fn_sig(self.tcx);

src/librustc_mir/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern crate core; // for NonZero
5454
extern crate log_settings;
5555
extern crate rustc_apfloat;
5656
extern crate byteorder;
57+
extern crate rustc_trans_utils;
5758

5859
mod diagnostics;
5960

@@ -65,6 +66,7 @@ mod shim;
6566
pub mod transform;
6667
pub mod util;
6768
pub mod interpret;
69+
pub mod monomorphize;
6870

6971
use rustc::ty::maps::Providers;
7072

0 commit comments

Comments
 (0)