Skip to content

Allow for instantiating statics from upstream crates #48353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implement describe_def query for LOCAL_CRATE
  • Loading branch information
michaelwoerister committed Feb 19, 2018
commit 8ff633cc3b85933e76c84b43d2c19250c397e9cb
98 changes: 98 additions & 0 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use hir::def_id::{CRATE_DEF_INDEX, DefId, LocalDefId, DefIndexAddressSpace};
use syntax::abi::Abi;
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
use syntax::codemap::Spanned;
use syntax::ext::base::MacroKind;
use syntax_pos::Span;

use hir::*;
Expand All @@ -32,13 +33,15 @@ use util::nodemap::{DefIdMap, FxHashMap};
use arena::TypedArena;
use std::cell::RefCell;
use std::io;
use ty::TyCtxt;

pub mod blocks;
mod collector;
mod def_collector;
pub mod definitions;
mod hir_id_validator;


pub const ITEM_LIKE_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::Low;
pub const REGULAR_SPACE: DefIndexAddressSpace = DefIndexAddressSpace::High;

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

pub fn describe_def(&self, node_id: NodeId) -> Option<Def> {
let node = if let Some(node) = self.find(node_id) {
node
} else {
return None
};

match node {
NodeItem(item) => {
let def_id = || {
self.local_def_id(item.id)
};

match item.node {
ItemStatic(_, m, _) => Some(Def::Static(def_id(),
m == MutMutable)),
ItemConst(..) => Some(Def::Const(def_id())),
ItemFn(..) => Some(Def::Fn(def_id())),
ItemMod(..) => Some(Def::Mod(def_id())),
ItemGlobalAsm(..) => Some(Def::GlobalAsm(def_id())),
ItemTy(..) => Some(Def::TyAlias(def_id())),
ItemEnum(..) => Some(Def::Enum(def_id())),
ItemStruct(..) => Some(Def::Struct(def_id())),
ItemUnion(..) => Some(Def::Union(def_id())),
ItemTrait(..) => Some(Def::Trait(def_id())),
ItemTraitAlias(..) => {
bug!("trait aliases are not yet implemented (see issue #41517)")
},
ItemExternCrate(_) |
ItemUse(..) |
ItemForeignMod(..) |
ItemImpl(..) => None,
}
}
NodeForeignItem(item) => {
let def_id = self.local_def_id(item.id);
match item.node {
ForeignItemFn(..) => Some(Def::Fn(def_id)),
ForeignItemStatic(_, m) => Some(Def::Static(def_id, m)),
ForeignItemType => Some(Def::TyForeign(def_id)),
}
}
NodeTraitItem(item) => {
let def_id = self.local_def_id(item.id);
match item.node {
TraitItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
TraitItemKind::Method(..) => Some(Def::Method(def_id)),
TraitItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
}
}
NodeImplItem(item) => {
let def_id = self.local_def_id(item.id);
match item.node {
ImplItemKind::Const(..) => Some(Def::AssociatedConst(def_id)),
ImplItemKind::Method(..) => Some(Def::Method(def_id)),
ImplItemKind::Type(..) => Some(Def::AssociatedTy(def_id)),
}
}
NodeVariant(variant) => {
let def_id = self.local_def_id(variant.node.data.id());
Some(Def::Variant(def_id))
}
NodeField(_) |
NodeExpr(_) |
NodeStmt(_) |
NodeTy(_) |
NodeTraitRef(_) |
NodePat(_) |
NodeBinding(_) |
NodeStructCtor(_) |
NodeLifetime(_) |
NodeVisibility(_) |
NodeBlock(_) => None,
NodeLocal(local) => {
Some(Def::Local(local.id))
}
NodeMacroDef(macro_def) => {
Some(Def::Macro(self.local_def_id(macro_def.id),
MacroKind::Bang))
}
NodeTyParam(param) => {
Some(Def::TyParam(self.local_def_id(param.id)))
}
}
}

fn entry_count(&self) -> usize {
self.map.len()
}
Expand Down Expand Up @@ -1275,3 +1364,12 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
}
}
}

pub fn describe_def(tcx: TyCtxt, def_id: DefId) -> Option<Def> {
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
tcx.hir.describe_def(node_id)
} else {
bug!("Calling local describe_def query provider for upstream DefId: {:?}",
def_id)
}
}
6 changes: 6 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use syntax::tokenstream::TokenStream;
use syntax::util::ThinVec;
use syntax::util::parser::ExprPrecedence;
use ty::AdtKind;
use ty::maps::Providers;

use rustc_data_structures::indexed_vec;

Expand Down Expand Up @@ -2204,3 +2205,8 @@ pub type TraitMap = NodeMap<Vec<TraitCandidate>>;
// Map from the NodeId of a glob import to a list of items which are actually
// imported.
pub type GlobMap = NodeMap<FxHashSet<Name>>;


pub fn provide(providers: &mut Providers) {
providers.describe_def = map::describe_def;
}
1 change: 1 addition & 0 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ pub fn phase_2_configure_and_expand_inner<'a, F>(sess: &'a Session,
}

pub fn default_provide(providers: &mut ty::maps::Providers) {
hir::provide(providers);
borrowck::provide(providers);
mir::provide(providers);
reachable::provide(providers);
Expand Down