@@ -6,7 +6,7 @@ use crate::ty::TyCtxt;
66use rustc_ast:: ast:: { self , Name , NodeId } ;
77use rustc_data_structures:: svh:: Svh ;
88use 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 } ;
1010use rustc_hir:: definitions:: { DefKey , DefPath , Definitions } ;
1111use rustc_hir:: intravisit;
1212use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
@@ -227,10 +227,14 @@ 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 > {
231- let node = self . find ( hir_id) ?;
230+ pub fn def_kind ( & self , local_def_id : LocalDefId ) -> DefKind {
231+ // FIXME(eddyb) support `find` on the crate root.
232+ if local_def_id. to_def_id ( ) . index == CRATE_DEF_INDEX {
233+ return DefKind :: Mod ;
234+ }
232235
233- Some ( match node {
236+ let hir_id = self . local_def_id_to_hir_id ( local_def_id) ;
237+ match self . get ( hir_id) {
234238 Node :: Item ( item) => match item. kind {
235239 ItemKind :: Static ( ..) => DefKind :: Static ,
236240 ItemKind :: Const ( ..) => DefKind :: Const ,
@@ -243,11 +247,11 @@ impl<'hir> Map<'hir> {
243247 ItemKind :: Union ( ..) => DefKind :: Union ,
244248 ItemKind :: Trait ( ..) => DefKind :: Trait ,
245249 ItemKind :: TraitAlias ( ..) => DefKind :: TraitAlias ,
246- ItemKind :: ExternCrate ( _)
247- | ItemKind :: Use ( ..)
248- | ItemKind :: ForeignMod ( ..)
249- | ItemKind :: GlobalAsm ( ..)
250- | ItemKind :: Impl { .. } => return None ,
250+ ItemKind :: ExternCrate ( _) => DefKind :: ExternCrate ,
251+ ItemKind :: Use ( ..) => DefKind :: Use ,
252+ ItemKind :: ForeignMod ( ..) => DefKind :: ForeignMod ,
253+ ItemKind :: GlobalAsm ( ..) => DefKind :: GlobalAsm ,
254+ ItemKind :: Impl { .. } => DefKind :: Impl ,
251255 } ,
252256 Node :: ForeignItem ( item) => match item. kind {
253257 ForeignItemKind :: Fn ( ..) => DefKind :: Fn ,
@@ -268,7 +272,7 @@ impl<'hir> Map<'hir> {
268272 Node :: Variant ( _) => DefKind :: Variant ,
269273 Node :: Ctor ( variant_data) => {
270274 // FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
271- variant_data. ctor_hir_id ( ) ? ;
275+ assert_ne ! ( variant_data. ctor_hir_id( ) , None ) ;
272276
273277 let ctor_of = match self . find ( self . get_parent_node ( hir_id) ) {
274278 Some ( Node :: Item ( ..) ) => def:: CtorOf :: Struct ,
@@ -277,10 +281,20 @@ impl<'hir> Map<'hir> {
277281 } ;
278282 DefKind :: Ctor ( ctor_of, def:: CtorKind :: from_hir ( variant_data) )
279283 }
280- Node :: AnonConst ( _)
281- | Node :: Field ( _)
282- | Node :: Expr ( _)
283- | Node :: Stmt ( _)
284+ Node :: AnonConst ( _) => DefKind :: AnonConst ,
285+ Node :: Field ( _) => DefKind :: Field ,
286+ Node :: Expr ( expr) => match expr. kind {
287+ ExprKind :: Closure ( .., None ) => DefKind :: Closure ,
288+ ExprKind :: Closure ( .., Some ( _) ) => DefKind :: Generator ,
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,14 +306,8 @@ 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- } ,
302- } )
309+ | Node :: Crate ( _) => bug ! ( "def_kind: unsupported node: {}" , self . node_to_string( hir_id) ) ,
310+ }
303311 }
304312
305313 fn find_entry ( & self , id : HirId ) -> Option < Entry < ' hir > > {
@@ -1082,6 +1090,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10821090}
10831091
10841092pub 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}
0 commit comments