@@ -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 ;
@@ -229,10 +229,14 @@ 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 > {
233- let node = self . find ( hir_id) ?;
232+ pub fn def_kind ( & self , local_def_id : LocalDefId ) -> DefKind {
233+ // FIXME(eddyb) support `find` on the crate root.
234+ if local_def_id. to_def_id ( ) . index == CRATE_DEF_INDEX {
235+ return DefKind :: Mod ;
236+ }
234237
235- Some ( match node {
238+ let hir_id = self . local_def_id_to_hir_id ( local_def_id) ;
239+ match self . get ( hir_id) {
236240 Node :: Item ( item) => match item. kind {
237241 ItemKind :: Static ( ..) => DefKind :: Static ,
238242 ItemKind :: Const ( ..) => DefKind :: Const ,
@@ -245,11 +249,11 @@ impl<'hir> Map<'hir> {
245249 ItemKind :: Union ( ..) => DefKind :: Union ,
246250 ItemKind :: Trait ( ..) => DefKind :: Trait ,
247251 ItemKind :: TraitAlias ( ..) => DefKind :: TraitAlias ,
248- ItemKind :: ExternCrate ( _)
249- | ItemKind :: Use ( ..)
250- | ItemKind :: ForeignMod ( ..)
251- | ItemKind :: GlobalAsm ( ..)
252- | ItemKind :: Impl { .. } => return None ,
252+ ItemKind :: ExternCrate ( _) => DefKind :: ExternCrate ,
253+ ItemKind :: Use ( ..) => DefKind :: Use ,
254+ ItemKind :: ForeignMod ( ..) => DefKind :: ForeignMod ,
255+ ItemKind :: GlobalAsm ( ..) => DefKind :: GlobalAsm ,
256+ ItemKind :: Impl { .. } => DefKind :: Impl ,
253257 } ,
254258 Node :: ForeignItem ( item) => match item. kind {
255259 ForeignItemKind :: Fn ( ..) => DefKind :: Fn ,
@@ -270,7 +274,7 @@ impl<'hir> Map<'hir> {
270274 Node :: Variant ( _) => DefKind :: Variant ,
271275 Node :: Ctor ( variant_data) => {
272276 // FIXME(eddyb) is this even possible, if we have a `Node::Ctor`?
273- variant_data. ctor_hir_id ( ) ? ;
277+ assert_ne ! ( variant_data. ctor_hir_id( ) , None ) ;
274278
275279 let ctor_of = match self . find ( self . get_parent_node ( hir_id) ) {
276280 Some ( Node :: Item ( ..) ) => def:: CtorOf :: Struct ,
@@ -279,10 +283,20 @@ impl<'hir> Map<'hir> {
279283 } ;
280284 DefKind :: Ctor ( ctor_of, def:: CtorKind :: from_hir ( variant_data) )
281285 }
282- Node :: AnonConst ( _)
283- | Node :: Field ( _)
284- | Node :: Expr ( _)
285- | Node :: Stmt ( _)
286+ Node :: AnonConst ( _) => DefKind :: AnonConst ,
287+ Node :: Field ( _) => DefKind :: Field ,
288+ Node :: Expr ( expr) => match expr. kind {
289+ ExprKind :: Closure ( .., None ) => DefKind :: Closure ,
290+ ExprKind :: Closure ( .., Some ( _) ) => DefKind :: Generator ,
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,14 +308,8 @@ 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- } ,
304- } )
311+ | Node :: Crate ( _) => bug ! ( "def_kind: unsupported node: {}" , self . node_to_string( hir_id) ) ,
312+ }
305313 }
306314
307315 fn find_entry ( & self , id : HirId ) -> Option < Entry < ' hir > > {
@@ -1084,11 +1092,5 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10841092}
10851093
10861094pub 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}
0 commit comments