Skip to content

Commit 15ff0ad

Browse files
Use DefId instead of NodeId in MonoItem::Static.
1 parent d5ed655 commit 15ff0ad

File tree

6 files changed

+64
-41
lines changed

6 files changed

+64
-41
lines changed

src/librustc/mir/mono.rs

Lines changed: 5 additions & 2 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 hir::def_id::DefId;
1112
use syntax::ast::NodeId;
1213
use syntax::symbol::InternedString;
1314
use ty::{Instance, TyCtxt};
@@ -21,7 +22,7 @@ use std::hash::Hash;
2122
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash)]
2223
pub enum MonoItem<'tcx> {
2324
Fn(Instance<'tcx>),
24-
Static(NodeId),
25+
Static(DefId),
2526
GlobalAsm(NodeId),
2627
}
2728

@@ -50,7 +51,9 @@ impl<'tcx> HashStable<StableHashingContext<'tcx>> for MonoItem<'tcx> {
5051
MonoItem::Fn(ref instance) => {
5152
instance.hash_stable(hcx, hasher);
5253
}
53-
MonoItem::Static(node_id) |
54+
MonoItem::Static(def_id) => {
55+
def_id.hash_stable(hcx, hasher);
56+
}
5457
MonoItem::GlobalAsm(node_id) => {
5558
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
5659
node_id.hash_stable(hcx, hasher);

src/librustc_mir/monomorphize/collector.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ fn collect_items_rec<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
368368
let recursion_depth_reset;
369369

370370
match starting_point {
371-
MonoItem::Static(node_id) => {
372-
let def_id = tcx.hir.local_def_id(node_id);
371+
MonoItem::Static(def_id) => {
373372
let instance = Instance::mono(tcx, def_id);
374373

375374
// Sanity check whether this ended up being collected accidentally
@@ -652,8 +651,7 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
652651
let tcx = self.tcx;
653652
let instance = Instance::mono(tcx, static_.def_id);
654653
if should_monomorphize_locally(tcx, &instance) {
655-
let node_id = tcx.hir.as_local_node_id(static_.def_id).unwrap();
656-
self.output.push(MonoItem::Static(node_id));
654+
self.output.push(MonoItem::Static(static_.def_id));
657655
}
658656

659657
self.super_static(static_, context, location);
@@ -946,10 +944,10 @@ impl<'b, 'a, 'v> ItemLikeVisitor<'v> for RootCollector<'b, 'a, 'v> {
946944
self.output.push(MonoItem::GlobalAsm(item.id));
947945
}
948946
hir::ItemStatic(..) => {
947+
let def_id = self.tcx.hir.local_def_id(item.id);
949948
debug!("RootCollector: ItemStatic({})",
950-
def_id_to_string(self.tcx,
951-
self.tcx.hir.local_def_id(item.id)));
952-
self.output.push(MonoItem::Static(item.id));
949+
def_id_to_string(self.tcx, def_id));
950+
self.output.push(MonoItem::Static(def_id));
953951
}
954952
hir::ItemConst(..) => {
955953
// const items only generate mono items if they are

src/librustc_mir/monomorphize/item.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
9797
fn symbol_name(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> ty::SymbolName {
9898
match *self.as_mono_item() {
9999
MonoItem::Fn(instance) => tcx.symbol_name(instance),
100-
MonoItem::Static(node_id) => {
101-
let def_id = tcx.hir.local_def_id(node_id);
100+
MonoItem::Static(def_id) => {
102101
tcx.symbol_name(Instance::mono(tcx, def_id))
103102
}
104103
MonoItem::GlobalAsm(node_id) => {
@@ -159,7 +158,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
159158
fn explicit_linkage(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Option<Linkage> {
160159
let def_id = match *self.as_mono_item() {
161160
MonoItem::Fn(ref instance) => instance.def_id(),
162-
MonoItem::Static(node_id) => tcx.hir.local_def_id(node_id),
161+
MonoItem::Static(def_id) => def_id,
163162
MonoItem::GlobalAsm(..) => return None,
164163
};
165164

@@ -209,7 +208,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
209208
debug!("is_instantiable({:?})", self);
210209
let (def_id, substs) = match *self.as_mono_item() {
211210
MonoItem::Fn(ref instance) => (instance.def_id(), instance.substs),
212-
MonoItem::Static(node_id) => (tcx.hir.local_def_id(node_id), Substs::empty()),
211+
MonoItem::Static(def_id) => (def_id, Substs::empty()),
213212
// global asm never has predicates
214213
MonoItem::GlobalAsm(..) => return true
215214
};
@@ -218,14 +217,11 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
218217
}
219218

220219
fn to_string(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> String {
221-
let hir_map = &tcx.hir;
222-
223220
return match *self.as_mono_item() {
224221
MonoItem::Fn(instance) => {
225222
to_string_internal(tcx, "fn ", instance)
226223
},
227-
MonoItem::Static(node_id) => {
228-
let def_id = hir_map.local_def_id(node_id);
224+
MonoItem::Static(def_id) => {
229225
let instance = Instance::new(def_id, tcx.intern_substs(&[]));
230226
to_string_internal(tcx, "static ", instance)
231227
},
@@ -251,7 +247,9 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
251247
MonoItem::Fn(Instance { def, .. }) => {
252248
tcx.hir.as_local_node_id(def.def_id())
253249
}
254-
MonoItem::Static(node_id) |
250+
MonoItem::Static(def_id) => {
251+
tcx.hir.as_local_node_id(def_id)
252+
}
255253
MonoItem::GlobalAsm(node_id) => {
256254
Some(node_id)
257255
}

src/librustc_mir/monomorphize/partitioning.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ pub trait CodegenUnitExt<'tcx> {
180180
}
181181
}
182182
}
183-
MonoItem::Static(node_id) |
183+
MonoItem::Static(def_id) => {
184+
tcx.hir.as_local_node_id(def_id)
185+
}
184186
MonoItem::GlobalAsm(node_id) => {
185187
Some(node_id)
186188
}
@@ -382,7 +384,15 @@ fn place_root_translation_items<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
382384
};
383385
(Linkage::External, visibility)
384386
}
385-
MonoItem::Static(node_id) |
387+
MonoItem::Static(def_id) => {
388+
let visibility = if tcx.is_exported_symbol(def_id) {
389+
can_be_internalized = false;
390+
default_visibility(def_id)
391+
} else {
392+
Visibility::Hidden
393+
};
394+
(Linkage::External, visibility)
395+
}
386396
MonoItem::GlobalAsm(node_id) => {
387397
let def_id = tcx.hir.local_def_id(node_id);
388398
let visibility = if tcx.is_exported_symbol(def_id) {
@@ -643,7 +653,7 @@ fn characteristic_def_id_of_trans_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
643653

644654
Some(def_id)
645655
}
646-
MonoItem::Static(node_id) |
656+
MonoItem::Static(def_id) => Some(def_id),
647657
MonoItem::GlobalAsm(node_id) => Some(tcx.hir.local_def_id(node_id)),
648658
}
649659
}

src/librustc_trans/consts.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc::hir::map as hir_map;
1616
use rustc::middle::const_val::ConstEvalErr;
1717
use debuginfo;
1818
use base;
19-
use monomorphize::{MonoItem, MonoItemExt};
19+
use monomorphize::MonoItem;
2020
use common::{CodegenCx, val_ty};
2121
use declare;
2222
use monomorphize::Instance;
@@ -110,21 +110,24 @@ pub fn get_static(cx: &CodegenCx, def_id: DefId) -> ValueRef {
110110
return g;
111111
}
112112

113+
let defined_in_current_codegen_unit = cx.codegen_unit
114+
.items()
115+
.contains_key(&MonoItem::Static(def_id));
116+
assert!(!defined_in_current_codegen_unit,
117+
"consts::get_static() should always hit the cache for \
118+
statics defined in the same CGU, but did not for `{:?}`",
119+
def_id);
120+
113121
let ty = instance.ty(cx.tcx);
122+
let sym = cx.tcx.symbol_name(instance);
123+
114124
let g = if let Some(id) = cx.tcx.hir.as_local_node_id(def_id) {
115125

116126
let llty = cx.layout_of(ty).llvm_type(cx);
117127
let (g, attrs) = match cx.tcx.hir.get(id) {
118128
hir_map::NodeItem(&hir::Item {
119129
ref attrs, span, node: hir::ItemStatic(..), ..
120130
}) => {
121-
let sym = MonoItem::Static(id).symbol_name(cx.tcx);
122-
123-
let defined_in_current_codegen_unit = cx.codegen_unit
124-
.items()
125-
.contains_key(&MonoItem::Static(id));
126-
assert!(!defined_in_current_codegen_unit);
127-
128131
if declare::get_declared_value(cx, &sym[..]).is_some() {
129132
span_bug!(span, "trans: Conflicting symbol names for static?");
130133
}
@@ -143,7 +146,7 @@ pub fn get_static(cx: &CodegenCx, def_id: DefId) -> ValueRef {
143146
hir_map::NodeForeignItem(&hir::ForeignItem {
144147
ref attrs, span, node: hir::ForeignItemStatic(..), ..
145148
}) => {
146-
let sym = cx.tcx.symbol_name(instance);
149+
147150
let g = if let Some(name) =
148151
attr::first_attr_value_str_by_name(&attrs, "linkage") {
149152
// If this is a static with a linkage specified, then we need to handle
@@ -203,8 +206,6 @@ pub fn get_static(cx: &CodegenCx, def_id: DefId) -> ValueRef {
203206

204207
g
205208
} else {
206-
let sym = cx.tcx.symbol_name(instance);
207-
208209
// FIXME(nagisa): perhaps the map of externs could be offloaded to llvm somehow?
209210
// FIXME(nagisa): investigate whether it can be changed into define_global
210211
let g = declare::declare_global(cx, &sym, cx.layout_of(ty).llvm_type(cx));
@@ -246,11 +247,10 @@ pub fn get_static(cx: &CodegenCx, def_id: DefId) -> ValueRef {
246247

247248
pub fn trans_static<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
248249
m: hir::Mutability,
249-
id: ast::NodeId,
250+
def_id: DefId,
250251
attrs: &[ast::Attribute])
251252
-> Result<ValueRef, ConstEvalErr<'tcx>> {
252253
unsafe {
253-
let def_id = cx.tcx.hir.local_def_id(id);
254254
let g = get_static(cx, def_id);
255255

256256
let v = ::mir::trans_static_initializer(cx, def_id)?;

src/librustc_trans/trans_item.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ use llvm;
2424
use monomorphize::Instance;
2525
use type_of::LayoutLlvmExt;
2626
use rustc::hir;
27+
use rustc::hir::def_id::DefId;
2728
use rustc::mir::mono::{Linkage, Visibility};
2829
use rustc::ty::TypeFoldable;
2930
use rustc::ty::layout::LayoutOf;
30-
use syntax::ast;
3131
use syntax::attr;
3232
use std::fmt;
3333

@@ -44,11 +44,18 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
4444
cx.codegen_unit.name());
4545

4646
match *self.as_mono_item() {
47-
MonoItem::Static(node_id) => {
47+
MonoItem::Static(def_id) => {
4848
let tcx = cx.tcx;
49+
let node_id = match tcx.hir.as_local_node_id(def_id) {
50+
Some(node_id) => node_id,
51+
None => {
52+
bug!("MonoItemExt::define() called for non-local \
53+
static `{:?}`.", def_id)
54+
}
55+
};
4956
let item = tcx.hir.expect_item(node_id);
5057
if let hir::ItemStatic(_, m, _) = item.node {
51-
match consts::trans_static(&cx, m, item.id, &item.attrs) {
58+
match consts::trans_static(&cx, m, def_id, &item.attrs) {
5259
Ok(_) => { /* Cool, everything's alright. */ },
5360
Err(err) => {
5461
err.report(tcx, item.span, "static");
@@ -91,8 +98,8 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
9198
debug!("symbol {}", &symbol_name);
9299

93100
match *self.as_mono_item() {
94-
MonoItem::Static(node_id) => {
95-
predefine_static(cx, node_id, linkage, visibility, &symbol_name);
101+
MonoItem::Static(def_id) => {
102+
predefine_static(cx, def_id, linkage, visibility, &symbol_name);
96103
}
97104
MonoItem::Fn(instance) => {
98105
predefine_fn(cx, instance, linkage, visibility, &symbol_name);
@@ -126,11 +133,18 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug + BaseMonoItemExt<'a, 'tcx> {
126133
impl<'a, 'tcx> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {}
127134

128135
fn predefine_static<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
129-
node_id: ast::NodeId,
136+
def_id: DefId,
130137
linkage: Linkage,
131138
visibility: Visibility,
132139
symbol_name: &str) {
133-
let def_id = cx.tcx.hir.local_def_id(node_id);
140+
let node_id = match cx.tcx.hir.as_local_node_id(def_id) {
141+
Some(node_id) => node_id,
142+
None => {
143+
bug!("MonoItemExt::predefine() called for non-local static `{:?}`.",
144+
def_id)
145+
}
146+
};
147+
134148
let instance = Instance::mono(cx.tcx, def_id);
135149
let ty = instance.ty(cx.tcx);
136150
let llty = cx.layout_of(ty).llvm_type(cx);

0 commit comments

Comments
 (0)