Skip to content

Commit

Permalink
trans: Use CrateContext::empty_substs_for_def_id() instead of Substs:…
Browse files Browse the repository at this point in the history
…:empty() where appropriate.
  • Loading branch information
michaelwoerister committed May 11, 2016
1 parent 64bc3c2 commit 802bb57
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/librustc_trans/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ fn get_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
_ => bug!("expected fn item type, found {}", ty)
};

let instance = Instance::mono(ccx.tcx(), def_id);
let instance = Instance::mono(ccx.shared(), def_id);
if let Some(&llfn) = ccx.instances().borrow().get(&instance) {
return immediate_rvalue(llfn, fn_ptr_ty);
}
Expand Down
12 changes: 7 additions & 5 deletions src/librustc_trans/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(scx: &SharedCrateContext<'a, 'tcx>,
let mir = errors::expect(scx.sess().diagnostic(), scx.get_mir(def_id),
|| format!("Could not find MIR for static: {:?}", def_id));

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

assert!(can_have_local_instance(self.scx.tcx(), exchange_malloc_fn_def_id));
let empty_substs = self.scx.empty_substs_for_def_id(exchange_malloc_fn_def_id);
let exchange_malloc_fn_trans_item =
create_fn_trans_item(self.scx.tcx(),
exchange_malloc_fn_def_id,
self.scx.tcx().mk_substs(Substs::empty()),
empty_substs,
self.param_substs);

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

assert!(can_have_local_instance(scx.tcx(), exchange_free_fn_def_id));
let fn_substs = scx.empty_substs_for_def_id(exchange_free_fn_def_id);
let exchange_free_fn_trans_item =
create_fn_trans_item(scx.tcx(),
exchange_free_fn_def_id,
scx.tcx().mk_substs(Substs::empty()),
fn_substs,
scx.tcx().mk_substs(Substs::empty()));

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

let instance = Instance::mono(self.scx.tcx(), def_id);
let instance = Instance::mono(self.scx, def_id);
self.output.push(TransItem::Fn(instance));
}
}
Expand Down Expand Up @@ -1148,7 +1150,7 @@ impl<'b, 'a, 'v> hir_visit::Visitor<'v> for RootCollector<'b, 'a, 'v> {
debug!("RootCollector: MethodImplItem({})",
def_id_to_string(self.scx.tcx(), def_id));

let instance = Instance::mono(self.scx.tcx(), def_id);
let instance = Instance::mono(self.scx, def_id);
self.output.push(TransItem::Fn(instance));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
-> Datum<'tcx, Lvalue> {
let ty = ccx.tcx().lookup_item_type(def_id).ty;

let instance = Instance::mono(ccx.tcx(), def_id);
let instance = Instance::mono(ccx.shared(), def_id);
if let Some(&g) = ccx.instances().borrow().get(&instance) {
return Datum::new(g, ty, Lvalue::new("static"));
}
Expand Down
23 changes: 17 additions & 6 deletions src/librustc_trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,21 @@ impl<'b, 'tcx> SharedCrateContext<'b, 'tcx> {
pub fn translation_items(&self) -> &RefCell<FnvHashMap<TransItem<'tcx>, TransItemState>> {
&self.translation_items
}

/// Given the def-id of some item that has no type parameters, make
/// a suitable "empty substs" for it.
pub fn empty_substs_for_def_id(&self, item_def_id: DefId) -> &'tcx Substs<'tcx> {
let scheme = self.tcx().lookup_item_type(item_def_id);
self.empty_substs_for_scheme(&scheme)
}

pub fn empty_substs_for_scheme(&self, scheme: &ty::TypeScheme<'tcx>)
-> &'tcx Substs<'tcx> {
assert!(scheme.generics.types.is_empty());
self.tcx().mk_substs(
Substs::new(VecPerParamSpace::empty(),
scheme.generics.regions.map(|_| ty::ReStatic)))
}
}

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

pub fn empty_substs_for_scheme(&self, scheme: &ty::TypeScheme<'tcx>)
-> &'tcx Substs<'tcx> {
assert!(scheme.generics.types.is_empty());
self.tcx().mk_substs(
Substs::new(VecPerParamSpace::empty(),
scheme.generics.regions.map(|_| ty::ReStatic)))
self.shared().empty_substs_for_scheme(scheme)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,6 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {

pub fn trans_static_initializer(ccx: &CrateContext, def_id: DefId)
-> Result<ValueRef, ConstEvalFailure> {
let instance = Instance::mono(ccx.tcx(), def_id);
let instance = Instance::mono(ccx.shared(), def_id);
MirConstContext::trans_def(ccx, instance, vec![]).map(|c| c.llval)
}
4 changes: 2 additions & 2 deletions src/librustc_trans/monomorphize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ impl<'tcx> Instance<'tcx> {
assert!(substs.regions.iter().all(|&r| r == ty::ReStatic));
Instance { def: def_id, substs: substs }
}
pub fn mono<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Instance<'tcx> {
Instance::new(def_id, tcx.mk_substs(Substs::empty()))
pub fn mono<'a>(scx: &SharedCrateContext<'a, 'tcx>, def_id: DefId) -> Instance<'tcx> {
Instance::new(def_id, scx.empty_substs_for_def_id(def_id))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/symbol_names_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a, 'tcx> SymbolNamesTest<'a, 'tcx> {
for attr in tcx.get_attrs(def_id).iter() {
if attr.check_name(SYMBOL_NAME) {
// for now, can only use on monomorphic names
let instance = Instance::mono(tcx, def_id);
let instance = Instance::mono(self.ccx.shared(), def_id);
let name = symbol_names::exported_name(self.ccx, &instance);
tcx.sess.span_err(attr.span, &format!("symbol-name({})", name));
} else if attr.check_name(ITEM_PATH) {
Expand Down
19 changes: 18 additions & 1 deletion src/librustc_trans/trans_item.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Walks the crate looking for items/impl-items/trait-items that have
//! either a `rustc_symbol_name` or `rustc_item_path` attribute and
//! generates an error giving, respectively, the symbol name or
//! item-path. This is used for unit testing the code that generates
//! paths etc in all kinds of annoying scenarios.

use base::llvm_linkage_by_name;
use glue::DropGlueKind;
use llvm;
Expand Down Expand Up @@ -333,7 +349,8 @@ impl<'tcx> TransItem<'tcx> {
},
TransItem::Static(node_id) => {
let def_id = hir_map.local_def_id(node_id);
let instance = Instance::mono(tcx, def_id);
let empty_substs = tcx.mk_substs(subst::Substs::empty());
let instance = Instance::new(def_id, empty_substs);
to_string_internal(tcx, "static ", instance)
},
};
Expand Down

0 comments on commit 802bb57

Please sign in to comment.