Skip to content
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

Add all remaining DefKinds. #70043

Merged
merged 7 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 38 additions & 1 deletion src/librustc_hir/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ 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,
Generator,
}

impl DefKind {
Expand Down Expand Up @@ -113,6 +125,16 @@ 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 => "constant expression",
DefKind::Field => "field",
DefKind::Impl => "implementation",
DefKind::Closure => "closure",
DefKind::Generator => "generator",
DefKind::ExternCrate => "extern crate",
DefKind::GlobalAsm => "global assembly block",
}
}

Expand All @@ -124,7 +146,10 @@ impl DefKind {
| DefKind::AssocOpaqueTy
| DefKind::AssocFn
| DefKind::Enum
| DefKind::OpaqueTy => "an",
| DefKind::OpaqueTy
| DefKind::Impl
| DefKind::Use
| DefKind::ExternCrate => "an",
DefKind::Macro(macro_kind) => macro_kind.article(),
_ => "a",
}
Expand Down Expand Up @@ -155,6 +180,18 @@ 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::Generator
| DefKind::Use
| DefKind::ForeignMod
| DefKind::GlobalAsm
| DefKind::Impl => false,
}
}
}
Expand Down
31 changes: 0 additions & 31 deletions src/librustc_hir/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2452,27 +2452,6 @@ pub enum ItemKind<'hir> {
}

impl ItemKind<'_> {
pub fn descr(&self) -> &str {
match *self {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "`use` import",
ItemKind::Static(..) => "static item",
ItemKind::Const(..) => "constant item",
ItemKind::Fn(..) => "function",
ItemKind::Mod(..) => "module",
ItemKind::ForeignMod(..) => "extern block",
ItemKind::GlobalAsm(..) => "global asm item",
ItemKind::TyAlias(..) => "type alias",
ItemKind::OpaqueTy(..) => "opaque type",
ItemKind::Enum(..) => "enum",
ItemKind::Struct(..) => "struct",
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Impl { .. } => "implementation",
}
}

pub fn generics(&self) -> Option<&Generics<'_>> {
Some(match *self {
ItemKind::Fn(_, ref generics, _)
Expand Down Expand Up @@ -2551,16 +2530,6 @@ pub enum ForeignItemKind<'hir> {
Type,
}

impl ForeignItemKind<'hir> {
pub fn descriptive_variant(&self) -> &str {
match *self {
ForeignItemKind::Fn(..) => "foreign function",
ForeignItemKind::Static(..) => "foreign static item",
ForeignItemKind::Type => "foreign type",
}
}
}

/// A variable captured by a closure.
#[derive(Debug, Copy, Clone, RustcEncodable, RustcDecodable, HashStable_Generic)]
pub struct Upvar {
Expand Down
7 changes: 1 addition & 6 deletions src/librustc_infer/infer/error_reporting/need_type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
.get_opt_name()
.map(|parent_symbol| parent_symbol.to_string());

let type_parent_desc = self
.tcx
.def_kind(parent_def_id)
.map(|parent_def_kind| parent_def_kind.descr(parent_def_id));

(parent_name, type_parent_desc)
(parent_name, Some(self.tcx.def_kind(parent_def_id).descr(parent_def_id)))
} else {
(None, None)
};
Expand Down
56 changes: 26 additions & 30 deletions src/librustc_metadata/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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::Generator,
}
}
}

Expand Down Expand Up @@ -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)))
}
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
55 changes: 31 additions & 24 deletions src/librustc_middle/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -227,10 +227,14 @@ 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> {
let node = self.find(hir_id)?;
pub fn def_kind(&self, local_def_id: LocalDefId) -> DefKind {
// FIXME(eddyb) support `find` on the crate root.
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
return DefKind::Mod;
}

Some(match node {
let hir_id = self.local_def_id_to_hir_id(local_def_id);
match self.get(hir_id) {
Node::Item(item) => match item.kind {
ItemKind::Static(..) => DefKind::Static,
ItemKind::Const(..) => DefKind::Const,
Expand All @@ -243,11 +247,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,
Expand All @@ -268,7 +272,7 @@ impl<'hir> Map<'hir> {
Node::Variant(_) => DefKind::Variant,
Node::Ctor(variant_data) => {
// FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
variant_data.ctor_hir_id()?;
assert_ne!(variant_data.ctor_hir_id(), None);

let ctor_of = match self.find(self.get_parent_node(hir_id)) {
Some(Node::Item(..)) => def::CtorOf::Struct,
Expand All @@ -277,10 +281,20 @@ 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(.., None) => DefKind::Closure,
ExprKind::Closure(.., Some(_)) => DefKind::Generator,
_ => 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(_)
Expand All @@ -292,14 +306,8 @@ 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)),
}
}

fn find_entry(&self, id: HirId) -> Option<Entry<'hir>> {
Expand Down Expand Up @@ -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());
}
2 changes: 1 addition & 1 deletion src/librustc_middle/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub enum EvalResult {
fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, mut def_id: DefId) -> bool {
// Check if `def_id` is a trait method.
match tcx.def_kind(def_id) {
Some(DefKind::AssocFn) | Some(DefKind::AssocTy) | Some(DefKind::AssocConst) => {
DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
if let ty::TraitContainer(trait_def_id) = tcx.associated_item(def_id).container {
// Trait methods do not declare visibility (even
// for visibility info in cstore). Use containing
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ rustc_queries! {
cache_on_disk_if { true }
}

query def_kind(_: DefId) -> Option<DefKind> {}
query def_kind(_: DefId) -> DefKind {}
query def_span(_: DefId) -> Span {
// FIXME(mw): DefSpans are not really inputs since they are derived from
// HIR. But at the moment HIR hashing still contains some hacks that allow
Expand Down
24 changes: 8 additions & 16 deletions src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use rustc_errors::ErrorReported;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::definitions::{DefPathData, DefPathHash, Definitions};
use rustc_hir::definitions::{DefPathHash, Definitions};
use rustc_hir::lang_items;
use rustc_hir::lang_items::PanicLocationLangItem;
use rustc_hir::{HirId, Node, TraitCandidate};
Expand Down Expand Up @@ -1492,21 +1492,13 @@ impl<'tcx> TyCtxt<'tcx> {

/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "struct")`).
pub fn article_and_description(&self, def_id: DefId) -> (&'static str, &'static str) {
self.def_kind(def_id)
.map(|def_kind| (def_kind.article(), def_kind.descr(def_id)))
.unwrap_or_else(|| match self.def_key(def_id).disambiguated_data.data {
DefPathData::ClosureExpr => match self.generator_kind(def_id) {
None => ("a", "closure"),
Some(rustc_hir::GeneratorKind::Async(..)) => ("an", "async closure"),
Some(rustc_hir::GeneratorKind::Gen) => ("a", "generator"),
},
DefPathData::LifetimeNs(..) => ("a", "lifetime"),
DefPathData::Impl => ("an", "implementation"),
DefPathData::TypeNs(..) | DefPathData::ValueNs(..) | DefPathData::MacroNs(..) => {
unreachable!()
}
_ => bug!("article_and_description called on def_id {:?}", def_id),
})
match self.def_kind(def_id) {
DefKind::Generator => match self.generator_kind(def_id).unwrap() {
rustc_hir::GeneratorKind::Async(..) => ("an", "async closure"),
rustc_hir::GeneratorKind::Gen => ("a", "generator"),
},
def_kind => (def_kind.article(), def_kind.descr(def_id)),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2680,7 +2680,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
} else {
match self.def_kind(def_id) {
Some(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy) => true,
DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy => true,
_ => false,
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ pub trait PrettyPrinter<'tcx>:
p!(write("::{:?}", promoted));
} else {
match self.tcx().def_kind(did) {
Some(DefKind::Static | DefKind::Const | DefKind::AssocConst) => {
DefKind::Static | DefKind::Const | DefKind::AssocConst => {
p!(print_value_path(did, substs))
}
_ => {
Expand Down
Loading