add a few more DefKinds

make Map::def_kind take LocalDefId

Co-Authored-By: Vadim Petrochenkov <vadim.petrochenkov@gmail.com>

crates are DefKind::Mod
This commit is contained in:
mark 2020-03-16 10:01:03 -05:00
parent 0612568358
commit cff5b998e0
10 changed files with 133 additions and 64 deletions

View File

@ -77,6 +77,17 @@ pub enum DefKind {
// Macro namespace
Macro(MacroKind),
// Not namespaced (or they are, but we don't treat them so)
ExternCrate,
Use,
ForeignMod,
AnonConst,
Field,
LifetimeParam,
GlobalAsm,
Impl,
Closure,
}
impl DefKind {
@ -113,6 +124,15 @@ impl DefKind {
DefKind::TyParam => "type parameter",
DefKind::ConstParam => "const parameter",
DefKind::Macro(macro_kind) => macro_kind.descr(),
DefKind::LifetimeParam => "lifetime parameter",
DefKind::Use => "import",
DefKind::ForeignMod => "foreign module",
DefKind::AnonConst => "anonymous constant",
DefKind::Field => "field",
DefKind::Impl => "implementation",
DefKind::Closure => "closure",
DefKind::ExternCrate => "extern crate",
DefKind::GlobalAsm => "global assembly block",
}
}
@ -124,7 +144,9 @@ impl DefKind {
| DefKind::AssocOpaqueTy
| DefKind::AssocFn
| DefKind::Enum
| DefKind::OpaqueTy => "an",
| DefKind::OpaqueTy
| DefKind::AnonConst
| DefKind::Impl => "an",
DefKind::Macro(macro_kind) => macro_kind.article(),
_ => "a",
}
@ -155,6 +177,17 @@ impl DefKind {
| DefKind::AssocConst => ns == Namespace::ValueNS,
DefKind::Macro(..) => ns == Namespace::MacroNS,
// Not namespaced.
DefKind::AnonConst
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::ExternCrate
| DefKind::Closure
| DefKind::Use
| DefKind::ForeignMod
| DefKind::GlobalAsm
| DefKind::Impl => false,
}
}
}

View File

@ -562,8 +562,8 @@ impl MetadataBlob {
}
impl EntryKind {
fn def_kind(&self) -> Option<DefKind> {
Some(match *self {
fn def_kind(&self) -> DefKind {
match *self {
EntryKind::Const(..) => DefKind::Const,
EntryKind::AssocConst(..) => DefKind::AssocConst,
EntryKind::ImmStatic
@ -587,14 +587,13 @@ impl EntryKind {
EntryKind::Enum(..) => DefKind::Enum,
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
EntryKind::ForeignType => DefKind::ForeignTy,
EntryKind::ForeignMod
| EntryKind::GlobalAsm
| EntryKind::Impl(_)
| EntryKind::Field
| EntryKind::Generator(_)
| EntryKind::Closure => return None,
})
EntryKind::Impl(_) => DefKind::Impl,
EntryKind::Closure => DefKind::Closure,
EntryKind::ForeignMod => DefKind::ForeignMod,
EntryKind::GlobalAsm => DefKind::GlobalAsm,
EntryKind::Field => DefKind::Field,
EntryKind::Generator(_) => DefKind::Closure,
}
}
}
@ -679,11 +678,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}
}
fn def_kind(&self, index: DefIndex) -> Option<DefKind> {
fn def_kind(&self, index: DefIndex) -> DefKind {
if !self.is_proc_macro(index) {
self.kind(index).def_kind()
} else {
Some(DefKind::Macro(macro_kind(self.raw_proc_macro(index))))
DefKind::Macro(macro_kind(self.raw_proc_macro(index)))
}
}
@ -1009,20 +1008,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.get(self, child_index)
.unwrap_or(Lazy::empty());
for child_index in child_children.decode((self, sess)) {
if let Some(kind) = self.def_kind(child_index) {
callback(Export {
res: Res::Def(kind, self.local_def_id(child_index)),
ident: self.item_ident(child_index, sess),
vis: self.get_visibility(child_index),
span: self
.root
.tables
.span
.get(self, child_index)
.unwrap()
.decode((self, sess)),
});
}
let kind = self.def_kind(child_index);
callback(Export {
res: Res::Def(kind, self.local_def_id(child_index)),
ident: self.item_ident(child_index, sess),
vis: self.get_visibility(child_index),
span: self
.root
.tables
.span
.get(self, child_index)
.unwrap()
.decode((self, sess)),
});
}
continue;
}
@ -1033,10 +1031,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let def_key = self.def_key(child_index);
let span = self.get_span(child_index, sess);
if let (Some(kind), true) = (
self.def_kind(child_index),
def_key.disambiguated_data.data.get_opt_name().is_some(),
) {
if def_key.disambiguated_data.data.get_opt_name().is_some() {
let kind = self.def_kind(child_index);
let ident = self.item_ident(child_index, sess);
let vis = self.get_visibility(child_index);
let def_id = self.local_def_id(child_index);

View File

@ -127,7 +127,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
static_mutability => { cdata.static_mutability(def_id.index) }
generator_kind => { cdata.generator_kind(def_id.index) }
def_kind => { cdata.def_kind(def_id.index) }
def_kind => { Some(cdata.def_kind(def_id.index)) }
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
lookup_stability => {
cdata.get_stability(def_id.index).map(|s| tcx.intern_stability(s))

View File

@ -6,7 +6,7 @@ use crate::ty::TyCtxt;
use rustc_ast::ast::{self, Name, NodeId};
use rustc_data_structures::svh::Svh;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, Definitions};
use rustc_hir::intravisit;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@ -227,7 +227,12 @@ impl<'hir> Map<'hir> {
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
}
pub fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
pub fn def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
return Some(DefKind::Mod);
}
let hir_id = self.local_def_id_to_hir_id(local_def_id);
let node = self.find(hir_id)?;
Some(match node {
@ -243,11 +248,11 @@ impl<'hir> Map<'hir> {
ItemKind::Union(..) => DefKind::Union,
ItemKind::Trait(..) => DefKind::Trait,
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
ItemKind::ExternCrate(_)
| ItemKind::Use(..)
| ItemKind::ForeignMod(..)
| ItemKind::GlobalAsm(..)
| ItemKind::Impl { .. } => return None,
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
ItemKind::Use(..) => DefKind::Use,
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
ItemKind::Impl { .. } => DefKind::Impl,
},
Node::ForeignItem(item) => match item.kind {
ForeignItemKind::Fn(..) => DefKind::Fn,
@ -277,10 +282,19 @@ impl<'hir> Map<'hir> {
};
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
}
Node::AnonConst(_)
| Node::Field(_)
| Node::Expr(_)
| Node::Stmt(_)
Node::AnonConst(_) => DefKind::AnonConst,
Node::Field(_) => DefKind::Field,
Node::Expr(expr) => match expr.kind {
ExprKind::Closure { .. } => DefKind::Closure,
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
},
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
Node::GenericParam(param) => match param.kind {
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
GenericParamKind::Type { .. } => DefKind::TyParam,
GenericParamKind::Const { .. } => DefKind::ConstParam,
},
Node::Stmt(_)
| Node::PathSegment(_)
| Node::Ty(_)
| Node::TraitRef(_)
@ -292,13 +306,7 @@ impl<'hir> Map<'hir> {
| Node::Lifetime(_)
| Node::Visibility(_)
| Node::Block(_)
| Node::Crate(_) => return None,
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
Node::GenericParam(param) => match param.kind {
GenericParamKind::Lifetime { .. } => return None,
GenericParamKind::Type { .. } => DefKind::TyParam,
GenericParamKind::Const { .. } => DefKind::ConstParam,
},
| Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
})
}
@ -1082,6 +1090,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
}
pub fn provide(providers: &mut Providers<'_>) {
providers.def_kind =
|tcx, def_id| tcx.hir().def_kind(tcx.hir().as_local_hir_id(def_id.expect_local()));
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local());
}

View File

@ -817,7 +817,7 @@ fn write_mir_sig(
write!(w, "static {}", if tcx.is_mutable_static(src.def_id()) { "mut " } else { "" })?
}
(_, _) if is_function => write!(w, "fn ")?,
(None, _) => {} // things like anon const, not an item
(Some(DefKind::AnonConst), _) | (None, _) => {} // things like anon const, not an item
_ => bug!("Unexpected def kind {:?}", kind),
}

View File

@ -1,6 +1,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(in_band_lifetimes)]
#![feature(nll)]
#![feature(or_patterns)]
#![recursion_limit = "256"]
use rustc_ast::ast::Ident;
@ -612,8 +613,8 @@ impl EmbargoVisitor<'tcx> {
}
}
// These have type privacy, so are not reachable unless they're
// public
// These have type privacy or are not namespaced, so are not reachable unless they're
// public.
DefKind::AssocConst
| DefKind::AssocTy
| DefKind::AssocOpaqueTy
@ -626,7 +627,16 @@ impl EmbargoVisitor<'tcx> {
| DefKind::AssocFn
| DefKind::Trait
| DefKind::TyParam
| DefKind::Variant => (),
| DefKind::Variant
| DefKind::LifetimeParam
| DefKind::ExternCrate
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::Field
| DefKind::GlobalAsm
| DefKind::Impl
| DefKind::Closure => (),
}
}

View File

@ -906,7 +906,20 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion))
}
Res::Def(DefKind::TyParam | DefKind::ConstParam, _)
Res::Def(
DefKind::TyParam
| DefKind::ConstParam
| DefKind::ExternCrate
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::GlobalAsm
| DefKind::Closure
| DefKind::Impl,
_,
)
| Res::Local(..)
| Res::SelfTy(..)
| Res::SelfCtor(..)

View File

@ -2502,10 +2502,8 @@ impl<'a> Resolver<'a> {
}
let container = match parent.kind {
ModuleKind::Def(DefKind::Mod, _, _) => "module",
ModuleKind::Def(DefKind::Trait, _, _) => "trait",
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id().unwrap()),
ModuleKind::Block(..) => "block",
_ => "enum",
};
let old_noun = match old_binding.is_import() {

View File

@ -760,9 +760,22 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
Res::Def(HirDefKind::Mod, def_id) => {
Some(Ref { kind: RefKind::Mod, span, ref_id: id_from_def_id(def_id) })
}
Res::PrimTy(..)
Res::Def(
HirDefKind::Macro(..)
| HirDefKind::ExternCrate
| HirDefKind::ForeignMod
| HirDefKind::LifetimeParam
| HirDefKind::AnonConst
| HirDefKind::Use
| HirDefKind::Field
| HirDefKind::GlobalAsm
| HirDefKind::Impl
| HirDefKind::Closure,
_,
)
| Res::PrimTy(..)
| Res::SelfTy(..)
| Res::Def(HirDefKind::Macro(..), _)
| Res::ToolMod
| Res::NonMacroAttr(..)
| Res::SelfCtor(..)

View File

@ -4972,8 +4972,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sugg_call = fields.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
match def_id
.as_local()
.map(|def_id| hir.as_local_hir_id(def_id))
.and_then(|hir_id| hir.def_kind(hir_id))
.and_then(|def_id| hir.def_kind(def_id))
{
Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, _)) => {
msg = "instantiate this tuple variant";