Skip to content
Permalink
Browse files

rustc: use DefKind instead of Def, where possible.

  • Loading branch information...
eddyb committed Apr 20, 2019
1 parent a3fcab3 commit b92b1a76e175f396d7986177d0a2d5907bbba888
@@ -80,6 +80,53 @@ pub enum DefKind {
Macro(MacroKind),
}

impl DefKind {
pub fn descr(self) -> &'static str {
match self {
DefKind::Fn => "function",
DefKind::Mod => "module",
DefKind::Static => "static",
DefKind::Enum => "enum",
DefKind::Variant => "variant",
DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
DefKind::Struct => "struct",
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) =>
bug!("impossible struct constructor"),
DefKind::Existential => "existential type",
DefKind::TyAlias => "type alias",
DefKind::TraitAlias => "trait alias",
DefKind::AssociatedTy => "associated type",
DefKind::AssociatedExistential => "associated existential type",
DefKind::Union => "union",
DefKind::Trait => "trait",
DefKind::ForeignTy => "foreign type",
DefKind::Method => "method",
DefKind::Const => "constant",
DefKind::AssociatedConst => "associated constant",
DefKind::TyParam => "type parameter",
DefKind::ConstParam => "const parameter",
DefKind::Macro(macro_kind) => macro_kind.descr(),
}
}

/// An English article for the def.
pub fn article(&self) -> &'static str {
match *self {
DefKind::AssociatedTy
| DefKind::AssociatedConst
| DefKind::AssociatedExistential
| DefKind::Enum
| DefKind::Existential => "an",
DefKind::Macro(macro_kind) => macro_kind.article(),
_ => "a",
}
}
}

#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
pub enum Def<Id = hir::HirId> {
Def(DefKind, DefId),
@@ -328,39 +375,13 @@ impl<Id> Def<Id> {
/// A human readable name for the def kind ("function", "module", etc.).
pub fn kind_name(&self) -> &'static str {
match *self {
Def::Def(DefKind::Fn, _) => "function",
Def::Def(DefKind::Mod, _) => "module",
Def::Def(DefKind::Static, _) => "static",
Def::Def(DefKind::Enum, _) => "enum",
Def::Def(DefKind::Variant, _) => "variant",
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), _) => "tuple variant",
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), _) => "unit variant",
Def::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive), _) => "struct variant",
Def::Def(DefKind::Struct, _) => "struct",
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fn), _) => "tuple struct",
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Const), _) => "unit struct",
Def::Def(DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive), _) =>
bug!("impossible struct constructor"),
Def::Def(DefKind::Existential, _) => "existential type",
Def::Def(DefKind::TyAlias, _) => "type alias",
Def::Def(DefKind::TraitAlias, _) => "trait alias",
Def::Def(DefKind::AssociatedTy, _) => "associated type",
Def::Def(DefKind::AssociatedExistential, _) => "associated existential type",
Def::Def(kind, _) => kind.descr(),
Def::SelfCtor(..) => "self constructor",
Def::Def(DefKind::Union, _) => "union",
Def::Def(DefKind::Trait, _) => "trait",
Def::Def(DefKind::ForeignTy, _) => "foreign type",
Def::Def(DefKind::Method, _) => "method",
Def::Def(DefKind::Const, _) => "constant",
Def::Def(DefKind::AssociatedConst, _) => "associated constant",
Def::Def(DefKind::TyParam, _) => "type parameter",
Def::Def(DefKind::ConstParam, _) => "const parameter",
Def::PrimTy(..) => "builtin type",
Def::Local(..) => "local variable",
Def::Upvar(..) => "closure capture",
Def::Label(..) => "label",
Def::SelfTy(..) => "self type",
Def::Def(DefKind::Macro(macro_kind), _) => macro_kind.descr(),
Def::ToolMod => "tool module",
Def::NonMacroAttr(attr_kind) => attr_kind.descr(),
Def::Err => "unresolved item",
@@ -370,13 +391,8 @@ impl<Id> Def<Id> {
/// An English article for the def.
pub fn article(&self) -> &'static str {
match *self {
Def::Def(DefKind::AssociatedTy, _)
| Def::Def(DefKind::AssociatedConst, _)
| Def::Def(DefKind::AssociatedExistential, _)
| Def::Def(DefKind::Enum, _)
| Def::Def(DefKind::Existential, _)
| Def::Err => "an",
Def::Def(DefKind::Macro(macro_kind), _) => macro_kind.article(),
Def::Def(kind, _) => kind.article(),
Def::Err => "an",
_ => "a",
}
}
@@ -17,7 +17,7 @@ use syntax::ext::base::MacroKind;
use syntax_pos::{Span, DUMMY_SP};

use crate::hir::*;
use crate::hir::{Def, DefKind};
use crate::hir::DefKind;
use crate::hir::itemlikevisit::ItemLikeVisitor;
use crate::hir::print::Nested;
use crate::util::nodemap::FxHashMap;
@@ -310,14 +310,14 @@ 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> {
fn def_kind(&self, node_id: NodeId) -> Option<DefKind> {
let node = if let Some(node) = self.find(node_id) {
node
} else {
return None
};

let kind = match node {
Some(match node {
Node::Item(item) => {
match item.node {
ItemKind::Static(..) => DefKind::Static,
@@ -382,15 +382,11 @@ impl<'hir> Map<'hir> {
Node::TraitRef(_) |
Node::Pat(_) |
Node::Binding(_) |
Node::Local(_) |
Node::Lifetime(_) |
Node::Visibility(_) |
Node::Block(_) |
Node::Crate => return None,
// FIXME(eddyb) this is the only non-`DefKind` case here,
// investigate whether it's actually used, and ideally remove it.
Node::Local(local) => {
return Some(Def::Local(local.hir_id));
}
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
Node::GenericParam(param) => {
match param.kind {
@@ -399,14 +395,7 @@ impl<'hir> Map<'hir> {
GenericParamKind::Const { .. } => DefKind::ConstParam,
}
}
};
Some(Def::Def(kind, self.local_def_id(node_id)))
}

// FIXME(@ljedrz): replace the NodeId variant
pub fn describe_def_by_hir_id(&self, hir_id: HirId) -> Option<Def> {
let node_id = self.hir_to_node_id(hir_id);
self.describe_def(node_id)
})
}

fn entry_count(&self) -> usize {
@@ -1464,11 +1453,11 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
node_id_to_string(map, node_id, include_id)
}

pub fn describe_def(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<Def> {
pub fn def_kind(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<DefKind> {
if let Some(node_id) = tcx.hir().as_local_node_id(def_id) {
tcx.hir().describe_def(node_id)
tcx.hir().def_kind(node_id)
} else {
bug!("Calling local describe_def query provider for upstream DefId: {:?}",
bug!("Calling local def_kind query provider for upstream DefId: {:?}",
def_id)
}
}
@@ -2521,7 +2521,7 @@ pub type GlobMap = NodeMap<FxHashSet<Name>>;

pub fn provide(providers: &mut Providers<'_>) {
check_attr::provide(providers);
providers.describe_def = map::describe_def;
providers.def_kind = map::def_kind;
}

#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
@@ -98,6 +98,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReachableContext<'a, 'tcx> {
}
hir::ExprKind::MethodCall(..) => {
self.tables.type_dependent_def(expr.hir_id)
.map(|(kind, def_id)| Def::Def(kind, def_id))
}
_ => None
};
@@ -525,10 +525,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
// See issue #38412.
fn skip_stability_check_due_to_privacy(self, mut def_id: DefId) -> bool {
// Check if `def_id` is a trait method.
match self.describe_def(def_id) {
Some(Def::Def(DefKind::Method, _)) |
Some(Def::Def(DefKind::AssociatedTy, _)) |
Some(Def::Def(DefKind::AssociatedConst, _)) => {
match self.def_kind(def_id) {
Some(DefKind::Method) |
Some(DefKind::AssociatedTy) |
Some(DefKind::AssociatedConst) => {
if let ty::TraitContainer(trait_def_id) = self.associated_item(def_id).container {
// Trait methods do not declare visibility (even
// for visibility info in cstore). Use containing
@@ -469,7 +469,7 @@ rustc_queries! {
cache { true }
}

query describe_def(_: DefId) -> Option<Def> {}
query def_kind(_: DefId) -> Option<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
@@ -44,6 +44,7 @@ use crate::ty::steal::Steal;
use crate::ty::subst::{UserSubsts, UnpackedKind};
use crate::ty::{BoundVar, BindingMode};
use crate::ty::CanonicalPolyFnSig;
use crate::util::common::ErrorReported;
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap, ItemLocalSet};
use crate::util::nodemap::{FxHashMap, FxHashSet};
use errors::DiagnosticBuilder;
@@ -347,7 +348,7 @@ pub struct TypeckTables<'tcx> {

/// Resolved definitions for `<T>::X` associated paths and
/// method calls, including those of overloaded operators.
type_dependent_defs: ItemLocalMap<Def>,
type_dependent_defs: ItemLocalMap<Result<(DefKind, DefId), ErrorReported>>,

/// Resolved field indices for field accesses in expressions (`S { field }`, `obj.field`)
/// or patterns (`S { field }`). The index is often useful by itself, but to learn more
@@ -481,30 +482,32 @@ impl<'tcx> TypeckTables<'tcx> {
pub fn qpath_def(&self, qpath: &hir::QPath, id: hir::HirId) -> Def {
match *qpath {
hir::QPath::Resolved(_, ref path) => path.def,
hir::QPath::TypeRelative(..) => {
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
self.type_dependent_defs.get(&id.local_id).cloned().unwrap_or(Def::Err)
}
hir::QPath::TypeRelative(..) => self.type_dependent_def(id)
.map_or(Def::Err, |(kind, def_id)| Def::Def(kind, def_id)),
}
}

pub fn type_dependent_defs(&self) -> LocalTableInContext<'_, Def> {
pub fn type_dependent_defs(
&self,
) -> LocalTableInContext<'_, Result<(DefKind, DefId), ErrorReported>> {
LocalTableInContext {
local_id_root: self.local_id_root,
data: &self.type_dependent_defs
}
}

pub fn type_dependent_def(&self, id: HirId) -> Option<Def> {
pub fn type_dependent_def(&self, id: HirId) -> Option<(DefKind, DefId)> {
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
self.type_dependent_defs.get(&id.local_id).cloned()
self.type_dependent_defs.get(&id.local_id).cloned().and_then(|r| r.ok())
}

pub fn type_dependent_def_id(&self, id: HirId) -> Option<DefId> {
self.type_dependent_def(id).map(|def| def.def_id())
self.type_dependent_def(id).map(|(_, def_id)| def_id)
}

pub fn type_dependent_defs_mut(&mut self) -> LocalTableInContextMut<'_, Def> {
pub fn type_dependent_defs_mut(
&mut self,
) -> LocalTableInContextMut<'_, Result<(DefKind, DefId), ErrorReported>> {
LocalTableInContextMut {
local_id_root: self.local_id_root,
data: &mut self.type_dependent_defs
@@ -658,7 +661,7 @@ impl<'tcx> TypeckTables<'tcx> {
}

match self.type_dependent_defs().get(expr.hir_id) {
Some(&Def::Def(DefKind::Method, _)) => true,
Some(Ok((DefKind::Method, _))) => true,
_ => false
}
}
@@ -191,12 +191,12 @@ pub enum AssociatedKind {
}

impl AssociatedItem {
pub fn def(&self) -> Def {
pub fn def_kind(&self) -> DefKind {
match self.kind {
AssociatedKind::Const => Def::Def(DefKind::AssociatedConst, self.def_id),
AssociatedKind::Method => Def::Def(DefKind::Method, self.def_id),
AssociatedKind::Type => Def::Def(DefKind::AssociatedTy, self.def_id),
AssociatedKind::Existential => Def::Def(DefKind::AssociatedExistential, self.def_id),
AssociatedKind::Const => DefKind::AssociatedConst,
AssociatedKind::Method => DefKind::Method,
AssociatedKind::Type => DefKind::AssociatedTy,
AssociatedKind::Existential => DefKind::AssociatedExistential,
}
}

@@ -2805,10 +2805,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
_ => false,
}
} else {
match self.describe_def(def_id).expect("no def for def-id") {
Def::Def(DefKind::AssociatedConst, _)
| Def::Def(DefKind::Method, _)
| Def::Def(DefKind::AssociatedTy, _) => true,
match self.def_kind(def_id).expect("no def for def-id") {
DefKind::AssociatedConst
| DefKind::Method
| DefKind::AssociatedTy => true,
_ => false,
}
};
@@ -3046,7 +3046,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// `DefId` of the impl that the method belongs to; otherwise, returns `None`.
pub fn impl_of_method(self, def_id: DefId) -> Option<DefId> {
let item = if def_id.krate != LOCAL_CRATE {
if let Some(Def::Def(DefKind::Method, _)) = self.describe_def(def_id) {
if let Some(DefKind::Method) = self.def_kind(def_id) {
Some(self.associated_item(def_id))
} else {
None
@@ -1,6 +1,6 @@
use crate::dep_graph::{self, DepNode};
use crate::hir::def_id::{CrateNum, DefId, DefIndex};
use crate::hir::def::{Def, Export};
use crate::hir::def::{DefKind, Export};
use crate::hir::{self, TraitCandidate, ItemLocalId, CodegenFnAttrs};
use crate::infer::canonical::{self, Canonical};
use crate::lint;
@@ -87,13 +87,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {

let mut fn_warned = false;
let mut op_warned = false;
let maybe_def = match expr.node {
let maybe_def_id = match expr.node {
hir::ExprKind::Call(ref callee, _) => {
match callee.node {
hir::ExprKind::Path(ref qpath) => {
let def = cx.tables.qpath_def(qpath, callee.hir_id);
match def {
Def::Def(DefKind::Fn, _) | Def::Def(DefKind::Method, _) => Some(def),
Def::Def(DefKind::Fn, def_id)
| Def::Def(DefKind::Method, def_id) => Some(def_id),
// `Def::Local` if it was a closure, for which we
// do not currently support must-use linting
_ => None
@@ -103,12 +104,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
}
},
hir::ExprKind::MethodCall(..) => {
cx.tables.type_dependent_def(expr.hir_id)
cx.tables.type_dependent_def_id(expr.hir_id)
},
_ => None
};
if let Some(def) = maybe_def {
let def_id = def.def_id();
if let Some(def_id) = maybe_def_id {
fn_warned = check_must_use(cx, def_id, s.span, "return value of ", "");
} else if type_permits_lack_of_use {
// We don't warn about unused unit or uninhabited types.
@@ -138,7 +138,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
is_const_fn_raw => { cdata.is_const_fn_raw(def_id.index) }
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
static_mutability => { cdata.static_mutability(def_id.index) }
describe_def => { cdata.get_def(def_id.index) }
def_kind => { 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))
Oops, something went wrong.

0 comments on commit b92b1a7

Please sign in to comment.
You can’t perform that action at this time.