Skip to content

Commit d65e721

Browse files
committed
Auto merge of #60525 - eddyb:namespaces-not-kinds, r=petrochenkov
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
2 parents c55b68a + 60f1944 commit d65e721

File tree

11 files changed

+86
-121
lines changed

11 files changed

+86
-121
lines changed

src/librustc/hir/lowering.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ impl<'a> LoweringContext<'a> {
952952
self.resolver.definitions().create_def_with_parent(
953953
parent_index,
954954
node_id,
955-
DefPathData::LifetimeParam(str_name),
955+
DefPathData::LifetimeNs(str_name),
956956
DefIndexAddressSpace::High,
957957
Mark::root(),
958958
span,
@@ -1749,7 +1749,7 @@ impl<'a> LoweringContext<'a> {
17491749
self.context.resolver.definitions().create_def_with_parent(
17501750
self.parent,
17511751
def_node_id,
1752-
DefPathData::LifetimeParam(name.ident().as_interned_str()),
1752+
DefPathData::LifetimeNs(name.ident().as_interned_str()),
17531753
DefIndexAddressSpace::High,
17541754
Mark::root(),
17551755
lifetime.span,

src/librustc/hir/map/def_collector.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,13 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
141141
// information we encapsulate into, the better
142142
let def_data = match i.node {
143143
ItemKind::Impl(..) => DefPathData::Impl,
144-
ItemKind::Trait(..) => DefPathData::Trait(i.ident.as_interned_str()),
145-
ItemKind::TraitAlias(..) => DefPathData::TraitAlias(i.ident.as_interned_str()),
146-
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
147-
ItemKind::Existential(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
148-
ItemKind::Ty(..) => DefPathData::TypeNs(i.ident.as_interned_str()),
149144
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
150145
return visit::walk_item(self, i);
151146
}
147+
ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
148+
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
149+
ItemKind::Existential(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
150+
ItemKind::Ty(..) => DefPathData::TypeNs(i.ident.as_interned_str()),
152151
ItemKind::Fn(
153152
ref decl,
154153
ref header,
@@ -165,10 +164,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
165164
body,
166165
)
167166
}
168-
ItemKind::Mod(..) => DefPathData::Module(i.ident.as_interned_str()),
169167
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
170168
DefPathData::ValueNs(i.ident.as_interned_str()),
171-
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.as_interned_str()),
169+
ItemKind::MacroDef(..) => DefPathData::MacroNs(i.ident.as_interned_str()),
172170
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id),
173171
ItemKind::GlobalAsm(..) => DefPathData::Misc,
174172
ItemKind::Use(..) => {
@@ -213,7 +211,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
213211

214212
fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
215213
let def = self.create_def(v.node.id,
216-
DefPathData::EnumVariant(v.node.ident.as_interned_str()),
214+
DefPathData::TypeNs(v.node.ident.as_interned_str()),
217215
REGULAR_SPACE,
218216
v.span);
219217
self.with_parent(def, |this| {
@@ -230,7 +228,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
230228
let name = field.ident.map(|ident| ident.name)
231229
.unwrap_or_else(|| Symbol::intern(&index.to_string()));
232230
let def = self.create_def(field.id,
233-
DefPathData::Field(name.as_interned_str()),
231+
DefPathData::ValueNs(name.as_interned_str()),
234232
REGULAR_SPACE,
235233
field.span);
236234
self.with_parent(def, |this| this.visit_struct_field(field));
@@ -240,9 +238,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
240238
fn visit_generic_param(&mut self, param: &'a GenericParam) {
241239
let name = param.ident.as_interned_str();
242240
let def_path_data = match param.kind {
243-
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeParam(name),
244-
GenericParamKind::Type { .. } => DefPathData::TypeParam(name),
245-
GenericParamKind::Const { .. } => DefPathData::ConstParam(name),
241+
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeNs(name),
242+
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
243+
GenericParamKind::Const { .. } => DefPathData::ValueNs(name),
246244
};
247245
self.create_def(param.id, def_path_data, REGULAR_SPACE, param.ident.span);
248246

@@ -254,7 +252,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
254252
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
255253
DefPathData::ValueNs(ti.ident.as_interned_str()),
256254
TraitItemKind::Type(..) => {
257-
DefPathData::AssocTypeInTrait(ti.ident.as_interned_str())
255+
DefPathData::TypeNs(ti.ident.as_interned_str())
258256
},
259257
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
260258
};
@@ -281,9 +279,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
281279
}
282280
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
283281
DefPathData::ValueNs(ii.ident.as_interned_str()),
284-
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.as_interned_str()),
282+
ImplItemKind::Type(..) |
285283
ImplItemKind::Existential(..) => {
286-
DefPathData::AssocExistentialInImpl(ii.ident.as_interned_str())
284+
DefPathData::TypeNs(ii.ident.as_interned_str())
287285
},
288286
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
289287
};

src/librustc/hir/map/definitions.rs

+8-48
Original file line numberDiff line numberDiff line change
@@ -337,35 +337,17 @@ pub enum DefPathData {
337337
// Different kinds of items and item-like things:
338338
/// An impl
339339
Impl,
340-
/// A trait
341-
Trait(InternedString),
342-
/// An associated type **declaration** (i.e., in a trait)
343-
AssocTypeInTrait(InternedString),
344-
/// An associated type **value** (i.e., in an impl)
345-
AssocTypeInImpl(InternedString),
346-
/// An existential associated type **value** (i.e., in an impl)
347-
AssocExistentialInImpl(InternedString),
348340
/// Something in the type NS
349341
TypeNs(InternedString),
350342
/// Something in the value NS
351343
ValueNs(InternedString),
352-
/// A module declaration
353-
Module(InternedString),
354-
/// A macro rule
355-
MacroDef(InternedString),
344+
/// Something in the macro NS
345+
MacroNs(InternedString),
346+
/// Something in the lifetime NS
347+
LifetimeNs(InternedString),
356348
/// A closure expression
357349
ClosureExpr,
358350
// Subportions of items
359-
/// A type (generic) parameter
360-
TypeParam(InternedString),
361-
/// A lifetime (generic) parameter
362-
LifetimeParam(InternedString),
363-
/// A const (generic) parameter
364-
ConstParam(InternedString),
365-
/// A variant of a enum
366-
EnumVariant(InternedString),
367-
/// A struct field
368-
Field(InternedString),
369351
/// Implicit ctor for a unit or tuple-like struct or enum variant.
370352
Ctor,
371353
/// A constant expression (see {ast,hir}::AnonConst).
@@ -376,8 +358,6 @@ pub enum DefPathData {
376358
/// a whole crate (as opposed to just one item). GlobalMetaData components
377359
/// are only supposed to show up right below the crate root.
378360
GlobalMetaData(InternedString),
379-
/// A trait alias.
380-
TraitAlias(InternedString),
381361
}
382362

383363
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
@@ -633,19 +613,9 @@ impl DefPathData {
633613
use self::DefPathData::*;
634614
match *self {
635615
TypeNs(name) |
636-
Trait(name) |
637-
TraitAlias(name) |
638-
AssocTypeInTrait(name) |
639-
AssocTypeInImpl(name) |
640-
AssocExistentialInImpl(name) |
641616
ValueNs(name) |
642-
Module(name) |
643-
MacroDef(name) |
644-
TypeParam(name) |
645-
LifetimeParam(name) |
646-
ConstParam(name) |
647-
EnumVariant(name) |
648-
Field(name) |
617+
MacroNs(name) |
618+
LifetimeNs(name) |
649619
GlobalMetaData(name) => Some(name),
650620

651621
Impl |
@@ -662,19 +632,9 @@ impl DefPathData {
662632
use self::DefPathData::*;
663633
let s = match *self {
664634
TypeNs(name) |
665-
Trait(name) |
666-
TraitAlias(name) |
667-
AssocTypeInTrait(name) |
668-
AssocTypeInImpl(name) |
669-
AssocExistentialInImpl(name) |
670635
ValueNs(name) |
671-
Module(name) |
672-
MacroDef(name) |
673-
TypeParam(name) |
674-
LifetimeParam(name) |
675-
ConstParam(name) |
676-
EnumVariant(name) |
677-
Field(name) |
636+
MacroNs(name) |
637+
LifetimeNs(name) |
678638
GlobalMetaData(name) => {
679639
return name
680640
}

src/librustc/ty/print/pretty.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
355355
// the children of the visible parent (as was done when computing
356356
// `visible_parent_map`), looking for the specific child we currently have and then
357357
// have access to the re-exported name.
358-
DefPathData::Module(ref mut name) |
359358
DefPathData::TypeNs(ref mut name) if Some(visible_parent) != actual_parent => {
360359
let reexport = self.tcx().item_children(visible_parent)
361360
.iter()
@@ -367,7 +366,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
367366
}
368367
// Re-exported `extern crate` (#43189).
369368
DefPathData::CrateRoot => {
370-
data = DefPathData::Module(
369+
data = DefPathData::TypeNs(
371370
self.tcx().original_crate_name(def_id.krate).as_interned_str(),
372371
);
373372
}
@@ -859,15 +858,16 @@ impl TyCtxt<'_, '_, '_> {
859858
// (but also some things just print a `DefId` generally so maybe we need this?)
860859
fn guess_def_namespace(self, def_id: DefId) -> Namespace {
861860
match self.def_key(def_id).disambiguated_data.data {
862-
DefPathData::ValueNs(..) |
863-
DefPathData::EnumVariant(..) |
864-
DefPathData::Field(..) |
865-
DefPathData::AnonConst |
866-
DefPathData::ConstParam(..) |
867-
DefPathData::ClosureExpr |
868-
DefPathData::Ctor => Namespace::ValueNS,
869-
870-
DefPathData::MacroDef(..) => Namespace::MacroNS,
861+
DefPathData::TypeNs(..)
862+
| DefPathData::CrateRoot
863+
| DefPathData::ImplTrait => Namespace::TypeNS,
864+
865+
DefPathData::ValueNs(..)
866+
| DefPathData::AnonConst
867+
| DefPathData::ClosureExpr
868+
| DefPathData::Ctor => Namespace::ValueNS,
869+
870+
DefPathData::MacroNs(..) => Namespace::MacroNS,
871871

872872
_ => Namespace::TypeNS,
873873
}

src/librustc/ty/util.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
22
33
use crate::hir;
4+
use crate::hir::def::DefKind;
45
use crate::hir::def_id::DefId;
56
use crate::hir::map::DefPathData;
67
use crate::mir::interpret::{sign_extend, truncate};
@@ -529,21 +530,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
529530

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

539536
/// Returns `true` if `def_id` refers to a trait alias (i.e., `trait Foo = ...;`),
540537
/// and `false` otherwise.
541538
pub fn is_trait_alias(self, def_id: DefId) -> bool {
542-
if let DefPathData::TraitAlias(_) = self.def_key(def_id).disambiguated_data.data {
543-
true
544-
} else {
545-
false
546-
}
539+
self.def_kind(def_id) == Some(DefKind::TraitAlias)
547540
}
548541

549542
/// Returns `true` if this `DefId` refers to the implicit constructor for

src/librustc_metadata/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ crate fn proc_macro_def_path_table(crate_root: &CrateRoot,
459459
let def_index = definitions.create_def_with_parent(
460460
crate_root,
461461
ast::DUMMY_NODE_ID,
462-
DefPathData::MacroDef(name.as_interned_str()),
462+
DefPathData::MacroNs(name.as_interned_str()),
463463
DefIndexAddressSpace::High,
464464
Mark::root(),
465465
DUMMY_SP);

src/librustc_metadata/encoder.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,13 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
586586
let data = VariantData {
587587
ctor_kind: variant.ctor_kind,
588588
discr: variant.discr,
589+
// FIXME(eddyb) deduplicate these with `encode_enum_variant_ctor`.
589590
ctor: variant.ctor_def_id.map(|did| did.index),
590-
ctor_sig: None,
591+
ctor_sig: if variant.ctor_kind == CtorKind::Fn {
592+
variant.ctor_def_id.map(|ctor_def_id| self.lazy(&tcx.fn_sig(ctor_def_id)))
593+
} else {
594+
None
595+
},
591596
};
592597

593598
let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap();

src/librustc_mir/monomorphize/partitioning.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ use std::sync::Arc;
9999
use syntax::symbol::InternedString;
100100
use rustc::dep_graph::{WorkProductId, WorkProduct, DepNode, DepConstructor};
101101
use rustc::hir::{CodegenFnAttrFlags, HirId};
102+
use rustc::hir::def::DefKind;
102103
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE, CRATE_DEF_INDEX};
103-
use rustc::hir::map::DefPathData;
104104
use rustc::mir::mono::{Linkage, Visibility, CodegenUnitNameBuilder};
105105
use rustc::middle::exported_symbols::SymbolExportLevel;
106-
use rustc::ty::{self, TyCtxt, InstanceDef};
106+
use rustc::ty::{self, DefIdTree, TyCtxt, InstanceDef};
107107
use rustc::ty::print::characteristic_def_id_of_type;
108108
use rustc::ty::query::Providers;
109109
use rustc::util::common::time;
@@ -805,33 +805,27 @@ fn compute_codegen_unit_name(tcx: TyCtxt<'_, '_, '_>,
805805
let mut cgu_def_id = None;
806806
// Walk backwards from the item we want to find the module for:
807807
loop {
808-
let def_key = tcx.def_key(current_def_id);
809-
810-
match def_key.disambiguated_data.data {
811-
DefPathData::Module(..) => {
812-
if cgu_def_id.is_none() {
813-
cgu_def_id = Some(current_def_id);
814-
}
808+
if current_def_id.index == CRATE_DEF_INDEX {
809+
if cgu_def_id.is_none() {
810+
// If we have not found a module yet, take the crate root.
811+
cgu_def_id = Some(DefId {
812+
krate: def_id.krate,
813+
index: CRATE_DEF_INDEX,
814+
});
815815
}
816-
DefPathData::CrateRoot { .. } => {
817-
if cgu_def_id.is_none() {
818-
// If we have not found a module yet, take the crate root.
819-
cgu_def_id = Some(DefId {
820-
krate: def_id.krate,
821-
index: CRATE_DEF_INDEX,
822-
});
823-
}
824-
break
825-
}
826-
_ => {
827-
// If we encounter something that is not a module, throw away
828-
// any module that we've found so far because we now know that
829-
// it is nested within something else.
830-
cgu_def_id = None;
816+
break
817+
} else if tcx.def_kind(current_def_id) == Some(DefKind::Mod) {
818+
if cgu_def_id.is_none() {
819+
cgu_def_id = Some(current_def_id);
831820
}
821+
} else {
822+
// If we encounter something that is not a module, throw away
823+
// any module that we've found so far because we now know that
824+
// it is nested within something else.
825+
cgu_def_id = None;
832826
}
833827

834-
current_def_id.index = def_key.parent.unwrap();
828+
current_def_id = tcx.parent(current_def_id).unwrap();
835829
}
836830

837831
let cgu_def_id = cgu_def_id.unwrap();

src/librustc_traits/lowering/mod.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod environment;
22

3+
use rustc::hir::def::DefKind;
34
use rustc::hir::def_id::DefId;
45
use rustc::hir::intravisit::{self, NestedVisitorMap, Visitor};
56
use rustc::hir::map::definitions::DefPathData;
@@ -157,13 +158,27 @@ crate fn program_clauses_for<'a, 'tcx>(
157158
tcx: TyCtxt<'a, 'tcx, 'tcx>,
158159
def_id: DefId,
159160
) -> Clauses<'tcx> {
161+
// FIXME(eddyb) this should only be using `def_kind`.
160162
match tcx.def_key(def_id).disambiguated_data.data {
161-
DefPathData::Trait(_) |
162-
DefPathData::TraitAlias(_) => program_clauses_for_trait(tcx, def_id),
163+
DefPathData::TypeNs(..) => match tcx.def_kind(def_id) {
164+
Some(DefKind::Trait)
165+
| Some(DefKind::TraitAlias) => program_clauses_for_trait(tcx, def_id),
166+
// FIXME(eddyb) deduplicate this `associated_item` call with
167+
// `program_clauses_for_associated_type_{value,def}`.
168+
Some(DefKind::AssociatedTy) => match tcx.associated_item(def_id).container {
169+
ty::AssociatedItemContainer::ImplContainer(_) =>
170+
program_clauses_for_associated_type_value(tcx, def_id),
171+
ty::AssociatedItemContainer::TraitContainer(_) =>
172+
program_clauses_for_associated_type_def(tcx, def_id)
173+
},
174+
Some(DefKind::Struct)
175+
| Some(DefKind::Enum)
176+
| Some(DefKind::TyAlias)
177+
| Some(DefKind::Union)
178+
| Some(DefKind::Existential) => program_clauses_for_type_def(tcx, def_id),
179+
_ => List::empty(),
180+
},
163181
DefPathData::Impl => program_clauses_for_impl(tcx, def_id),
164-
DefPathData::AssocTypeInImpl(..) => program_clauses_for_associated_type_value(tcx, def_id),
165-
DefPathData::AssocTypeInTrait(..) => program_clauses_for_associated_type_def(tcx, def_id),
166-
DefPathData::TypeNs(..) => program_clauses_for_type_def(tcx, def_id),
167182
_ => List::empty(),
168183
}
169184
}

0 commit comments

Comments
 (0)