22
33use std:: sync:: Arc ;
44
5- use hir_expand:: { name:: Name , AstId , ExpandResult , HirFileId , MacroCallId , MacroDefKind } ;
5+ use hir_expand:: { name:: Name , AstId , ExpandResult , HirFileId , InFile , MacroCallId , MacroDefKind } ;
66use smallvec:: SmallVec ;
77use syntax:: ast;
88
@@ -12,7 +12,10 @@ use crate::{
1212 db:: DefDatabase ,
1313 intern:: Interned ,
1414 item_tree:: { self , AssocItem , FnFlags , ItemTree , ItemTreeId , ModItem , Param , TreeId } ,
15- nameres:: { attr_resolution:: ResolvedAttr , proc_macro:: ProcMacroKind , DefMap } ,
15+ nameres:: {
16+ attr_resolution:: ResolvedAttr , diagnostics:: DefDiagnostic , proc_macro:: ProcMacroKind ,
17+ DefMap ,
18+ } ,
1619 type_ref:: { TraitRef , TypeBound , TypeRef } ,
1720 visibility:: RawVisibility ,
1821 AssocItemId , AstIdWithPath , ConstId , ConstLoc , FunctionId , FunctionLoc , HasModule , ImplId ,
@@ -210,6 +213,13 @@ pub struct TraitData {
210213
211214impl TraitData {
212215 pub ( crate ) fn trait_data_query ( db : & dyn DefDatabase , tr : TraitId ) -> Arc < TraitData > {
216+ db. trait_data_with_diagnostics ( tr) . 0
217+ }
218+
219+ pub ( crate ) fn trait_data_with_diagnostics_query (
220+ db : & dyn DefDatabase ,
221+ tr : TraitId ,
222+ ) -> ( Arc < TraitData > , Arc < Vec < DefDiagnostic > > ) {
213223 let tr_loc @ ItemLoc { container : module_id, id : tree_id } = tr. lookup ( db) ;
214224 let item_tree = tree_id. item_tree ( db) ;
215225 let tr_def = & item_tree[ tree_id. value ] ;
@@ -229,17 +239,20 @@ impl TraitData {
229239 let mut collector =
230240 AssocItemCollector :: new ( db, module_id, tree_id. file_id ( ) , ItemContainerId :: TraitId ( tr) ) ;
231241 collector. collect ( & item_tree, tree_id. tree_id ( ) , & tr_def. items ) ;
232- let ( items, attribute_calls) = collector. finish ( ) ;
233-
234- Arc :: new ( TraitData {
235- name,
236- attribute_calls,
237- items,
238- is_auto,
239- is_unsafe,
240- visibility,
241- skip_array_during_method_dispatch,
242- } )
242+ let ( items, attribute_calls, diagnostics) = collector. finish ( ) ;
243+
244+ (
245+ Arc :: new ( TraitData {
246+ name,
247+ attribute_calls,
248+ items,
249+ is_auto,
250+ is_unsafe,
251+ visibility,
252+ skip_array_during_method_dispatch,
253+ } ) ,
254+ Arc :: new ( diagnostics) ,
255+ )
243256 }
244257
245258 pub fn associated_types ( & self ) -> impl Iterator < Item = TypeAliasId > + ' _ {
@@ -280,7 +293,14 @@ pub struct ImplData {
280293
281294impl ImplData {
282295 pub ( crate ) fn impl_data_query ( db : & dyn DefDatabase , id : ImplId ) -> Arc < ImplData > {
283- let _p = profile:: span ( "impl_data_query" ) ;
296+ db. impl_data_with_diagnostics ( id) . 0
297+ }
298+
299+ pub ( crate ) fn impl_data_with_diagnostics_query (
300+ db : & dyn DefDatabase ,
301+ id : ImplId ,
302+ ) -> ( Arc < ImplData > , Arc < Vec < DefDiagnostic > > ) {
303+ let _p = profile:: span ( "impl_data_with_diagnostics_query" ) ;
284304 let ItemLoc { container : module_id, id : tree_id } = id. lookup ( db) ;
285305
286306 let item_tree = tree_id. item_tree ( db) ;
@@ -293,10 +313,13 @@ impl ImplData {
293313 AssocItemCollector :: new ( db, module_id, tree_id. file_id ( ) , ItemContainerId :: ImplId ( id) ) ;
294314 collector. collect ( & item_tree, tree_id. tree_id ( ) , & impl_def. items ) ;
295315
296- let ( items, attribute_calls) = collector. finish ( ) ;
316+ let ( items, attribute_calls, diagnostics ) = collector. finish ( ) ;
297317 let items = items. into_iter ( ) . map ( |( _, item) | item) . collect ( ) ;
298318
299- Arc :: new ( ImplData { target_trait, self_ty, items, is_negative, attribute_calls } )
319+ (
320+ Arc :: new ( ImplData { target_trait, self_ty, items, is_negative, attribute_calls } ) ,
321+ Arc :: new ( diagnostics) ,
322+ )
300323 }
301324
302325 pub fn attribute_calls ( & self ) -> impl Iterator < Item = ( AstId < ast:: Item > , MacroCallId ) > + ' _ {
@@ -437,6 +460,7 @@ struct AssocItemCollector<'a> {
437460 db : & ' a dyn DefDatabase ,
438461 module_id : ModuleId ,
439462 def_map : Arc < DefMap > ,
463+ inactive_diagnostics : Vec < DefDiagnostic > ,
440464 container : ItemContainerId ,
441465 expander : Expander ,
442466
@@ -459,15 +483,21 @@ impl<'a> AssocItemCollector<'a> {
459483 expander : Expander :: new ( db, file_id, module_id) ,
460484 items : Vec :: new ( ) ,
461485 attr_calls : Vec :: new ( ) ,
486+ inactive_diagnostics : Vec :: new ( ) ,
462487 }
463488 }
464489
465490 fn finish (
466491 self ,
467- ) -> ( Vec < ( Name , AssocItemId ) > , Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ) {
492+ ) -> (
493+ Vec < ( Name , AssocItemId ) > ,
494+ Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ,
495+ Vec < DefDiagnostic > ,
496+ ) {
468497 (
469498 self . items ,
470499 if self . attr_calls . is_empty ( ) { None } else { Some ( Box :: new ( self . attr_calls ) ) } ,
500+ self . inactive_diagnostics ,
471501 )
472502 }
473503
@@ -479,6 +509,12 @@ impl<'a> AssocItemCollector<'a> {
479509 ' items: for & item in assoc_items {
480510 let attrs = item_tree. attrs ( self . db , self . module_id . krate , ModItem :: from ( item) . into ( ) ) ;
481511 if !attrs. is_cfg_enabled ( self . expander . cfg_options ( ) ) {
512+ self . inactive_diagnostics . push ( DefDiagnostic :: unconfigured_code (
513+ self . module_id . local_id ,
514+ InFile :: new ( self . expander . current_file_id ( ) , item. ast_id ( & item_tree) . upcast ( ) ) ,
515+ attrs. cfg ( ) . unwrap ( ) ,
516+ self . expander . cfg_options ( ) . clone ( ) ,
517+ ) ) ;
482518 continue ;
483519 }
484520
0 commit comments