From e5b9f54cd99b425ffe99e732995b9fd3be7b1d36 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Fri, 3 May 2019 21:51:24 +0300 Subject: [PATCH] rustc: collapse relevant DefPathData variants into TypeNs. --- src/librustc/hir/map/def_collector.rs | 20 ++++----- src/librustc/hir/map/definitions.rs | 32 -------------- src/librustc/ty/print/pretty.rs | 4 +- src/librustc/ty/util.rs | 13 ++---- src/librustc_metadata/encoder.rs | 7 ++- src/librustc_mir/monomorphize/partitioning.rs | 44 ++++++++----------- src/librustc_traits/lowering/mod.rs | 25 ++++++++--- src/test/ui/symbol-names/basic.stderr | 2 +- src/test/ui/symbol-names/impl1.stderr | 4 +- 9 files changed, 61 insertions(+), 90 deletions(-) diff --git a/src/librustc/hir/map/def_collector.rs b/src/librustc/hir/map/def_collector.rs index 78de8539859..e3e451a61f5 100644 --- a/src/librustc/hir/map/def_collector.rs +++ b/src/librustc/hir/map/def_collector.rs @@ -139,14 +139,13 @@ fn visit_item(&mut self, i: &'a Item) { // 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, @@ -163,7 +162,6 @@ fn visit_item(&mut self, i: &'a Item) { 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()), @@ -211,7 +209,7 @@ fn visit_foreign_item(&mut self, foreign_item: &'a ForeignItem) { 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| { @@ -239,7 +237,7 @@ 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::Type { .. } => DefPathData::TypeNs(name), GenericParamKind::Const { .. } => DefPathData::ConstParam(name), }; self.create_def(param.id, def_path_data, REGULAR_SPACE, param.ident.span); @@ -252,7 +250,7 @@ fn visit_trait_item(&mut self, ti: &'a TraitItem) { 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), }; @@ -279,9 +277,9 @@ fn visit_impl_item(&mut self, ii: &'a ImplItem) { } 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), }; diff --git a/src/librustc/hir/map/definitions.rs b/src/librustc/hir/map/definitions.rs index 1006d813e65..99b3865c82a 100644 --- a/src/librustc/hir/map/definitions.rs +++ b/src/librustc/hir/map/definitions.rs @@ -337,33 +337,19 @@ 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), /// 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. @@ -376,8 +362,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, @@ -633,18 +617,10 @@ pub fn get_opt_name(&self) -> Option { 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) | GlobalMetaData(name) => Some(name), @@ -662,18 +638,10 @@ pub fn as_interned_str(&self) -> InternedString { 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) | GlobalMetaData(name) => { return name diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index d10da495ee7..c66610c98d5 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -355,7 +355,6 @@ fn try_print_visible_def_path( // 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() @@ -367,7 +366,7 @@ fn try_print_visible_def_path( } // Re-exported `extern crate` (#43189). DefPathData::CrateRoot => { - data = DefPathData::Module( + data = DefPathData::TypeNs( self.tcx().original_crate_name(def_id.krate).as_interned_str(), ); } @@ -860,7 +859,6 @@ impl TyCtxt<'_, '_, '_> { 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(..) | diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs index 0cd1700dd0d..4c8ed71a57c 100644 --- a/src/librustc/ty/util.rs +++ b/src/librustc/ty/util.rs @@ -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}; @@ -529,21 +530,13 @@ pub fn is_closure(self, def_id: DefId) -> bool { /// 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 diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index f914184b34f..0946dad5320 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -586,8 +586,13 @@ fn encode_enum_variant_info( 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(); diff --git a/src/librustc_mir/monomorphize/partitioning.rs b/src/librustc_mir/monomorphize/partitioning.rs index 368bcc33399..f321e05d681 100644 --- a/src/librustc_mir/monomorphize/partitioning.rs +++ b/src/librustc_mir/monomorphize/partitioning.rs @@ -99,11 +99,11 @@ 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; @@ -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(); diff --git a/src/librustc_traits/lowering/mod.rs b/src/librustc_traits/lowering/mod.rs index c3cbdb03762..50c21302632 100644 --- a/src/librustc_traits/lowering/mod.rs +++ b/src/librustc_traits/lowering/mod.rs @@ -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; @@ -157,13 +158,27 @@ fn into_well_formed_goal(self) -> DomainGoal<'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(), } } diff --git a/src/test/ui/symbol-names/basic.stderr b/src/test/ui/symbol-names/basic.stderr index 6ddd93d632e..7539cbada8b 100644 --- a/src/test/ui/symbol-names/basic.stderr +++ b/src/test/ui/symbol-names/basic.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_ZN5basic4main17h08bcaf310214ed52E) +error: symbol-name(_ZN5basic4main17hd72940ef9669d526E) --> $DIR/basic.rs:3:1 | LL | #[rustc_symbol_name] diff --git a/src/test/ui/symbol-names/impl1.stderr b/src/test/ui/symbol-names/impl1.stderr index eda8646b5b4..20e48782a3a 100644 --- a/src/test/ui/symbol-names/impl1.stderr +++ b/src/test/ui/symbol-names/impl1.stderr @@ -1,4 +1,4 @@ -error: symbol-name(_ZN5impl13foo3Foo3bar17hc487d6ec13fe9124E) +error: symbol-name(_ZN5impl13foo3Foo3bar17he53b9bee7600ed8dE) --> $DIR/impl1.rs:8:9 | LL | #[rustc_symbol_name] @@ -10,7 +10,7 @@ error: def-path(foo::Foo::bar) LL | #[rustc_def_path] | ^^^^^^^^^^^^^^^^^ -error: symbol-name(_ZN5impl13bar33_$LT$impl$u20$impl1..foo..Foo$GT$3baz17h38577281258e1527E) +error: symbol-name(_ZN5impl13bar33_$LT$impl$u20$impl1..foo..Foo$GT$3baz17h86c41f0462d901d4E) --> $DIR/impl1.rs:18:9 | LL | #[rustc_symbol_name] -- 2.44.0