Skip to content

Commit 3ac26f7

Browse files
committed
add a few more DefKinds
make Map::def_kind take LocalDefId Co-Authored-By: Vadim Petrochenkov <[email protected]> crates are DefKind::Mod
1 parent ad48d52 commit 3ac26f7

File tree

9 files changed

+119
-67
lines changed

9 files changed

+119
-67
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

@@ -1084,11 +1092,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10841092
}
10851093

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

src/librustc_mir/util/pretty.rs

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

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/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,10 +2502,8 @@ impl<'a> Resolver<'a> {
25022502
}
25032503

25042504
let container = match parent.kind {
2505-
ModuleKind::Def(DefKind::Mod, _, _) => "module",
2506-
ModuleKind::Def(DefKind::Trait, _, _) => "trait",
2505+
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id().unwrap()),
25072506
ModuleKind::Block(..) => "block",
2508-
_ => "enum",
25092507
};
25102508

25112509
let old_noun = match old_binding.is_import() {

src/librustc_save_analysis/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,22 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
760760
Res::Def(HirDefKind::Mod, def_id) => {
761761
Some(Ref { kind: RefKind::Mod, span, ref_id: id_from_def_id(def_id) })
762762
}
763-
Res::PrimTy(..)
763+
764+
Res::Def(
765+
HirDefKind::Macro(..)
766+
| HirDefKind::ExternCrate
767+
| HirDefKind::ForeignMod
768+
| HirDefKind::LifetimeParam
769+
| HirDefKind::AnonConst
770+
| HirDefKind::Use
771+
| HirDefKind::Field
772+
| HirDefKind::GlobalAsm
773+
| HirDefKind::Impl
774+
| HirDefKind::Closure,
775+
_,
776+
)
777+
| Res::PrimTy(..)
764778
| Res::SelfTy(..)
765-
| Res::Def(HirDefKind::Macro(..), _)
766779
| Res::ToolMod
767780
| Res::NonMacroAttr(..)
768781
| Res::SelfCtor(..)

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4948,7 +4948,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
49484948
}
49494949
Some(Node::Ctor(hir::VariantData::Tuple(fields, _))) => {
49504950
sugg_call = fields.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
4951-
match hir.as_local_hir_id(def_id).and_then(|hir_id| hir.def_kind(hir_id)) {
4951+
match self.tcx.def_kind(def_id) {
49524952
Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, _)) => {
49534953
msg = "instantiate this tuple variant";
49544954
}

0 commit comments

Comments
 (0)