Skip to content

Commit 802bb57

Browse files
trans: Use CrateContext::empty_substs_for_def_id() instead of Substs::empty() where appropriate.
1 parent 64bc3c2 commit 802bb57

File tree

8 files changed

+48
-18
lines changed

8 files changed

+48
-18
lines changed

src/librustc_trans/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
494494
_ => bug!("expected fn item type, found {}", ty)
495495
};
496496

497-
let instance = Instance::mono(ccx.tcx(), def_id);
497+
let instance = Instance::mono(ccx.shared(), def_id);
498498
if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
499499
return immediate_rvalue(llfn, fn_ptr_ty);
500500
}

src/librustc_trans/collector.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(scx: &SharedCrateContext<'a, 'tcx>,
348348
let mir = errors::expect(scx.sess().diagnostic(), scx.get_mir(def_id),
349349
|| format!("Could not find MIR for static: {:?}", def_id));
350350

351-
let empty_substs = scx.tcx().mk_substs(Substs::empty());
351+
let empty_substs = scx.empty_substs_for_def_id(def_id);
352352
let mut visitor = MirNeighborCollector {
353353
scx: scx,
354354
mir: &mir,
@@ -496,10 +496,11 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
496496
.unwrap_or_else(|e| self.scx.sess().fatal(&e));
497497

498498
assert!(can_have_local_instance(self.scx.tcx(), exchange_malloc_fn_def_id));
499+
let empty_substs = self.scx.empty_substs_for_def_id(exchange_malloc_fn_def_id);
499500
let exchange_malloc_fn_trans_item =
500501
create_fn_trans_item(self.scx.tcx(),
501502
exchange_malloc_fn_def_id,
502-
self.scx.tcx().mk_substs(Substs::empty()),
503+
empty_substs,
503504
self.param_substs);
504505

505506
self.output.push(exchange_malloc_fn_trans_item);
@@ -679,10 +680,11 @@ fn find_drop_glue_neighbors<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>,
679680
.unwrap_or_else(|e| scx.sess().fatal(&e));
680681

681682
assert!(can_have_local_instance(scx.tcx(), exchange_free_fn_def_id));
683+
let fn_substs = scx.empty_substs_for_def_id(exchange_free_fn_def_id);
682684
let exchange_free_fn_trans_item =
683685
create_fn_trans_item(scx.tcx(),
684686
exchange_free_fn_def_id,
685-
scx.tcx().mk_substs(Substs::empty()),
687+
fn_substs,
686688
scx.tcx().mk_substs(Substs::empty()));
687689

688690
output.push(exchange_free_fn_trans_item);
@@ -1111,7 +1113,7 @@ impl<'b, 'a, 'v> hir_visit::Visitor<'v> for RootCollector<'b, 'a, 'v> {
11111113
debug!("RootCollector: ItemFn({})",
11121114
def_id_to_string(self.scx.tcx(), def_id));
11131115

1114-
let instance = Instance::mono(self.scx.tcx(), def_id);
1116+
let instance = Instance::mono(self.scx, def_id);
11151117
self.output.push(TransItem::Fn(instance));
11161118
}
11171119
}
@@ -1148,7 +1150,7 @@ impl<'b, 'a, 'v> hir_visit::Visitor<'v> for RootCollector<'b, 'a, 'v> {
11481150
debug!("RootCollector: MethodImplItem({})",
11491151
def_id_to_string(self.scx.tcx(), def_id));
11501152

1151-
let instance = Instance::mono(self.scx.tcx(), def_id);
1153+
let instance = Instance::mono(self.scx, def_id);
11521154
self.output.push(TransItem::Fn(instance));
11531155
}
11541156
}

src/librustc_trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
10121012
-> Datum<'tcx, Lvalue> {
10131013
let ty = ccx.tcx().lookup_item_type(def_id).ty;
10141014

1015-
let instance = Instance::mono(ccx.tcx(), def_id);
1015+
let instance = Instance::mono(ccx.shared(), def_id);
10161016
if let Some(&g) = ccx.instances().borrow().get(&instance) {
10171017
return Datum::new(g, ty, Lvalue::new("static"));
10181018
}

src/librustc_trans/context.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,21 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
488488
pub fn translation_items(&self) -> &RefCell<FnvHashMap<TransItem<'tcx>, TransItemState>> {
489489
&self.translation_items
490490
}
491+
492+
/// Given the def-id of some item that has no type parameters, make
493+
/// a suitable "empty substs" for it.
494+
pub fn empty_substs_for_def_id(&self, item_def_id: DefId) -> &'tcx Substs<'tcx> {
495+
let scheme = self.tcx().lookup_item_type(item_def_id);
496+
self.empty_substs_for_scheme(&scheme)
497+
}
498+
499+
pub fn empty_substs_for_scheme(&self, scheme: &ty::TypeScheme<'tcx>)
500+
-> &'tcx Substs<'tcx> {
501+
assert!(scheme.generics.types.is_empty());
502+
self.tcx().mk_substs(
503+
Substs::new(VecPerParamSpace::empty(),
504+
scheme.generics.regions.map(|_| ty::ReStatic)))
505+
}
491506
}
492507

493508
impl<'tcx> LocalCrateContext<'tcx> {
@@ -902,16 +917,12 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
902917
/// Given the def-id of some item that has no type parameters, make
903918
/// a suitable "empty substs" for it.
904919
pub fn empty_substs_for_def_id(&self, item_def_id: DefId) -> &'tcx Substs<'tcx> {
905-
let scheme = self.tcx().lookup_item_type(item_def_id);
906-
self.empty_substs_for_scheme(&scheme)
920+
self.shared().empty_substs_for_def_id(item_def_id)
907921
}
908922

909923
pub fn empty_substs_for_scheme(&self, scheme: &ty::TypeScheme<'tcx>)
910924
-> &'tcx Substs<'tcx> {
911-
assert!(scheme.generics.types.is_empty());
912-
self.tcx().mk_substs(
913-
Substs::new(VecPerParamSpace::empty(),
914-
scheme.generics.regions.map(|_| ty::ReStatic)))
925+
self.shared().empty_substs_for_scheme(scheme)
915926
}
916927
}
917928

src/librustc_trans/mir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,6 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
854854

855855
pub fn trans_static_initializer(ccx: &CrateContext, def_id: DefId)
856856
-> Result<ValueRef, ConstEvalFailure> {
857-
let instance = Instance::mono(ccx.tcx(), def_id);
857+
let instance = Instance::mono(ccx.shared(), def_id);
858858
MirConstContext::trans_def(ccx, instance, vec![]).map(|c| c.llval)
859859
}

src/librustc_trans/monomorphize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ impl<'tcx> Instance<'tcx> {
183183
assert!(substs.regions.iter().all(|&r| r == ty::ReStatic));
184184
Instance { def: def_id, substs: substs }
185185
}
186-
pub fn mono<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Instance<'tcx> {
187-
Instance::new(def_id, tcx.mk_substs(Substs::empty()))
186+
pub fn mono<'a>(scx: &SharedCrateContext<'a, 'tcx>, def_id: DefId) -> Instance<'tcx> {
187+
Instance::new(def_id, scx.empty_substs_for_def_id(def_id))
188188
}
189189
}
190190

src/librustc_trans/symbol_names_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
5252
for attr in tcx.get_attrs(def_id).iter() {
5353
if attr.check_name(SYMBOL_NAME) {
5454
// for now, can only use on monomorphic names
55-
let instance = Instance::mono(tcx, def_id);
55+
let instance = Instance::mono(self.ccx.shared(), def_id);
5656
let name = symbol_names::exported_name(self.ccx, &instance);
5757
tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
5858
} else if attr.check_name(ITEM_PATH) {

src/librustc_trans/trans_item.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
// Copyright 2016 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+
11+
//! Walks the crate looking for items/impl-items/trait-items that have
12+
//! either a `rustc_symbol_name` or `rustc_item_path` attribute and
13+
//! generates an error giving, respectively, the symbol name or
14+
//! item-path. This is used for unit testing the code that generates
15+
//! paths etc in all kinds of annoying scenarios.
16+
117
use base::llvm_linkage_by_name;
218
use glue::DropGlueKind;
319
use llvm;
@@ -333,7 +349,8 @@ impl<'tcx> TransItem<'tcx> {
333349
},
334350
TransItem::Static(node_id) => {
335351
let def_id = hir_map.local_def_id(node_id);
336-
let instance = Instance::mono(tcx, def_id);
352+
let empty_substs = tcx.mk_substs(subst::Substs::empty());
353+
let instance = Instance::new(def_id, empty_substs);
337354
to_string_internal(tcx, "static ", instance)
338355
},
339356
};

0 commit comments

Comments
 (0)