Skip to content

Commit c72240a

Browse files
committed
rustc_trans: Refactor collection to use tcx
This commit refactors the `collect_crate_translation_items` function to only require the `TyCtxt` instead of a `SharedCrateContext` in preparation for query-ifying this portion of trans.
1 parent 1cdd689 commit c72240a

File tree

17 files changed

+166
-155
lines changed

17 files changed

+166
-155
lines changed

src/librustc_trans/abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ pub struct FnType<'tcx> {
612612
impl<'a, 'tcx> FnType<'tcx> {
613613
pub fn of_instance(ccx: &CrateContext<'a, 'tcx>, instance: &ty::Instance<'tcx>)
614614
-> Self {
615-
let fn_ty = instance_ty(ccx.shared(), &instance);
615+
let fn_ty = instance_ty(ccx.tcx(), &instance);
616616
let sig = ty_fn_sig(ccx, fn_ty);
617617
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&sig);
618618
Self::new(ccx, sig, &[])

src/librustc_trans/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ pub fn trans_instance<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, instance: Instance
578578
// release builds.
579579
info!("trans_instance({})", instance);
580580

581-
let fn_ty = common::instance_ty(ccx.shared(), &instance);
581+
let fn_ty = common::instance_ty(ccx.tcx(), &instance);
582582
let sig = common::ty_fn_sig(ccx, fn_ty);
583583
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&sig);
584584

@@ -1424,7 +1424,7 @@ fn collect_and_partition_translation_items<'a, 'tcx>(scx: &SharedCrateContext<'a
14241424

14251425
let (items, inlining_map) =
14261426
time(time_passes, "translation item collection", || {
1427-
collector::collect_crate_translation_items(&scx,
1427+
collector::collect_crate_translation_items(scx.tcx(),
14281428
exported_symbols,
14291429
collection_mode)
14301430
});

src/librustc_trans/callee.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
4545
assert!(!instance.substs.has_escaping_regions());
4646
assert!(!instance.substs.has_param_types());
4747

48-
let fn_ty = common::instance_ty(ccx.shared(), &instance);
48+
let fn_ty = common::instance_ty(ccx.tcx(), &instance);
4949
if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
5050
return llfn;
5151
}
@@ -148,5 +148,5 @@ pub fn resolve_and_get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
148148
substs: &'tcx Substs<'tcx>)
149149
-> ValueRef
150150
{
151-
get_fn(ccx, monomorphize::resolve(ccx.shared(), def_id, substs))
151+
get_fn(ccx, monomorphize::resolve(ccx.tcx(), def_id, substs))
152152
}

src/librustc_trans/collector.rs

Lines changed: 93 additions & 95 deletions
Large diffs are not rendered by default.

src/librustc_trans/common.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use machine;
2626
use monomorphize;
2727
use type_::Type;
2828
use value::Value;
29+
use rustc::traits;
2930
use rustc::ty::{self, Ty, TyCtxt};
3031
use rustc::ty::layout::{Layout, LayoutTyper};
3132
use rustc::ty::subst::{Kind, Subst, Substs};
@@ -37,7 +38,7 @@ use std::iter;
3738
use syntax::abi::Abi;
3839
use syntax::attr;
3940
use syntax::symbol::InternedString;
40-
use syntax_pos::Span;
41+
use syntax_pos::{Span, DUMMY_SP};
4142

4243
pub use context::{CrateContext, SharedCrateContext};
4344

@@ -140,6 +141,18 @@ pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -
140141
!layout.is_unsized() && layout.size(ccx).bytes() == 0
141142
}
142143

144+
pub fn type_needs_drop<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
145+
ty.needs_drop(tcx, ty::ParamEnv::empty(traits::Reveal::All))
146+
}
147+
148+
pub fn type_is_sized<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
149+
ty.is_sized(tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
150+
}
151+
152+
pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bool {
153+
ty.is_freeze(tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
154+
}
155+
143156
/*
144157
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
145158
*
@@ -573,20 +586,20 @@ pub fn is_inline_instance<'a, 'tcx>(
573586
}
574587

575588
/// Given a DefId and some Substs, produces the monomorphic item type.
576-
pub fn def_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>,
589+
pub fn def_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
577590
def_id: DefId,
578591
substs: &'tcx Substs<'tcx>)
579592
-> Ty<'tcx>
580593
{
581-
let ty = shared.tcx().type_of(def_id);
582-
shared.tcx().trans_apply_param_substs(substs, &ty)
594+
let ty = tcx.type_of(def_id);
595+
tcx.trans_apply_param_substs(substs, &ty)
583596
}
584597

585598
/// Return the substituted type of an instance.
586-
pub fn instance_ty<'a, 'tcx>(shared: &SharedCrateContext<'a, 'tcx>,
599+
pub fn instance_ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
587600
instance: &ty::Instance<'tcx>)
588601
-> Ty<'tcx>
589602
{
590-
let ty = instance.def.def_ty(shared.tcx());
591-
shared.tcx().trans_apply_param_substs(instance.substs, &ty)
603+
let ty = instance.def.def_ty(tcx);
604+
tcx.trans_apply_param_substs(instance.substs, &ty)
592605
}

src/librustc_trans/consts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn get_static(ccx: &CrateContext, def_id: DefId) -> ValueRef {
109109
return g;
110110
}
111111

112-
let ty = common::instance_ty(ccx.shared(), &instance);
112+
let ty = common::instance_ty(ccx.tcx(), &instance);
113113
let g = if let Some(id) = ccx.tcx().hir.as_local_node_id(def_id) {
114114

115115
let llty = type_of::type_of(ccx, ty);
@@ -269,7 +269,7 @@ pub fn trans_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
269269
};
270270

271271
let instance = Instance::mono(ccx.tcx(), def_id);
272-
let ty = common::instance_ty(ccx.shared(), &instance);
272+
let ty = common::instance_ty(ccx.tcx(), &instance);
273273
let llty = type_of::type_of(ccx, ty);
274274
let g = if val_llty == llty {
275275
g

src/librustc_trans/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use common;
1112
use llvm;
1213
use llvm::{ContextRef, ModuleRef, ValueRef};
1314
use rustc::dep_graph::{DepGraph, DepGraphSafe};
@@ -39,7 +40,6 @@ use std::str;
3940
use std::sync::Arc;
4041
use std::marker::PhantomData;
4142
use syntax::symbol::InternedString;
42-
use syntax_pos::DUMMY_SP;
4343
use abi::Abi;
4444

4545
#[derive(Clone, Default)]
@@ -319,15 +319,15 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
319319
}
320320

321321
pub fn type_needs_drop(&self, ty: Ty<'tcx>) -> bool {
322-
ty.needs_drop(self.tcx, ty::ParamEnv::empty(traits::Reveal::All))
322+
common::type_needs_drop(self.tcx, ty)
323323
}
324324

325325
pub fn type_is_sized(&self, ty: Ty<'tcx>) -> bool {
326-
ty.is_sized(self.tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
326+
common::type_is_sized(self.tcx, ty)
327327
}
328328

329329
pub fn type_is_freeze(&self, ty: Ty<'tcx>) -> bool {
330-
ty.is_freeze(self.tcx, ty::ParamEnv::empty(traits::Reveal::All), DUMMY_SP)
330+
common::type_is_freeze(self.tcx, ty)
331331
}
332332

333333
pub fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> {

src/librustc_trans/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,7 @@ pub fn create_global_var_metadata(cx: &CrateContext,
18031803
};
18041804

18051805
let is_local_to_unit = is_node_local_to_unit(cx, node_id);
1806-
let variable_type = common::def_ty(cx.shared(), node_def_id, Substs::empty());
1806+
let variable_type = common::def_ty(cx.tcx(), node_def_id, Substs::empty());
18071807
let type_metadata = type_metadata(cx, variable_type, span);
18081808
let var_name = tcx.item_name(node_def_id).to_string();
18091809
let linkage_name = mangled_name_of_item(cx, node_def_id, "");

src/librustc_trans/debuginfo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
428428
// If the method does *not* belong to a trait, proceed
429429
if cx.tcx().trait_id_of_impl(impl_def_id).is_none() {
430430
let impl_self_ty =
431-
common::def_ty(cx.shared(), impl_def_id, instance.substs);
431+
common::def_ty(cx.tcx(), impl_def_id, instance.substs);
432432

433433
// Only "class" methods are generally understood by LLVM,
434434
// so avoid methods on other types (e.g. `<*mut T>::null`).

src/librustc_trans/glue.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414

1515
use std;
1616

17-
use llvm;
18-
use llvm::{ValueRef};
19-
use rustc::ty::{self, Ty};
20-
use rustc::ty::layout::LayoutTyper;
17+
use builder::Builder;
2118
use common::*;
19+
use llvm::{ValueRef};
20+
use llvm;
2221
use meth;
2322
use monomorphize;
23+
use rustc::traits;
24+
use rustc::ty::layout::LayoutTyper;
25+
use rustc::ty::{self, Ty, TypeFoldable, TyCtxt};
2426
use value::Value;
25-
use builder::Builder;
2627

2728
pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, info: ValueRef)
2829
-> (ValueRef, ValueRef) {

src/librustc_trans/meth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
8080
let nullptr = C_null(Type::nil(ccx).ptr_to());
8181

8282
let mut components: Vec<_> = [
83-
callee::get_fn(ccx, monomorphize::resolve_drop_in_place(ccx.shared(), ty)),
83+
callee::get_fn(ccx, monomorphize::resolve_drop_in_place(ccx.tcx(), ty)),
8484
C_usize(ccx, ccx.size_of(ty)),
8585
C_usize(ccx, ccx.align_of(ty) as u64)
8686
].iter().cloned().collect();

src/librustc_trans/mir/block.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
265265
mir::TerminatorKind::Drop { ref location, target, unwind } => {
266266
let ty = location.ty(self.mir, bcx.tcx()).to_ty(bcx.tcx());
267267
let ty = self.monomorphize(&ty);
268-
let drop_fn = monomorphize::resolve_drop_in_place(bcx.ccx.shared(), ty);
268+
let drop_fn = monomorphize::resolve_drop_in_place(bcx.ccx.tcx(), ty);
269269

270270
if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def {
271271
// we don't actually need to drop anything.
@@ -429,7 +429,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
429429

430430
let (instance, mut llfn) = match callee.ty.sty {
431431
ty::TyFnDef(def_id, substs) => {
432-
(Some(monomorphize::resolve(bcx.ccx.shared(), def_id, substs)),
432+
(Some(monomorphize::resolve(bcx.ccx.tcx(), def_id, substs)),
433433
None)
434434
}
435435
ty::TyFnPtr(_) => {
@@ -546,7 +546,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
546546
};
547547

548548
let callee_ty = common::instance_ty(
549-
bcx.ccx.shared(), instance.as_ref().unwrap());
549+
bcx.ccx.tcx(), instance.as_ref().unwrap());
550550
trans_intrinsic_call(&bcx, callee_ty, &fn_ty, &llargs, dest,
551551
terminator.source_info.span);
552552

src/librustc_trans/mir/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
261261
substs: &'tcx Substs<'tcx>,
262262
args: IndexVec<mir::Local, Result<Const<'tcx>, ConstEvalErr<'tcx>>>)
263263
-> Result<Const<'tcx>, ConstEvalErr<'tcx>> {
264-
let instance = monomorphize::resolve(ccx.shared(), def_id, substs);
264+
let instance = monomorphize::resolve(ccx.tcx(), def_id, substs);
265265
let mir = ccx.tcx().instance_mir(instance.def);
266266
MirConstContext::new(ccx, &mir, instance.substs, args).trans()
267267
}

src/librustc_trans/mir/rvalue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
222222
match operand.ty.sty {
223223
ty::TyClosure(def_id, substs) => {
224224
let instance = monomorphize::resolve_closure(
225-
bcx.ccx.shared(), def_id, substs, ty::ClosureKind::FnOnce);
225+
bcx.ccx.tcx(), def_id, substs, ty::ClosureKind::FnOnce);
226226
OperandValue::Immediate(callee::get_fn(bcx.ccx, instance))
227227
}
228228
_ => {

src/librustc_trans/monomorphize.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,26 @@ fn needs_fn_once_adapter_shim(actual_closure_kind: ty::ClosureKind,
8585
}
8686

8787
pub fn resolve_closure<'a, 'tcx> (
88-
scx: &SharedCrateContext<'a, 'tcx>,
88+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
8989
def_id: DefId,
9090
substs: ty::ClosureSubsts<'tcx>,
9191
requested_kind: ty::ClosureKind)
9292
-> Instance<'tcx>
9393
{
94-
let actual_kind = scx.tcx().closure_kind(def_id);
94+
let actual_kind = tcx.closure_kind(def_id);
9595

9696
match needs_fn_once_adapter_shim(actual_kind, requested_kind) {
97-
Ok(true) => fn_once_adapter_instance(scx.tcx(), def_id, substs),
97+
Ok(true) => fn_once_adapter_instance(tcx, def_id, substs),
9898
_ => Instance::new(def_id, substs.substs)
9999
}
100100
}
101101

102102
fn resolve_associated_item<'a, 'tcx>(
103-
scx: &SharedCrateContext<'a, 'tcx>,
103+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
104104
trait_item: &ty::AssociatedItem,
105105
trait_id: DefId,
106106
rcvr_substs: &'tcx Substs<'tcx>
107107
) -> Instance<'tcx> {
108-
let tcx = scx.tcx();
109108
let def_id = trait_item.def_id;
110109
debug!("resolve_associated_item(trait_item={:?}, \
111110
trait_id={:?}, \
@@ -132,7 +131,7 @@ fn resolve_associated_item<'a, 'tcx>(
132131
}
133132
traits::VtableClosure(closure_data) => {
134133
let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap();
135-
resolve_closure(scx, closure_data.closure_def_id, closure_data.substs,
134+
resolve_closure(tcx, closure_data.closure_def_id, closure_data.substs,
136135
trait_closure_kind)
137136
}
138137
traits::VtableFnPointer(ref data) => {
@@ -163,21 +162,21 @@ fn resolve_associated_item<'a, 'tcx>(
163162
/// The point where linking happens. Resolve a (def_id, substs)
164163
/// pair to an instance.
165164
pub fn resolve<'a, 'tcx>(
166-
scx: &SharedCrateContext<'a, 'tcx>,
165+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
167166
def_id: DefId,
168167
substs: &'tcx Substs<'tcx>
169168
) -> Instance<'tcx> {
170169
debug!("resolve(def_id={:?}, substs={:?})",
171170
def_id, substs);
172-
let result = if let Some(trait_def_id) = scx.tcx().trait_of_item(def_id) {
171+
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
173172
debug!(" => associated item, attempting to find impl");
174-
let item = scx.tcx().associated_item(def_id);
175-
resolve_associated_item(scx, &item, trait_def_id, substs)
173+
let item = tcx.associated_item(def_id);
174+
resolve_associated_item(tcx, &item, trait_def_id, substs)
176175
} else {
177-
let item_type = def_ty(scx, def_id, substs);
176+
let item_type = def_ty(tcx, def_id, substs);
178177
let def = match item_type.sty {
179178
ty::TyFnDef(..) if {
180-
let f = item_type.fn_sig(scx.tcx());
179+
let f = item_type.fn_sig(tcx);
181180
f.abi() == Abi::RustIntrinsic ||
182181
f.abi() == Abi::PlatformIntrinsic
183182
} =>
@@ -186,9 +185,9 @@ pub fn resolve<'a, 'tcx>(
186185
ty::InstanceDef::Intrinsic(def_id)
187186
}
188187
_ => {
189-
if Some(def_id) == scx.tcx().lang_items().drop_in_place_fn() {
188+
if Some(def_id) == tcx.lang_items().drop_in_place_fn() {
190189
let ty = substs.type_at(0);
191-
if scx.type_needs_drop(ty) {
190+
if common::type_needs_drop(tcx, ty) {
192191
debug!(" => nontrivial drop glue");
193192
ty::InstanceDef::DropGlue(def_id, Some(ty))
194193
} else {
@@ -209,27 +208,27 @@ pub fn resolve<'a, 'tcx>(
209208
}
210209

211210
pub fn resolve_drop_in_place<'a, 'tcx>(
212-
scx: &SharedCrateContext<'a, 'tcx>,
211+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
213212
ty: Ty<'tcx>)
214213
-> ty::Instance<'tcx>
215214
{
216-
let def_id = scx.tcx().require_lang_item(DropInPlaceFnLangItem);
217-
let substs = scx.tcx().intern_substs(&[Kind::from(ty)]);
218-
resolve(scx, def_id, substs)
215+
let def_id = tcx.require_lang_item(DropInPlaceFnLangItem);
216+
let substs = tcx.intern_substs(&[Kind::from(ty)]);
217+
resolve(tcx, def_id, substs)
219218
}
220219

221-
pub fn custom_coerce_unsize_info<'scx, 'tcx>(scx: &SharedCrateContext<'scx, 'tcx>,
222-
source_ty: Ty<'tcx>,
223-
target_ty: Ty<'tcx>)
224-
-> CustomCoerceUnsized {
220+
pub fn custom_coerce_unsize_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
221+
source_ty: Ty<'tcx>,
222+
target_ty: Ty<'tcx>)
223+
-> CustomCoerceUnsized {
225224
let trait_ref = ty::Binder(ty::TraitRef {
226-
def_id: scx.tcx().lang_items().coerce_unsized_trait().unwrap(),
227-
substs: scx.tcx().mk_substs_trait(source_ty, &[target_ty])
225+
def_id: tcx.lang_items().coerce_unsized_trait().unwrap(),
226+
substs: tcx.mk_substs_trait(source_ty, &[target_ty])
228227
});
229228

230-
match scx.tcx().trans_fulfill_obligation(DUMMY_SP, trait_ref) {
229+
match tcx.trans_fulfill_obligation(DUMMY_SP, trait_ref) {
231230
traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {
232-
scx.tcx().coerce_unsized_info(impl_def_id).custom_kind.unwrap()
231+
tcx.coerce_unsized_info(impl_def_id).custom_kind.unwrap()
233232
}
234233
vtable => {
235234
bug!("invalid CoerceUnsized vtable: {:?}", vtable);

src/librustc_trans/partitioning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ fn characteristic_def_id_of_trans_item<'a, 'tcx>(scx: &SharedCrateContext<'a, 't
621621
if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
622622
// This is a method within an inherent impl, find out what the
623623
// self-type is:
624-
let impl_self_ty = common::def_ty(scx, impl_def_id, instance.substs);
624+
let impl_self_ty = common::def_ty(scx.tcx(), impl_def_id, instance.substs);
625625
if let Some(def_id) = characteristic_def_id_of_type(impl_self_ty) {
626626
return Some(def_id);
627627
}

0 commit comments

Comments
 (0)