Skip to content

Commit e5b9f54

Browse files
committed
rustc: collapse relevant DefPathData variants into TypeNs.
1 parent 13fde05 commit e5b9f54

File tree

9 files changed

+61
-90
lines changed

9 files changed

+61
-90
lines changed

src/librustc/hir/map/def_collector.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,13 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
139139
// information we encapsulate into, the better
140140
let def_data = match i.node {
141141
ItemKind::Impl(..) => DefPathData::Impl,
142-
ItemKind::Trait(..) => DefPathData::Trait(i.ident.as_interned_str()),
143-
ItemKind::TraitAlias(..) => DefPathData::TraitAlias(i.ident.as_interned_str()),
144-
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
145-
ItemKind::Existential(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
146-
ItemKind::Ty(..) => DefPathData::TypeNs(i.ident.as_interned_str()),
147142
ItemKind::Mod(..) if i.ident == keywords::Invalid.ident() => {
148143
return visit::walk_item(self, i);
149144
}
145+
ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) |
146+
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) |
147+
ItemKind::Existential(..) | ItemKind::ExternCrate(..) | ItemKind::ForeignMod(..) |
148+
ItemKind::Ty(..) => DefPathData::TypeNs(i.ident.as_interned_str()),
150149
ItemKind::Fn(
151150
ref decl,
152151
ref header,
@@ -163,7 +162,6 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
163162
body,
164163
)
165164
}
166-
ItemKind::Mod(..) => DefPathData::Module(i.ident.as_interned_str()),
167165
ItemKind::Static(..) | ItemKind::Const(..) | ItemKind::Fn(..) =>
168166
DefPathData::ValueNs(i.ident.as_interned_str()),
169167
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.as_interned_str()),
@@ -211,7 +209,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
211209

212210
fn visit_variant(&mut self, v: &'a Variant, g: &'a Generics, item_id: NodeId) {
213211
let def = self.create_def(v.node.id,
214-
DefPathData::EnumVariant(v.node.ident.as_interned_str()),
212+
DefPathData::TypeNs(v.node.ident.as_interned_str()),
215213
REGULAR_SPACE,
216214
v.span);
217215
self.with_parent(def, |this| {
@@ -239,7 +237,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
239237
let name = param.ident.as_interned_str();
240238
let def_path_data = match param.kind {
241239
GenericParamKind::Lifetime { .. } => DefPathData::LifetimeParam(name),
242-
GenericParamKind::Type { .. } => DefPathData::TypeParam(name),
240+
GenericParamKind::Type { .. } => DefPathData::TypeNs(name),
243241
GenericParamKind::Const { .. } => DefPathData::ConstParam(name),
244242
};
245243
self.create_def(param.id, def_path_data, REGULAR_SPACE, param.ident.span);
@@ -252,7 +250,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
252250
TraitItemKind::Method(..) | TraitItemKind::Const(..) =>
253251
DefPathData::ValueNs(ti.ident.as_interned_str()),
254252
TraitItemKind::Type(..) => {
255-
DefPathData::AssocTypeInTrait(ti.ident.as_interned_str())
253+
DefPathData::TypeNs(ti.ident.as_interned_str())
256254
},
257255
TraitItemKind::Macro(..) => return self.visit_macro_invoc(ti.id),
258256
};
@@ -279,9 +277,9 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
279277
}
280278
ImplItemKind::Method(..) | ImplItemKind::Const(..) =>
281279
DefPathData::ValueNs(ii.ident.as_interned_str()),
282-
ImplItemKind::Type(..) => DefPathData::AssocTypeInImpl(ii.ident.as_interned_str()),
280+
ImplItemKind::Type(..) |
283281
ImplItemKind::Existential(..) => {
284-
DefPathData::AssocExistentialInImpl(ii.ident.as_interned_str())
282+
DefPathData::TypeNs(ii.ident.as_interned_str())
285283
},
286284
ImplItemKind::Macro(..) => return self.visit_macro_invoc(ii.id),
287285
};

src/librustc/hir/map/definitions.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -337,33 +337,19 @@ 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),
354344
/// A macro rule
355345
MacroDef(InternedString),
356346
/// A closure expression
357347
ClosureExpr,
358348
// Subportions of items
359-
/// A type (generic) parameter
360-
TypeParam(InternedString),
361349
/// A lifetime (generic) parameter
362350
LifetimeParam(InternedString),
363351
/// A const (generic) parameter
364352
ConstParam(InternedString),
365-
/// A variant of a enum
366-
EnumVariant(InternedString),
367353
/// A struct field
368354
Field(InternedString),
369355
/// Implicit ctor for a unit or tuple-like struct or enum variant.
@@ -376,8 +362,6 @@ pub enum DefPathData {
376362
/// a whole crate (as opposed to just one item). GlobalMetaData components
377363
/// are only supposed to show up right below the crate root.
378364
GlobalMetaData(InternedString),
379-
/// A trait alias.
380-
TraitAlias(InternedString),
381365
}
382366

383367
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug,
@@ -633,18 +617,10 @@ impl DefPathData {
633617
use self::DefPathData::*;
634618
match *self {
635619
TypeNs(name) |
636-
Trait(name) |
637-
TraitAlias(name) |
638-
AssocTypeInTrait(name) |
639-
AssocTypeInImpl(name) |
640-
AssocExistentialInImpl(name) |
641620
ValueNs(name) |
642-
Module(name) |
643621
MacroDef(name) |
644-
TypeParam(name) |
645622
LifetimeParam(name) |
646623
ConstParam(name) |
647-
EnumVariant(name) |
648624
Field(name) |
649625
GlobalMetaData(name) => Some(name),
650626

@@ -662,18 +638,10 @@ impl DefPathData {
662638
use self::DefPathData::*;
663639
let s = match *self {
664640
TypeNs(name) |
665-
Trait(name) |
666-
TraitAlias(name) |
667-
AssocTypeInTrait(name) |
668-
AssocTypeInImpl(name) |
669-
AssocExistentialInImpl(name) |
670641
ValueNs(name) |
671-
Module(name) |
672642
MacroDef(name) |
673-
TypeParam(name) |
674643
LifetimeParam(name) |
675644
ConstParam(name) |
676-
EnumVariant(name) |
677645
Field(name) |
678646
GlobalMetaData(name) => {
679647
return name

src/librustc/ty/print/pretty.rs

Lines changed: 1 addition & 3 deletions
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
}
@@ -860,7 +859,6 @@ impl TyCtxt<'_, '_, '_> {
860859
fn guess_def_namespace(self, def_id: DefId) -> Namespace {
861860
match self.def_key(def_id).disambiguated_data.data {
862861
DefPathData::ValueNs(..) |
863-
DefPathData::EnumVariant(..) |
864862
DefPathData::Field(..) |
865863
DefPathData::AnonConst |
866864
DefPathData::ConstParam(..) |

src/librustc/ty/util.rs

Lines changed: 3 additions & 10 deletions
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/encoder.rs

Lines changed: 6 additions & 1 deletion
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

Lines changed: 19 additions & 25 deletions
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

Lines changed: 20 additions & 5 deletions
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
}

src/test/ui/symbol-names/basic.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: symbol-name(_ZN5basic4main17h08bcaf310214ed52E)
1+
error: symbol-name(_ZN5basic4main17hd72940ef9669d526E)
22
--> $DIR/basic.rs:3:1
33
|
44
LL | #[rustc_symbol_name]

src/test/ui/symbol-names/impl1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: symbol-name(_ZN5impl13foo3Foo3bar17hc487d6ec13fe9124E)
1+
error: symbol-name(_ZN5impl13foo3Foo3bar17he53b9bee7600ed8dE)
22
--> $DIR/impl1.rs:8:9
33
|
44
LL | #[rustc_symbol_name]
@@ -10,7 +10,7 @@ error: def-path(foo::Foo::bar)
1010
LL | #[rustc_def_path]
1111
| ^^^^^^^^^^^^^^^^^
1212

13-
error: symbol-name(_ZN5impl13bar33_$LT$impl$u20$impl1..foo..Foo$GT$3baz17h38577281258e1527E)
13+
error: symbol-name(_ZN5impl13bar33_$LT$impl$u20$impl1..foo..Foo$GT$3baz17h86c41f0462d901d4E)
1414
--> $DIR/impl1.rs:18:9
1515
|
1616
LL | #[rustc_symbol_name]

0 commit comments

Comments
 (0)