Skip to content

Commit

Permalink
Auto merge of #60525 - eddyb:namespaces-not-kinds, r=petrochenkov
Browse files Browse the repository at this point in the history
rustc: collapse relevant DefPathData variants into {Type,Value,Macro,Lifetime}Ns.

`DefPathData` was meant to disambiguate within each namespace, but over the years, that purpose was overlooked, and it started to serve a double-role as a sort of `DefKind` (which #60462 properly adds).
Now, we can go back to *only* categorizing namespaces (at least for the variants with names in them).

r? @petrochenkov or @nikomatsakis cc @michaelwoerister
  • Loading branch information
bors committed May 5, 2019
2 parents c55b68a + 60f1944 commit d65e721
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 121 deletions.
4 changes: 2 additions & 2 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ impl<'a> LoweringContext<'a> {
self.resolver.definitions().create_def_with_parent(
parent_index,
node_id,
DefPathData::LifetimeParam(str_name),
DefPathData::LifetimeNs(str_name),
DefIndexAddressSpace::High,
Mark::root(),
span,
Expand Down Expand Up @@ -1749,7 +1749,7 @@ impl<'a> LoweringContext<'a> {
self.context.resolver.definitions().create_def_with_parent(
self.parent,
def_node_id,
DefPathData::LifetimeParam(name.ident().as_interned_str()),
DefPathData::LifetimeNs(name.ident().as_interned_str()),
DefIndexAddressSpace::High,
Mark::root(),
lifetime.span,
Expand Down
28 changes: 13 additions & 15 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,13 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
// information we encapsulate into, the better
let def_data = match i.node {
ItemKind::Impl(..) => DefPathData::Impl,
ItemKind::Trait(..) => DefPathData::Trait(i.ident.as_interned_str()),
ItemKind::TraitAlias(..) => DefPathData::TraitAlias(i.ident.as_interned_str()),
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
ItemKind::Existential(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
ItemKind::Ty(..) => DefPathData::TypeNs(i.ident.as_interned_str()),
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
return visit::walk_item(self, i);
}
ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
ItemKind::Existential(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
ItemKind::Ty(..) => DefPathData::TypeNs(i.ident.as_interned_str()),
ItemKind::Fn(
ref decl,
ref header,
Expand All @@ -165,10 +164,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
body,
)
}
ItemKind::Mod(..) => DefPathData::Module(i.ident.as_interned_str()),
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
DefPathData::ValueNs(i.ident.as_interned_str()),
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.as_interned_str()),
ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.as_interned_str()),
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id),
ItemKind::GlobalAsm(..) => DefPathData::Misc,
ItemKind::Use(..) => {
Expand Down Expand Up @@ -213,7 +211,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {

fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
let def = self.create_def(v.node.id,
DefPathData::EnumVariant(v.node.ident.as_interned_str()),
DefPathData::TypeNs(v.node.ident.as_interned_str()),
REGULAR_SPACE,
v.span);
self.with_parent(def, |this| {
Expand All @@ -230,7 +228,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
let name = field.ident.map(|ident| ident.name)
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
let def = self.create_def(field.id,
DefPathData::Field(name.as_interned_str()),
DefPathData::ValueNs(name.as_interned_str()),
REGULAR_SPACE,
field.span);
self.with_parent(def, |this| this.visit_struct_field(field));
Expand All @@ -240,9 +238,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
fn visit_generic_param(&mut self, param: &'a GenericParam) {
let name = param.ident.as_interned_str();
let def_path_data = match param.kind {
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeParam(name),
GenericParamKind::Type { .. } => DefPathData::TypeParam(name),
GenericParamKind::Const { .. } => DefPathData::ConstParam(name),
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name),
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
};
self.create_def(param.id, def_path_data, REGULAR_SPACE, param.ident.span);

Expand All @@ -254,7 +252,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
DefPathData::ValueNs(ti.ident.as_interned_str()),
TraitItemKind::Type(..) => {
DefPathData::AssocTypeInTrait(ti.ident.as_interned_str())
DefPathData::TypeNs(ti.ident.as_interned_str())
},
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
};
Expand All @@ -281,9 +279,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
}
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
DefPathData::ValueNs(ii.ident.as_interned_str()),
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.as_interned_str()),
ImplItemKind::Type(..) |
ImplItemKind::Existential(..) => {
DefPathData::AssocExistentialInImpl(ii.ident.as_interned_str())
DefPathData::TypeNs(ii.ident.as_interned_str())
},
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
};
Expand Down
56 changes: 8 additions & 48 deletions src/librustc/hir/map/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,35 +337,17 @@ pub enum DefPathData {
// Different kinds of items and item-like things:
/// An impl
Impl,
/// A trait
Trait(InternedString),
/// An associated type **declaration** (i.e., in a trait)
AssocTypeInTrait(InternedString),
/// An associated type **value** (i.e., in an impl)
AssocTypeInImpl(InternedString),
/// An existential associated type **value** (i.e., in an impl)
AssocExistentialInImpl(InternedString),
/// Something in the type NS
TypeNs(InternedString),
/// Something in the value NS
ValueNs(InternedString),
/// A module declaration
Module(InternedString),
/// A macro rule
MacroDef(InternedString),
/// Something in the macro NS
MacroNs(InternedString),
/// Something in the lifetime NS
LifetimeNs(InternedString),
/// A closure expression
ClosureExpr,
// Subportions of items
/// A type (generic) parameter
TypeParam(InternedString),
/// A lifetime (generic) parameter
LifetimeParam(InternedString),
/// A const (generic) parameter
ConstParam(InternedString),
/// A variant of a enum
EnumVariant(InternedString),
/// A struct field
Field(InternedString),
/// Implicit ctor for a unit or tuple-like struct or enum variant.
Ctor,
/// A constant expression (see {ast,hir}::AnonConst).
Expand All @@ -376,8 +358,6 @@ pub enum DefPathData {
/// a whole crate (as opposed to just one item). GlobalMetaData components
/// are only supposed to show up right below the crate root.
GlobalMetaData(InternedString),
/// A trait alias.
TraitAlias(InternedString),
}

#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
Expand Down Expand Up @@ -633,19 +613,9 @@ impl DefPathData {
use self::DefPathData::*;
match *self {
TypeNs(name) |
Trait(name) |
TraitAlias(name) |
AssocTypeInTrait(name) |
AssocTypeInImpl(name) |
AssocExistentialInImpl(name) |
ValueNs(name) |
Module(name) |
MacroDef(name) |
TypeParam(name) |
LifetimeParam(name) |
ConstParam(name) |
EnumVariant(name) |
Field(name) |
MacroNs(name) |
LifetimeNs(name) |
GlobalMetaData(name) => Some(name),

Impl |
Expand All @@ -662,19 +632,9 @@ impl DefPathData {
use self::DefPathData::*;
let s = match *self {
TypeNs(name) |
Trait(name) |
TraitAlias(name) |
AssocTypeInTrait(name) |
AssocTypeInImpl(name) |
AssocExistentialInImpl(name) |
ValueNs(name) |
Module(name) |
MacroDef(name) |
TypeParam(name) |
LifetimeParam(name) |
ConstParam(name) |
EnumVariant(name) |
Field(name) |
MacroNs(name) |
LifetimeNs(name) |
GlobalMetaData(name) => {
return name
}
Expand Down
22 changes: 11 additions & 11 deletions src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
// the children of the visible parent (as was done when computing
// `visible_parent_map`), looking for the specific child we currently have and then
// have access to the re-exported name.
DefPathData::Module(ref mut name) |
DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
let reexport = self.tcx().item_children(visible_parent)
.iter()
Expand All @@ -367,7 +366,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
}
// Re-exported `extern crate` (#43189).
DefPathData::CrateRoot => {
data = DefPathData::Module(
data = DefPathData::TypeNs(
self.tcx().original_crate_name(def_id.krate).as_interned_str(),
);
}
Expand Down Expand Up @@ -859,15 +858,16 @@ impl TyCtxt<'_, '_, '_> {
// (but also some things just print a `DefId` generally so maybe we need this?)
fn guess_def_namespace(self, def_id: DefId) -> Namespace {
match self.def_key(def_id).disambiguated_data.data {
DefPathData::ValueNs(..) |
DefPathData::EnumVariant(..) |
DefPathData::Field(..) |
DefPathData::AnonConst |
DefPathData::ConstParam(..) |
DefPathData::ClosureExpr |
DefPathData::Ctor => Namespace::ValueNS,

DefPathData::MacroDef(..) => Namespace::MacroNS,
DefPathData::TypeNs(..)
| DefPathData::CrateRoot
| DefPathData::ImplTrait => Namespace::TypeNS,

DefPathData::ValueNs(..)
| DefPathData::AnonConst
| DefPathData::ClosureExpr
| DefPathData::Ctor => Namespace::ValueNS,

DefPathData::MacroNs(..) => Namespace::MacroNS,

_ => Namespace::TypeNS,
}
Expand Down
13 changes: 3 additions & 10 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
use crate::hir;
use crate::hir::def::DefKind;
use crate::hir::def_id::DefId;
use crate::hir::map::DefPathData;
use crate::mir::interpret::{sign_extend, truncate};
Expand Down Expand Up @@ -529,21 +530,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

/// Returns `true` if `def_id` refers to a trait (i.e., `trait Foo { ... }`).
pub fn is_trait(self, def_id: DefId) -> bool {
if let DefPathData::Trait(_) = self.def_key(def_id).disambiguated_data.data {
true
} else {
false
}
self.def_kind(def_id) == Some(DefKind::Trait)
}

/// Returns `true` if `def_id` refers to a trait alias (i.e., `trait Foo = ...;`),
/// and `false` otherwise.
pub fn is_trait_alias(self, def_id: DefId) -> bool {
if let DefPathData::TraitAlias(_) = self.def_key(def_id).disambiguated_data.data {
true
} else {
false
}
self.def_kind(def_id) == Some(DefKind::TraitAlias)
}

/// Returns `true` if this `DefId` refers to the implicit constructor for
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ crate fn proc_macro_def_path_table(crate_root: &CrateRoot,
let def_index = definitions.create_def_with_parent(
crate_root,
ast::DUMMY_NODE_ID,
DefPathData::MacroDef(name.as_interned_str()),
DefPathData::MacroNs(name.as_interned_str()),
DefIndexAddressSpace::High,
Mark::root(),
DUMMY_SP);
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,13 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
let data = VariantData {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
// FIXME(eddyb) deduplicate these with `encode_enum_variant_ctor`.
ctor: variant.ctor_def_id.map(|did| did.index),
ctor_sig: None,
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
variant.ctor_def_id.map(|ctor_def_id| self.lazy(&tcx.fn_sig(ctor_def_id)))
} else {
None
},
};

let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap();
Expand Down
44 changes: 19 additions & 25 deletions src/librustc_mir/monomorphize/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ use std::sync::Arc;
use syntax::symbol::InternedString;
use rustc::dep_graph::{WorkProductId, WorkProduct, DepNode, DepConstructor};
use rustc::hir::{CodegenFnAttrFlags, HirId};
use rustc::hir::def::DefKind;
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
use rustc::hir::map::DefPathData;
use rustc::mir::mono::{Linkage, Visibility, CodegenUnitNameBuilder};
use rustc::middle::exported_symbols::SymbolExportLevel;
use rustc::ty::{self, TyCtxt, InstanceDef};
use rustc::ty::{self, DefIdTree, TyCtxt, InstanceDef};
use rustc::ty::print::characteristic_def_id_of_type;
use rustc::ty::query::Providers;
use rustc::util::common::time;
Expand Down Expand Up @@ -805,33 +805,27 @@ fn compute_codegen_unit_name(tcx: TyCtxt<'_, '_, '_>,
let mut cgu_def_id = None;
// Walk backwards from the item we want to find the module for:
loop {
let def_key = tcx.def_key(current_def_id);

match def_key.disambiguated_data.data {
DefPathData::Module(..) => {
if cgu_def_id.is_none() {
cgu_def_id = Some(current_def_id);
}
if current_def_id.index == CRATE_DEF_INDEX {
if cgu_def_id.is_none() {
// If we have not found a module yet, take the crate root.
cgu_def_id = Some(DefId {
krate: def_id.krate,
index: CRATE_DEF_INDEX,
});
}
DefPathData::CrateRoot { .. } => {
if cgu_def_id.is_none() {
// If we have not found a module yet, take the crate root.
cgu_def_id = Some(DefId {
krate: def_id.krate,
index: CRATE_DEF_INDEX,
});
}
break
}
_ => {
// If we encounter something that is not a module, throw away
// any module that we've found so far because we now know that
// it is nested within something else.
cgu_def_id = None;
break
} else if tcx.def_kind(current_def_id) == Some(DefKind::Mod) {
if cgu_def_id.is_none() {
cgu_def_id = Some(current_def_id);
}
} else {
// If we encounter something that is not a module, throw away
// any module that we've found so far because we now know that
// it is nested within something else.
cgu_def_id = None;
}

current_def_id.index = def_key.parent.unwrap();
current_def_id = tcx.parent(current_def_id).unwrap();
}

let cgu_def_id = cgu_def_id.unwrap();
Expand Down
25 changes: 20 additions & 5 deletions src/librustc_traits/lowering/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod environment;

use rustc::hir::def::DefKind;
use rustc::hir::def_id::DefId;
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc::hir::map::definitions::DefPathData;
Expand Down Expand Up @@ -157,13 +158,27 @@ crate fn program_clauses_for<'a, 'tcx>(
tcx: TyCtxt<'a, 'tcx, 'tcx>,
def_id: DefId,
) -> Clauses<'tcx> {
// FIXME(eddyb) this should only be using `def_kind`.
match tcx.def_key(def_id).disambiguated_data.data {
DefPathData::Trait(_) |
DefPathData::TraitAlias(_) => program_clauses_for_trait(tcx, def_id),
DefPathData::TypeNs(..) => match tcx.def_kind(def_id) {
Some(DefKind::Trait)
| Some(DefKind::TraitAlias) => program_clauses_for_trait(tcx, def_id),
// FIXME(eddyb) deduplicate this `associated_item` call with
// `program_clauses_for_associated_type_{value,def}`.
Some(DefKind::AssociatedTy) => match tcx.associated_item(def_id).container {
ty::AssociatedItemContainer::ImplContainer(_) =>
program_clauses_for_associated_type_value(tcx, def_id),
ty::AssociatedItemContainer::TraitContainer(_) =>
program_clauses_for_associated_type_def(tcx, def_id)
},
Some(DefKind::Struct)
| Some(DefKind::Enum)
| Some(DefKind::TyAlias)
| Some(DefKind::Union)
| Some(DefKind::Existential) => program_clauses_for_type_def(tcx, def_id),
_ => List::empty(),
},
DefPathData::Impl => program_clauses_for_impl(tcx, def_id),
DefPathData::AssocTypeInImpl(..) => program_clauses_for_associated_type_value(tcx, def_id),
DefPathData::AssocTypeInTrait(..) => program_clauses_for_associated_type_def(tcx, def_id),
DefPathData::TypeNs(..) => program_clauses_for_type_def(tcx, def_id),
_ => List::empty(),
}
}
Expand Down
Loading

0 comments on commit d65e721

Please sign in to comment.