Skip to content

Commit 17d3d95

Browse files
mark-i-meddyb
authored andcommitted
add a few more DefKinds
make Map::def_kind take LocalDefId Co-Authored-By: Vadim Petrochenkov <vadim.petrochenkov@gmail.com> crates are DefKind::Mod
1 parent b2c1a60 commit 17d3d95

File tree

10 files changed

+157
-90
lines changed

10 files changed

+157
-90
lines changed

src/librustc_hir/def.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ pub enum DefKind {
7777

7878
// Macro namespace
7979
Macro(MacroKind),
80+
81+
// Not namespaced (or they are, but we don't treat them so)
82+
ExternCrate,
83+
Use,
84+
ForeignMod,
85+
AnonConst,
86+
Field,
87+
LifetimeParam,
88+
GlobalAsm,
89+
Impl,
90+
Closure,
8091
}
8192

8293
impl DefKind {
@@ -113,6 +124,15 @@ impl DefKind {
113124
DefKind::TyParam => "type parameter",
114125
DefKind::ConstParam => "const parameter",
115126
DefKind::Macro(macro_kind) => macro_kind.descr(),
127+
DefKind::LifetimeParam => "lifetime parameter",
128+
DefKind::Use => "import",
129+
DefKind::ForeignMod => "foreign module",
130+
DefKind::AnonConst => "anonymous constant",
131+
DefKind::Field => "field",
132+
DefKind::Impl => "implementation",
133+
DefKind::Closure => "closure",
134+
DefKind::ExternCrate => "extern crate",
135+
DefKind::GlobalAsm => "global assembly block",
116136
}
117137
}
118138

@@ -124,7 +144,9 @@ impl DefKind {
124144
| DefKind::AssocOpaqueTy
125145
| DefKind::AssocFn
126146
| DefKind::Enum
127-
| DefKind::OpaqueTy => "an",
147+
| DefKind::OpaqueTy
148+
| DefKind::AnonConst
149+
| DefKind::Impl => "an",
128150
DefKind::Macro(macro_kind) => macro_kind.article(),
129151
_ => "a",
130152
}
@@ -155,6 +177,17 @@ impl DefKind {
155177
| DefKind::AssocConst => ns == Namespace::ValueNS,
156178

157179
DefKind::Macro(..) => ns == Namespace::MacroNS,
180+
181+
// Not namespaced.
182+
DefKind::AnonConst
183+
| DefKind::Field
184+
| DefKind::LifetimeParam
185+
| DefKind::ExternCrate
186+
| DefKind::Closure
187+
| DefKind::Use
188+
| DefKind::ForeignMod
189+
| DefKind::GlobalAsm
190+
| DefKind::Impl => false,
158191
}
159192
}
160193
}

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,8 @@ impl MetadataBlob {
562562
}
563563

564564
impl EntryKind {
565-
fn def_kind(&self) -> Option<DefKind> {
566-
Some(match *self {
565+
fn def_kind(&self) -> DefKind {
566+
match *self {
567567
EntryKind::Const(..) => DefKind::Const,
568568
EntryKind::AssocConst(..) => DefKind::AssocConst,
569569
EntryKind::ImmStatic
@@ -587,14 +587,13 @@ impl EntryKind {
587587
EntryKind::Enum(..) => DefKind::Enum,
588588
EntryKind::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
589589
EntryKind::ForeignType => DefKind::ForeignTy,
590-
591-
EntryKind::ForeignMod
592-
| EntryKind::GlobalAsm
593-
| EntryKind::Impl(_)
594-
| EntryKind::Field
595-
| EntryKind::Generator(_)
596-
| EntryKind::Closure => return None,
597-
})
590+
EntryKind::Impl(_) => DefKind::Impl,
591+
EntryKind::Closure => DefKind::Closure,
592+
EntryKind::ForeignMod => DefKind::ForeignMod,
593+
EntryKind::GlobalAsm => DefKind::GlobalAsm,
594+
EntryKind::Field => DefKind::Field,
595+
EntryKind::Generator(_) => DefKind::Closure,
596+
}
598597
}
599598
}
600599

@@ -679,11 +678,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
679678
}
680679
}
681680

682-
fn def_kind(&self, index: DefIndex) -> Option<DefKind> {
681+
fn def_kind(&self, index: DefIndex) -> DefKind {
683682
if !self.is_proc_macro(index) {
684683
self.kind(index).def_kind()
685684
} else {
686-
Some(DefKind::Macro(macro_kind(self.raw_proc_macro(index))))
685+
DefKind::Macro(macro_kind(self.raw_proc_macro(index)))
687686
}
688687
}
689688

@@ -1009,20 +1008,19 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10091008
.get(self, child_index)
10101009
.unwrap_or(Lazy::empty());
10111010
for child_index in child_children.decode((self, sess)) {
1012-
if let Some(kind) = self.def_kind(child_index) {
1013-
callback(Export {
1014-
res: Res::Def(kind, self.local_def_id(child_index)),
1015-
ident: self.item_ident(child_index, sess),
1016-
vis: self.get_visibility(child_index),
1017-
span: self
1018-
.root
1019-
.tables
1020-
.span
1021-
.get(self, child_index)
1022-
.unwrap()
1023-
.decode((self, sess)),
1024-
});
1025-
}
1011+
let kind = self.def_kind(child_index);
1012+
callback(Export {
1013+
res: Res::Def(kind, self.local_def_id(child_index)),
1014+
ident: self.item_ident(child_index, sess),
1015+
vis: self.get_visibility(child_index),
1016+
span: self
1017+
.root
1018+
.tables
1019+
.span
1020+
.get(self, child_index)
1021+
.unwrap()
1022+
.decode((self, sess)),
1023+
});
10261024
}
10271025
continue;
10281026
}
@@ -1033,10 +1031,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10331031

10341032
let def_key = self.def_key(child_index);
10351033
let span = self.get_span(child_index, sess);
1036-
if let (Some(kind), true) = (
1037-
self.def_kind(child_index),
1038-
def_key.disambiguated_data.data.get_opt_name().is_some(),
1039-
) {
1034+
if def_key.disambiguated_data.data.get_opt_name().is_some() {
1035+
let kind = self.def_kind(child_index);
10401036
let ident = self.item_ident(child_index, sess);
10411037
let vis = self.get_visibility(child_index);
10421038
let def_id = self.local_def_id(child_index);

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
127127
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
128128
static_mutability => { cdata.static_mutability(def_id.index) }
129129
generator_kind => { cdata.generator_kind(def_id.index) }
130-
def_kind => { cdata.def_kind(def_id.index) }
130+
def_kind => { Some(cdata.def_kind(def_id.index)) }
131131
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
132132
lookup_stability => {
133133
cdata.get_stability(def_id.index).map(|s| tcx.intern_stability(s))

src/librustc_middle/hir/map/mod.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::ty::TyCtxt;
66
use rustc_ast::ast::{self, Name, NodeId};
77
use rustc_data_structures::svh::Svh;
88
use rustc_hir::def::{DefKind, Res};
9-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
9+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1010
use rustc_hir::definitions::{DefKey, DefPath, Definitions};
1111
use rustc_hir::intravisit;
1212
use rustc_hir::itemlikevisit::ItemLikeVisitor;
@@ -229,7 +229,12 @@ impl<'hir> Map<'hir> {
229229
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
230230
}
231231

232-
pub fn def_kind(&self, hir_id: HirId) -> Option<DefKind> {
232+
pub fn def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
233+
if local_def_id.to_def_id().index == CRATE_DEF_INDEX {
234+
return Some(DefKind::Mod);
235+
}
236+
237+
let hir_id = self.local_def_id_to_hir_id(local_def_id);
233238
let node = self.find(hir_id)?;
234239

235240
Some(match node {
@@ -245,11 +250,11 @@ impl<'hir> Map<'hir> {
245250
ItemKind::Union(..) => DefKind::Union,
246251
ItemKind::Trait(..) => DefKind::Trait,
247252
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
248-
ItemKind::ExternCrate(_)
249-
| ItemKind::Use(..)
250-
| ItemKind::ForeignMod(..)
251-
| ItemKind::GlobalAsm(..)
252-
| ItemKind::Impl { .. } => return None,
253+
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
254+
ItemKind::Use(..) => DefKind::Use,
255+
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
256+
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
257+
ItemKind::Impl { .. } => DefKind::Impl,
253258
},
254259
Node::ForeignItem(item) => match item.kind {
255260
ForeignItemKind::Fn(..) => DefKind::Fn,
@@ -279,10 +284,19 @@ impl<'hir> Map<'hir> {
279284
};
280285
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
281286
}
282-
Node::AnonConst(_)
283-
| Node::Field(_)
284-
| Node::Expr(_)
285-
| Node::Stmt(_)
287+
Node::AnonConst(_) => DefKind::AnonConst,
288+
Node::Field(_) => DefKind::Field,
289+
Node::Expr(expr) => match expr.kind {
290+
ExprKind::Closure { .. } => DefKind::Closure,
291+
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
292+
},
293+
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
294+
Node::GenericParam(param) => match param.kind {
295+
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
296+
GenericParamKind::Type { .. } => DefKind::TyParam,
297+
GenericParamKind::Const { .. } => DefKind::ConstParam,
298+
},
299+
Node::Stmt(_)
286300
| Node::PathSegment(_)
287301
| Node::Ty(_)
288302
| Node::TraitRef(_)
@@ -294,13 +308,7 @@ impl<'hir> Map<'hir> {
294308
| Node::Lifetime(_)
295309
| Node::Visibility(_)
296310
| Node::Block(_)
297-
| Node::Crate(_) => return None,
298-
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
299-
Node::GenericParam(param) => match param.kind {
300-
GenericParamKind::Lifetime { .. } => return None,
301-
GenericParamKind::Type { .. } => DefKind::TyParam,
302-
GenericParamKind::Const { .. } => DefKind::ConstParam,
303-
},
311+
| Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
304312
})
305313
}
306314

@@ -1086,11 +1094,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10861094
}
10871095

10881096
pub fn provide(providers: &mut Providers<'_>) {
1089-
providers.def_kind = |tcx, def_id| {
1090-
if let Some(hir_id) = tcx.hir().as_local_hir_id(def_id) {
1091-
tcx.hir().def_kind(hir_id)
1092-
} else {
1093-
bug!("calling local def_kind query provider for upstream DefId: {:?}", def_id);
1094-
}
1095-
};
1097+
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local());
10961098
}

src/librustc_mir/util/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ fn write_mir_sig(
816816
write!(w, "static {}", if tcx.is_mutable_static(src.def_id()) { "mut " } else { "" })?
817817
}
818818
(_, _) if is_function => write!(w, "fn ")?,
819-
(None, _) => {} // things like anon const, not an item
819+
(Some(DefKind::AnonConst), _) | (None, _) => {} // things like anon const, not an item
820820
_ => bug!("Unexpected def kind {:?}", kind),
821821
}
822822

src/librustc_privacy/lib.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
22
#![feature(in_band_lifetimes)]
33
#![feature(nll)]
4+
#![feature(or_patterns)]
45
#![recursion_limit = "256"]
56

67
use rustc_ast::ast::Ident;
@@ -610,8 +611,8 @@ impl EmbargoVisitor<'tcx> {
610611
}
611612
}
612613

613-
// These have type privacy, so are not reachable unless they're
614-
// public
614+
// These have type privacy or are not namespaced, so are not reachable unless they're
615+
// public.
615616
DefKind::AssocConst
616617
| DefKind::AssocTy
617618
| DefKind::AssocOpaqueTy
@@ -624,7 +625,16 @@ impl EmbargoVisitor<'tcx> {
624625
| DefKind::AssocFn
625626
| DefKind::Trait
626627
| DefKind::TyParam
627-
| DefKind::Variant => (),
628+
| DefKind::Variant
629+
| DefKind::LifetimeParam
630+
| DefKind::ExternCrate
631+
| DefKind::Use
632+
| DefKind::ForeignMod
633+
| DefKind::AnonConst
634+
| DefKind::Field
635+
| DefKind::GlobalAsm
636+
| DefKind::Impl
637+
| DefKind::Closure => (),
628638
}
629639
}
630640

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -870,9 +870,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
870870
let expansion = ExpnId::root(); // FIXME(jseyfried) intercrate hygiene
871871
// Record primary definitions.
872872
match res {
873-
Res::Def(kind @ DefKind::Mod, def_id)
874-
| Res::Def(kind @ DefKind::Enum, def_id)
875-
| Res::Def(kind @ DefKind::Trait, def_id) => {
873+
Res::Def(kind @ (DefKind::Mod | DefKind::Enum | DefKind::Trait), def_id) => {
876874
let module = self.r.new_module(
877875
parent,
878876
ModuleKind::Def(kind, def_id, ident.name),
@@ -882,30 +880,46 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
882880
);
883881
self.r.define(parent, ident, TypeNS, (module, vis, span, expansion));
884882
}
885-
Res::Def(DefKind::Struct, _)
886-
| Res::Def(DefKind::Union, _)
887-
| Res::Def(DefKind::Variant, _)
888-
| Res::Def(DefKind::TyAlias, _)
889-
| Res::Def(DefKind::ForeignTy, _)
890-
| Res::Def(DefKind::OpaqueTy, _)
891-
| Res::Def(DefKind::TraitAlias, _)
892-
| Res::Def(DefKind::AssocTy, _)
893-
| Res::Def(DefKind::AssocOpaqueTy, _)
883+
Res::Def(
884+
DefKind::Struct
885+
| DefKind::Union
886+
| DefKind::Variant
887+
| DefKind::TyAlias
888+
| DefKind::ForeignTy
889+
| DefKind::OpaqueTy
890+
| DefKind::TraitAlias
891+
| DefKind::AssocTy
892+
| DefKind::AssocOpaqueTy,
893+
_,
894+
)
894895
| Res::PrimTy(..)
895896
| Res::ToolMod => self.r.define(parent, ident, TypeNS, (res, vis, span, expansion)),
896-
Res::Def(DefKind::Fn, _)
897-
| Res::Def(DefKind::AssocFn, _)
898-
| Res::Def(DefKind::Static, _)
899-
| Res::Def(DefKind::Const, _)
900-
| Res::Def(DefKind::AssocConst, _)
901-
| Res::Def(DefKind::Ctor(..), _) => {
902-
self.r.define(parent, ident, ValueNS, (res, vis, span, expansion))
903-
}
897+
Res::Def(
898+
DefKind::Fn
899+
| DefKind::AssocFn
900+
| DefKind::Static
901+
| DefKind::Const
902+
| DefKind::AssocConst
903+
| DefKind::Ctor(..),
904+
_,
905+
) => self.r.define(parent, ident, ValueNS, (res, vis, span, expansion)),
904906
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
905907
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion))
906908
}
907-
Res::Def(DefKind::TyParam, _)
908-
| Res::Def(DefKind::ConstParam, _)
909+
Res::Def(
910+
DefKind::TyParam
911+
| DefKind::ConstParam
912+
| DefKind::AnonConst
913+
| DefKind::ExternCrate
914+
| DefKind::ForeignMod
915+
| DefKind::Closure
916+
| DefKind::Field
917+
| DefKind::Impl
918+
| DefKind::GlobalAsm
919+
| DefKind::LifetimeParam
920+
| DefKind::Use,
921+
_,
922+
)
909923
| Res::Local(..)
910924
| Res::SelfTy(..)
911925
| Res::SelfCtor(..)
@@ -914,7 +928,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
914928
// Record some extra data for better diagnostics.
915929
let cstore = self.r.cstore();
916930
match res {
917-
Res::Def(DefKind::Struct, def_id) | Res::Def(DefKind::Union, def_id) => {
931+
Res::Def(DefKind::Struct | DefKind::Union, def_id) => {
918932
let field_names = cstore.struct_field_names_untracked(def_id, self.r.session);
919933
self.insert_field_names(def_id, field_names);
920934
}

0 commit comments

Comments
 (0)