Skip to content

Commit feeb069

Browse files
authored
Rollup merge of #48353 - michaelwoerister:monoitem-static-defid, r=eddyb
Allow for instantiating statics from upstream crates This PR makes the infrastructure around translating statics a bit more flexible so that it can also instantiate statics from upstream crates if the need arises. This is preparatory work for a MIR-only RLIBs prototype, where the instantiation of a `static` may be deferred until a leaf crate. r? @eddyb (feel free to assign to someone else if you're busy)
2 parents 9dbc907 + 89b3ef3 commit feeb069

File tree

18 files changed

+204
-90
lines changed

18 files changed

+204
-90
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ define_dep_nodes!( <'tcx>
628628
[eval_always] CollectAndPartitionTranslationItems,
629629
[] ExportName(DefId),
630630
[] ContainsExternIndicator(DefId),
631-
[] IsTranslatedFunction(DefId),
631+
[] IsTranslatedItem(DefId),
632632
[] CodegenUnit(InternedString),
633633
[] CompileCodegenUnit(InternedString),
634634
[input] OutputFilenames,

src/librustc/hir/map/mod.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
2222
use syntax::abi::Abi;
2323
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
2424
use syntax::codemap::Spanned;
25+
use syntax::ext::base::MacroKind;
2526
use syntax_pos::Span;
2627

2728
use hir::*;
@@ -32,13 +33,15 @@ use util::nodemap::{DefIdMap, FxHashMap};
3233
use arena::TypedArena;
3334
use std::cell::RefCell;
3435
use std::io;
36+
use ty::TyCtxt;
3537

3638
pub mod blocks;
3739
mod collector;
3840
mod def_collector;
3941
pub mod definitions;
4042
mod hir_id_validator;
4143

44+
4245
pub const ITEM_LIKE_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::Low;
4346
pub const REGULAR_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;
4447

@@ -373,6 +376,92 @@ impl<'hir> Map<'hir> {
373376
self.definitions.as_local_node_id(def_id.to_def_id()).unwrap()
374377
}
375378

379+
pub fn describe_def(&self, node_id: NodeId) -> Option<Def> {
380+
let node = if let Some(node) = self.find(node_id) {
381+
node
382+
} else {
383+
return None
384+
};
385+
386+
match node {
387+
NodeItem(item) => {
388+
let def_id = || {
389+
self.local_def_id(item.id)
390+
};
391+
392+
match item.node {
393+
ItemStatic(_, m, _) => Some(Def::Static(def_id(),
394+
m == MutMutable)),
395+
ItemConst(..) => Some(Def::Const(def_id())),
396+
ItemFn(..) => Some(Def::Fn(def_id())),
397+
ItemMod(..) => Some(Def::Mod(def_id())),
398+
ItemGlobalAsm(..) => Some(Def::GlobalAsm(def_id())),
399+
ItemTy(..) => Some(Def::TyAlias(def_id())),
400+
ItemEnum(..) => Some(Def::Enum(def_id())),
401+
ItemStruct(..) => Some(Def::Struct(def_id())),
402+
ItemUnion(..) => Some(Def::Union(def_id())),
403+
ItemTrait(..) => Some(Def::Trait(def_id())),
404+
ItemTraitAlias(..) => {
405+
bug!("trait aliases are not yet implemented (see issue #41517)")
406+
},
407+
ItemExternCrate(_) |
408+
ItemUse(..) |
409+
ItemForeignMod(..) |
410+
ItemImpl(..) => None,
411+
}
412+
}
413+
NodeForeignItem(item) => {
414+
let def_id = self.local_def_id(item.id);
415+
match item.node {
416+
ForeignItemFn(..) => Some(Def::Fn(def_id)),
417+
ForeignItemStatic(_, m) => Some(Def::Static(def_id, m)),
418+
ForeignItemType => Some(Def::TyForeign(def_id)),
419+
}
420+
}
421+
NodeTraitItem(item) => {
422+
let def_id = self.local_def_id(item.id);
423+
match item.node {
424+
TraitItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
425+
TraitItemKind::Method(..) => Some(Def::Method(def_id)),
426+
TraitItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
427+
}
428+
}
429+
NodeImplItem(item) => {
430+
let def_id = self.local_def_id(item.id);
431+
match item.node {
432+
ImplItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
433+
ImplItemKind::Method(..) => Some(Def::Method(def_id)),
434+
ImplItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
435+
}
436+
}
437+
NodeVariant(variant) => {
438+
let def_id = self.local_def_id(variant.node.data.id());
439+
Some(Def::Variant(def_id))
440+
}
441+
NodeField(_) |
442+
NodeExpr(_) |
443+
NodeStmt(_) |
444+
NodeTy(_) |
445+
NodeTraitRef(_) |
446+
NodePat(_) |
447+
NodeBinding(_) |
448+
NodeStructCtor(_) |
449+
NodeLifetime(_) |
450+
NodeVisibility(_) |
451+
NodeBlock(_) => None,
452+
NodeLocal(local) => {
453+
Some(Def::Local(local.id))
454+
}
455+
NodeMacroDef(macro_def) => {
456+
Some(Def::Macro(self.local_def_id(macro_def.id),
457+
MacroKind::Bang))
458+
}
459+
NodeTyParam(param) => {
460+
Some(Def::TyParam(self.local_def_id(param.id)))
461+
}
462+
}
463+
}
464+
376465
fn entry_count(&self) -> usize {
377466
self.map.len()
378467
}
@@ -1275,3 +1364,12 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
12751364
}
12761365
}
12771366
}
1367+
1368+
pub fn describe_def(tcx: TyCtxt, def_id: DefId) -> Option<Def> {
1369+
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
1370+
tcx.hir.describe_def(node_id)
1371+
} else {
1372+
bug!("Calling local describe_def query provider for upstream DefId: {:?}",
1373+
def_id)
1374+
}
1375+
}

src/librustc/hir/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use syntax::tokenstream::TokenStream;
4343
use syntax::util::ThinVec;
4444
use syntax::util::parser::ExprPrecedence;
4545
use ty::AdtKind;
46+
use ty::maps::Providers;
4647

4748
use rustc_data_structures::indexed_vec;
4849

@@ -2204,3 +2205,8 @@ pub type TraitMap = NodeMap<Vec<TraitCandidate>>;
22042205
// Map from the NodeId of a glob import to a list of items which are actually
22052206
// imported.
22062207
pub type GlobMap = NodeMap<FxHashSet<Name>>;
2208+
2209+
2210+
pub fn provide(providers: &mut Providers) {
2211+
providers.describe_def = map::describe_def;
2212+
}

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/ty/maps/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ define_maps! { <'tcx>
350350
[] fn export_name: ExportName(DefId) -> Option<Symbol>,
351351
[] fn contains_extern_indicator: ContainsExternIndicator(DefId) -> bool,
352352
[] fn symbol_export_level: GetSymbolExportLevel(DefId) -> SymbolExportLevel,
353-
[] fn is_translated_function: IsTranslatedFunction(DefId) -> bool,
353+
[] fn is_translated_item: IsTranslatedItem(DefId) -> bool,
354354
[] fn codegen_unit: CodegenUnit(InternedString) -> Arc<CodegenUnit<'tcx>>,
355355
[] fn compile_codegen_unit: CompileCodegenUnit(InternedString) -> Stats,
356356
[] fn output_filenames: output_filenames_node(CrateNum)

src/librustc/ty/maps/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
929929
DepKind::ContainsExternIndicator => {
930930
force!(contains_extern_indicator, def_id!());
931931
}
932-
DepKind::IsTranslatedFunction => { force!(is_translated_function, def_id!()); }
932+
DepKind::IsTranslatedItem => { force!(is_translated_item, def_id!()); }
933933
DepKind::OutputFilenames => { force!(output_filenames, LOCAL_CRATE); }
934934

935935
DepKind::TargetFeaturesWhitelist => { force!(target_features_whitelist, LOCAL_CRATE); }

src/librustc_driver/driver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
932932
}
933933

934934
pub fn default_provide(providers: &mut ty::maps::Providers) {
935+
hir::provide(providers);
935936
borrowck::provide(providers);
936937
mir::provide(providers);
937938
reachable::provide(providers);

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/base.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ fn collect_and_partition_translation_items<'a, 'tcx>(
10041004
let translation_items: DefIdSet = items.iter().filter_map(|trans_item| {
10051005
match *trans_item {
10061006
MonoItem::Fn(ref instance) => Some(instance.def_id()),
1007+
MonoItem::Static(def_id) => Some(def_id),
10071008
_ => None,
10081009
}
10091010
}).collect();
@@ -1107,7 +1108,7 @@ impl CrateInfo {
11071108
}
11081109
}
11091110

1110-
fn is_translated_function(tcx: TyCtxt, id: DefId) -> bool {
1111+
fn is_translated_item(tcx: TyCtxt, id: DefId) -> bool {
11111112
let (all_trans_items, _) =
11121113
tcx.collect_and_partition_translation_items(LOCAL_CRATE);
11131114
all_trans_items.contains(&id)
@@ -1222,7 +1223,7 @@ pub fn provide(providers: &mut Providers) {
12221223
providers.collect_and_partition_translation_items =
12231224
collect_and_partition_translation_items;
12241225

1225-
providers.is_translated_function = is_translated_function;
1226+
providers.is_translated_item = is_translated_item;
12261227

12271228
providers.codegen_unit = |tcx, name| {
12281229
let (_, all) = tcx.collect_and_partition_translation_items(LOCAL_CRATE);

src/librustc_trans/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub fn get_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
149149
unsafe {
150150
llvm::LLVMRustSetLinkage(llfn, llvm::Linkage::ExternalLinkage);
151151

152-
if cx.tcx.is_translated_function(instance_def_id) {
152+
if cx.tcx.is_translated_item(instance_def_id) {
153153
if instance_def_id.is_local() {
154154
if !cx.tcx.is_exported_symbol(instance_def_id) {
155155
llvm::LLVMRustSetVisibility(llfn, llvm::Visibility::Hidden);

0 commit comments

Comments
 (0)