Skip to content

Commit cff5b99

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 0612568 commit cff5b99

File tree

10 files changed

+133
-64
lines changed

10 files changed

+133
-64
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 & 20 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;
@@ -227,7 +227,12 @@ impl<'hir> Map<'hir> {
227227
self.tcx.definitions.opt_local_def_id_to_hir_id(def_id)
228228
}
229229

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

233238
Some(match node {
@@ -243,11 +248,11 @@ impl<'hir> Map<'hir> {
243248
ItemKind::Union(..) => DefKind::Union,
244249
ItemKind::Trait(..) => DefKind::Trait,
245250
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
246-
ItemKind::ExternCrate(_)
247-
| ItemKind::Use(..)
248-
| ItemKind::ForeignMod(..)
249-
| ItemKind::GlobalAsm(..)
250-
| ItemKind::Impl { .. } => return None,
251+
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
252+
ItemKind::Use(..) => DefKind::Use,
253+
ItemKind::ForeignMod(..) => DefKind::ForeignMod,
254+
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
255+
ItemKind::Impl { .. } => DefKind::Impl,
251256
},
252257
Node::ForeignItem(item) => match item.kind {
253258
ForeignItemKind::Fn(..) => DefKind::Fn,
@@ -277,10 +282,19 @@ impl<'hir> Map<'hir> {
277282
};
278283
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
279284
}
280-
Node::AnonConst(_)
281-
| Node::Field(_)
282-
| Node::Expr(_)
283-
| Node::Stmt(_)
285+
Node::AnonConst(_) => DefKind::AnonConst,
286+
Node::Field(_) => DefKind::Field,
287+
Node::Expr(expr) => match expr.kind {
288+
ExprKind::Closure { .. } => DefKind::Closure,
289+
_ => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
290+
},
291+
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
292+
Node::GenericParam(param) => match param.kind {
293+
GenericParamKind::Lifetime { .. } => DefKind::LifetimeParam,
294+
GenericParamKind::Type { .. } => DefKind::TyParam,
295+
GenericParamKind::Const { .. } => DefKind::ConstParam,
296+
},
297+
Node::Stmt(_)
284298
| Node::PathSegment(_)
285299
| Node::Ty(_)
286300
| Node::TraitRef(_)
@@ -292,13 +306,7 @@ impl<'hir> Map<'hir> {
292306
| Node::Lifetime(_)
293307
| Node::Visibility(_)
294308
| Node::Block(_)
295-
| Node::Crate(_) => return None,
296-
Node::MacroDef(_) => DefKind::Macro(MacroKind::Bang),
297-
Node::GenericParam(param) => match param.kind {
298-
GenericParamKind::Lifetime { .. } => return None,
299-
GenericParamKind::Type { .. } => DefKind::TyParam,
300-
GenericParamKind::Const { .. } => DefKind::ConstParam,
301-
},
309+
| Node::Crate(_) => bug!("def_kind: unsupported node: {}", self.node_to_string(hir_id)),
302310
})
303311
}
304312

@@ -1082,6 +1090,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10821090
}
10831091

10841092
pub fn provide(providers: &mut Providers<'_>) {
1085-
providers.def_kind =
1086-
|tcx, def_id| tcx.hir().def_kind(tcx.hir().as_local_hir_id(def_id.expect_local()));
1093+
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local());
10871094
}

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;
@@ -612,8 +613,8 @@ impl EmbargoVisitor<'tcx> {
612613
}
613614
}
614615

615-
// These have type privacy, so are not reachable unless they're
616-
// public
616+
// These have type privacy or are not namespaced, so are not reachable unless they're
617+
// public.
617618
DefKind::AssocConst
618619
| DefKind::AssocTy
619620
| DefKind::AssocOpaqueTy
@@ -626,7 +627,16 @@ impl EmbargoVisitor<'tcx> {
626627
| DefKind::AssocFn
627628
| DefKind::Trait
628629
| DefKind::TyParam
629-
| DefKind::Variant => (),
630+
| DefKind::Variant
631+
| DefKind::LifetimeParam
632+
| DefKind::ExternCrate
633+
| DefKind::Use
634+
| DefKind::ForeignMod
635+
| DefKind::AnonConst
636+
| DefKind::Field
637+
| DefKind::GlobalAsm
638+
| DefKind::Impl
639+
| DefKind::Closure => (),
630640
}
631641
}
632642

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,20 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
906906
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
907907
self.r.define(parent, ident, MacroNS, (res, vis, span, expansion))
908908
}
909-
Res::Def(DefKind::TyParam | DefKind::ConstParam, _)
909+
Res::Def(
910+
DefKind::TyParam
911+
| DefKind::ConstParam
912+
| DefKind::ExternCrate
913+
| DefKind::Use
914+
| DefKind::ForeignMod
915+
| DefKind::AnonConst
916+
| DefKind::Field
917+
| DefKind::LifetimeParam
918+
| DefKind::GlobalAsm
919+
| DefKind::Closure
920+
| DefKind::Impl,
921+
_,
922+
)
910923
| Res::Local(..)
911924
| Res::SelfTy(..)
912925
| Res::SelfCtor(..)

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4972,8 +4972,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
49724972
sugg_call = fields.iter().map(|_| "_").collect::<Vec<_>>().join(", ");
49734973
match def_id
49744974
.as_local()
4975-
.map(|def_id| hir.as_local_hir_id(def_id))
4976-
.and_then(|hir_id| hir.def_kind(hir_id))
4975+
.and_then(|def_id| hir.def_kind(def_id))
49774976
{
49784977
Some(hir::def::DefKind::Ctor(hir::def::CtorOf::Variant, _)) => {
49794978
msg = "instantiate this tuple variant";

0 commit comments

Comments
 (0)