@@ -9,7 +9,7 @@ use rustc_data_structures::svh::Svh;
9
9
use rustc_data_structures:: sync:: { par_for_each_in, try_par_for_each_in, DynSend , DynSync } ;
10
10
use rustc_hir:: def:: { DefKind , Res } ;
11
11
use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId , LOCAL_CRATE } ;
12
- use rustc_hir:: definitions:: { DefKey , DefPath , DefPathHash } ;
12
+ use rustc_hir:: definitions:: { DefKey , DefPath , DefPathData , DefPathHash } ;
13
13
use rustc_hir:: intravisit:: { self , Visitor } ;
14
14
use rustc_hir:: * ;
15
15
use rustc_index:: Idx ;
@@ -168,6 +168,98 @@ impl<'hir> Map<'hir> {
168
168
self . tcx . definitions_untracked ( ) . def_path_hash ( def_id)
169
169
}
170
170
171
+ /// Do not call this function directly. The query should be called.
172
+ pub ( super ) fn def_kind ( self , local_def_id : LocalDefId ) -> DefKind {
173
+ let hir_id = self . tcx . local_def_id_to_hir_id ( local_def_id) ;
174
+ let node = match self . find ( hir_id) {
175
+ Some ( node) => node,
176
+ None => match self . def_key ( local_def_id) . disambiguated_data . data {
177
+ // FIXME: Some anonymous constants produced by `#[rustc_legacy_const_generics]`
178
+ // do not have corresponding HIR nodes, but they are still anonymous constants.
179
+ DefPathData :: AnonConst => return DefKind :: AnonConst ,
180
+ _ => bug ! ( "no HIR node for def id {local_def_id:?}" ) ,
181
+ } ,
182
+ } ;
183
+ match node {
184
+ Node :: Item ( item) => match item. kind {
185
+ ItemKind :: Static ( _, mt, _) => DefKind :: Static ( mt) ,
186
+ ItemKind :: Const ( ..) => DefKind :: Const ,
187
+ ItemKind :: Fn ( ..) => DefKind :: Fn ,
188
+ ItemKind :: Macro ( _, macro_kind) => DefKind :: Macro ( macro_kind) ,
189
+ ItemKind :: Mod ( ..) => DefKind :: Mod ,
190
+ ItemKind :: OpaqueTy ( ..) => DefKind :: OpaqueTy ,
191
+ ItemKind :: TyAlias ( ..) => DefKind :: TyAlias ,
192
+ ItemKind :: Enum ( ..) => DefKind :: Enum ,
193
+ ItemKind :: Struct ( ..) => DefKind :: Struct ,
194
+ ItemKind :: Union ( ..) => DefKind :: Union ,
195
+ ItemKind :: Trait ( ..) => DefKind :: Trait ,
196
+ ItemKind :: TraitAlias ( ..) => DefKind :: TraitAlias ,
197
+ ItemKind :: ExternCrate ( _) => DefKind :: ExternCrate ,
198
+ ItemKind :: Use ( ..) => DefKind :: Use ,
199
+ ItemKind :: ForeignMod { .. } => DefKind :: ForeignMod ,
200
+ ItemKind :: GlobalAsm ( ..) => DefKind :: GlobalAsm ,
201
+ ItemKind :: Impl ( impl_) => DefKind :: Impl { of_trait : impl_. of_trait . is_some ( ) } ,
202
+ } ,
203
+ Node :: ForeignItem ( item) => match item. kind {
204
+ ForeignItemKind :: Fn ( ..) => DefKind :: Fn ,
205
+ ForeignItemKind :: Static ( _, mt) => DefKind :: Static ( mt) ,
206
+ ForeignItemKind :: Type => DefKind :: ForeignTy ,
207
+ } ,
208
+ Node :: TraitItem ( item) => match item. kind {
209
+ TraitItemKind :: Const ( ..) => DefKind :: AssocConst ,
210
+ TraitItemKind :: Fn ( ..) => DefKind :: AssocFn ,
211
+ TraitItemKind :: Type ( ..) => DefKind :: AssocTy ,
212
+ } ,
213
+ Node :: ImplItem ( item) => match item. kind {
214
+ ImplItemKind :: Const ( ..) => DefKind :: AssocConst ,
215
+ ImplItemKind :: Fn ( ..) => DefKind :: AssocFn ,
216
+ ImplItemKind :: Type ( ..) => DefKind :: AssocTy ,
217
+ } ,
218
+ Node :: Variant ( _) => DefKind :: Variant ,
219
+ Node :: Ctor ( variant_data) => {
220
+ let ctor_of = match self . find_parent ( hir_id) {
221
+ Some ( Node :: Item ( ..) ) => def:: CtorOf :: Struct ,
222
+ Some ( Node :: Variant ( ..) ) => def:: CtorOf :: Variant ,
223
+ _ => unreachable ! ( ) ,
224
+ } ;
225
+ match variant_data. ctor_kind ( ) {
226
+ Some ( kind) => DefKind :: Ctor ( ctor_of, kind) ,
227
+ None => bug ! ( "constructor node without a constructor" ) ,
228
+ }
229
+ }
230
+ Node :: AnonConst ( _) => DefKind :: AnonConst ,
231
+ Node :: ConstBlock ( _) => DefKind :: InlineConst ,
232
+ Node :: Field ( _) => DefKind :: Field ,
233
+ Node :: Expr ( expr) => match expr. kind {
234
+ ExprKind :: Closure ( _) => DefKind :: Closure ,
235
+ _ => bug ! ( "def_kind: unsupported node: {}" , self . node_to_string( hir_id) ) ,
236
+ } ,
237
+ Node :: GenericParam ( param) => match param. kind {
238
+ GenericParamKind :: Lifetime { .. } => DefKind :: LifetimeParam ,
239
+ GenericParamKind :: Type { .. } => DefKind :: TyParam ,
240
+ GenericParamKind :: Const { .. } => DefKind :: ConstParam ,
241
+ } ,
242
+ Node :: Crate ( _) => DefKind :: Mod ,
243
+ Node :: Stmt ( _)
244
+ | Node :: PathSegment ( _)
245
+ | Node :: Ty ( _)
246
+ | Node :: TypeBinding ( _)
247
+ | Node :: Infer ( _)
248
+ | Node :: TraitRef ( _)
249
+ | Node :: Pat ( _)
250
+ | Node :: PatField ( _)
251
+ | Node :: ExprField ( _)
252
+ | Node :: Local ( _)
253
+ | Node :: Param ( _)
254
+ | Node :: Arm ( _)
255
+ | Node :: Lifetime ( _)
256
+ | Node :: Block ( _) => span_bug ! (
257
+ self . span( hir_id) ,
258
+ "unexpected node with def id {local_def_id:?}: {node:?}"
259
+ ) ,
260
+ }
261
+ }
262
+
171
263
/// Finds the id of the parent node to this one.
172
264
///
173
265
/// If calling repeatedly and iterating over parents, prefer [`Map::parent_iter`].
0 commit comments