3
3
pub mod adt;
4
4
5
5
use base_db:: CrateId ;
6
- use hir_expand:: {
7
- name:: Name , AstId , ExpandResult , HirFileId , InFile , MacroCallId , MacroCallKind , MacroDefKind ,
8
- } ;
6
+ use hir_expand:: name:: Name ;
9
7
use intern:: { sym, Symbol } ;
10
8
use la_arena:: { Idx , RawIdx } ;
11
- use smallvec:: SmallVec ;
12
- use syntax:: { ast, Parse } ;
13
9
use triomphe:: Arc ;
14
10
use tt:: iter:: TtElement ;
15
11
16
12
use crate :: {
17
13
db:: DefDatabase ,
18
- expander:: { Expander , Mark } ,
19
- item_tree:: { self , AssocItem , FnFlags , ItemTree , ItemTreeId , MacroCall , ModItem , TreeId } ,
20
- macro_call_as_call_id,
21
- nameres:: {
22
- attr_resolution:: ResolvedAttr ,
23
- diagnostics:: { DefDiagnostic , DefDiagnostics } ,
24
- proc_macro:: { parse_macro_name_and_helper_attrs, ProcMacroKind } ,
25
- DefMap , MacroSubNs ,
26
- } ,
14
+ item_tree:: { self , FnFlags , ModItem } ,
15
+ nameres:: proc_macro:: { parse_macro_name_and_helper_attrs, ProcMacroKind } ,
27
16
path:: ImportAlias ,
28
17
type_ref:: { TraitRef , TypeBound , TypeRefId , TypesMap } ,
29
18
visibility:: RawVisibility ,
30
- AssocItemId , AstIdWithPath , ConstId , ConstLoc , ExternCrateId , FunctionId , FunctionLoc ,
31
- HasModule , ImplId , Intern , ItemContainerId , ItemLoc , Lookup , Macro2Id , MacroRulesId , ModuleId ,
32
- ProcMacroId , StaticId , TraitAliasId , TraitId , TypeAliasId , TypeAliasLoc ,
19
+ ConstId , ExternCrateId , FunctionId , HasModule , ImplId , ItemContainerId , ItemLoc , Lookup ,
20
+ Macro2Id , MacroRulesId , ProcMacroId , StaticId , TraitAliasId , TraitId , TypeAliasId ,
33
21
} ;
34
22
35
23
#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -257,23 +245,13 @@ bitflags::bitflags! {
257
245
#[ derive( Debug , Clone , PartialEq , Eq ) ]
258
246
pub struct TraitData {
259
247
pub name : Name ,
260
- pub items : Box < [ ( Name , AssocItemId ) ] > ,
261
248
pub flags : TraitFlags ,
262
249
pub visibility : RawVisibility ,
263
- // box it as the vec is usually empty anyways
264
- pub macro_calls : Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ,
265
250
}
266
251
267
252
impl TraitData {
268
253
#[ inline]
269
254
pub ( crate ) fn trait_data_query ( db : & dyn DefDatabase , tr : TraitId ) -> Arc < TraitData > {
270
- db. trait_data_with_diagnostics ( tr) . 0
271
- }
272
-
273
- pub ( crate ) fn trait_data_with_diagnostics_query (
274
- db : & dyn DefDatabase ,
275
- tr : TraitId ,
276
- ) -> ( Arc < TraitData > , DefDiagnostics ) {
277
255
let ItemLoc { container : module_id, id : tree_id } = tr. lookup ( db) ;
278
256
let item_tree = tree_id. item_tree ( db) ;
279
257
let tr_def = & item_tree[ tree_id. value ] ;
@@ -318,40 +296,7 @@ impl TraitData {
318
296
flags |= TraitFlags :: SKIP_BOXED_SLICE_DURING_METHOD_DISPATCH ;
319
297
}
320
298
321
- let mut collector =
322
- AssocItemCollector :: new ( db, module_id, tree_id. file_id ( ) , ItemContainerId :: TraitId ( tr) ) ;
323
- collector. collect ( & item_tree, tree_id. tree_id ( ) , & tr_def. items ) ;
324
- let ( items, macro_calls, diagnostics) = collector. finish ( ) ;
325
-
326
- (
327
- Arc :: new ( TraitData { name, macro_calls, items, visibility, flags } ) ,
328
- DefDiagnostics :: new ( diagnostics) ,
329
- )
330
- }
331
-
332
- pub fn associated_types ( & self ) -> impl Iterator < Item = TypeAliasId > + ' _ {
333
- self . items . iter ( ) . filter_map ( |( _name, item) | match item {
334
- AssocItemId :: TypeAliasId ( t) => Some ( * t) ,
335
- _ => None ,
336
- } )
337
- }
338
-
339
- pub fn associated_type_by_name ( & self , name : & Name ) -> Option < TypeAliasId > {
340
- self . items . iter ( ) . find_map ( |( item_name, item) | match item {
341
- AssocItemId :: TypeAliasId ( t) if item_name == name => Some ( * t) ,
342
- _ => None ,
343
- } )
344
- }
345
-
346
- pub fn method_by_name ( & self , name : & Name ) -> Option < FunctionId > {
347
- self . items . iter ( ) . find_map ( |( item_name, item) | match item {
348
- AssocItemId :: FunctionId ( t) if item_name == name => Some ( * t) ,
349
- _ => None ,
350
- } )
351
- }
352
-
353
- pub fn attribute_calls ( & self ) -> impl Iterator < Item = ( AstId < ast:: Item > , MacroCallId ) > + ' _ {
354
- self . macro_calls . iter ( ) . flat_map ( |it| it. iter ( ) ) . copied ( )
299
+ Arc :: new ( TraitData { name, visibility, flags } )
355
300
}
356
301
}
357
302
@@ -376,26 +321,16 @@ impl TraitAliasData {
376
321
pub struct ImplData {
377
322
pub target_trait : Option < TraitRef > ,
378
323
pub self_ty : TypeRefId ,
379
- pub items : Box < [ ( Name , AssocItemId ) ] > ,
380
324
pub is_negative : bool ,
381
325
pub is_unsafe : bool ,
382
- // box it as the vec is usually empty anyways
383
- pub macro_calls : Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ,
384
326
pub types_map : Arc < TypesMap > ,
385
327
}
386
328
387
329
impl ImplData {
388
330
#[ inline]
389
331
pub ( crate ) fn impl_data_query ( db : & dyn DefDatabase , id : ImplId ) -> Arc < ImplData > {
390
- db. impl_data_with_diagnostics ( id) . 0
391
- }
392
-
393
- pub ( crate ) fn impl_data_with_diagnostics_query (
394
- db : & dyn DefDatabase ,
395
- id : ImplId ,
396
- ) -> ( Arc < ImplData > , DefDiagnostics ) {
397
- let _p = tracing:: info_span!( "impl_data_with_diagnostics_query" ) . entered ( ) ;
398
- let ItemLoc { container : module_id, id : tree_id } = id. lookup ( db) ;
332
+ let _p = tracing:: info_span!( "impl_data_query" ) . entered ( ) ;
333
+ let ItemLoc { id : tree_id, .. } = id. lookup ( db) ;
399
334
400
335
let item_tree = tree_id. item_tree ( db) ;
401
336
let impl_def = & item_tree[ tree_id. value ] ;
@@ -404,28 +339,13 @@ impl ImplData {
404
339
let is_negative = impl_def. is_negative ;
405
340
let is_unsafe = impl_def. is_unsafe ;
406
341
407
- let mut collector =
408
- AssocItemCollector :: new ( db, module_id, tree_id. file_id ( ) , ItemContainerId :: ImplId ( id) ) ;
409
- collector. collect ( & item_tree, tree_id. tree_id ( ) , & impl_def. items ) ;
410
-
411
- let ( items, macro_calls, diagnostics) = collector. finish ( ) ;
412
-
413
- (
414
- Arc :: new ( ImplData {
415
- target_trait,
416
- self_ty,
417
- items,
418
- is_negative,
419
- is_unsafe,
420
- macro_calls,
421
- types_map : impl_def. types_map . clone ( ) ,
422
- } ) ,
423
- DefDiagnostics :: new ( diagnostics) ,
424
- )
425
- }
426
-
427
- pub fn attribute_calls ( & self ) -> impl Iterator < Item = ( AstId < ast:: Item > , MacroCallId ) > + ' _ {
428
- self . macro_calls . iter ( ) . flat_map ( |it| it. iter ( ) ) . copied ( )
342
+ Arc :: new ( ImplData {
343
+ target_trait,
344
+ self_ty,
345
+ is_negative,
346
+ is_unsafe,
347
+ types_map : impl_def. types_map . clone ( ) ,
348
+ } )
429
349
}
430
350
}
431
351
@@ -629,212 +549,6 @@ impl StaticData {
629
549
}
630
550
}
631
551
632
- struct AssocItemCollector < ' a > {
633
- db : & ' a dyn DefDatabase ,
634
- module_id : ModuleId ,
635
- def_map : Arc < DefMap > ,
636
- diagnostics : Vec < DefDiagnostic > ,
637
- container : ItemContainerId ,
638
- expander : Expander ,
639
-
640
- items : Vec < ( Name , AssocItemId ) > ,
641
- macro_calls : Vec < ( AstId < ast:: Item > , MacroCallId ) > ,
642
- }
643
-
644
- impl < ' a > AssocItemCollector < ' a > {
645
- fn new (
646
- db : & ' a dyn DefDatabase ,
647
- module_id : ModuleId ,
648
- file_id : HirFileId ,
649
- container : ItemContainerId ,
650
- ) -> Self {
651
- Self {
652
- db,
653
- module_id,
654
- def_map : module_id. def_map ( db) ,
655
- container,
656
- expander : Expander :: new ( db, file_id, module_id) ,
657
- items : Vec :: new ( ) ,
658
- macro_calls : Vec :: new ( ) ,
659
- diagnostics : Vec :: new ( ) ,
660
- }
661
- }
662
-
663
- fn finish (
664
- self ,
665
- ) -> (
666
- Box < [ ( Name , AssocItemId ) ] > ,
667
- Option < Box < Vec < ( AstId < ast:: Item > , MacroCallId ) > > > ,
668
- Vec < DefDiagnostic > ,
669
- ) {
670
- (
671
- self . items . into_boxed_slice ( ) ,
672
- if self . macro_calls . is_empty ( ) { None } else { Some ( Box :: new ( self . macro_calls ) ) } ,
673
- self . diagnostics ,
674
- )
675
- }
676
-
677
- fn collect ( & mut self , item_tree : & ItemTree , tree_id : TreeId , assoc_items : & [ AssocItem ] ) {
678
- let container = self . container ;
679
- self . items . reserve ( assoc_items. len ( ) ) ;
680
-
681
- ' items: for & item in assoc_items {
682
- let attrs = item_tree. attrs ( self . db , self . module_id . krate , ModItem :: from ( item) . into ( ) ) ;
683
- if !attrs. is_cfg_enabled ( self . expander . cfg_options ( ) ) {
684
- self . diagnostics . push ( DefDiagnostic :: unconfigured_code (
685
- self . module_id . local_id ,
686
- tree_id,
687
- ModItem :: from ( item) . into ( ) ,
688
- attrs. cfg ( ) . unwrap ( ) ,
689
- self . expander . cfg_options ( ) . clone ( ) ,
690
- ) ) ;
691
- continue ;
692
- }
693
-
694
- ' attrs: for attr in & * attrs {
695
- let ast_id =
696
- AstId :: new ( self . expander . current_file_id ( ) , item. ast_id ( item_tree) . upcast ( ) ) ;
697
- let ast_id_with_path = AstIdWithPath { path : attr. path . clone ( ) , ast_id } ;
698
-
699
- match self . def_map . resolve_attr_macro (
700
- self . db ,
701
- self . module_id . local_id ,
702
- ast_id_with_path,
703
- attr,
704
- ) {
705
- Ok ( ResolvedAttr :: Macro ( call_id) ) => {
706
- let loc = self . db . lookup_intern_macro_call ( call_id) ;
707
- if let MacroDefKind :: ProcMacro ( _, exp, _) = loc. def . kind {
708
- // If there's no expander for the proc macro (e.g. the
709
- // proc macro is ignored, or building the proc macro
710
- // crate failed), skip expansion like we would if it was
711
- // disabled. This is analogous to the handling in
712
- // `DefCollector::collect_macros`.
713
- if let Some ( err) = exp. as_expand_error ( self . module_id . krate ) {
714
- self . diagnostics . push ( DefDiagnostic :: macro_error (
715
- self . module_id . local_id ,
716
- ast_id,
717
- ( * attr. path ) . clone ( ) ,
718
- err,
719
- ) ) ;
720
- continue ' attrs;
721
- }
722
- }
723
-
724
- self . macro_calls . push ( ( ast_id, call_id) ) ;
725
- let res =
726
- self . expander . enter_expand_id :: < ast:: MacroItems > ( self . db , call_id) ;
727
- self . collect_macro_items ( res) ;
728
- continue ' items;
729
- }
730
- Ok ( _) => ( ) ,
731
- Err ( _) => {
732
- self . diagnostics . push ( DefDiagnostic :: unresolved_macro_call (
733
- self . module_id . local_id ,
734
- MacroCallKind :: Attr {
735
- ast_id,
736
- attr_args : None ,
737
- invoc_attr_index : attr. id ,
738
- } ,
739
- attr. path ( ) . clone ( ) ,
740
- ) ) ;
741
- }
742
- }
743
- }
744
-
745
- self . collect_item ( item_tree, tree_id, container, item) ;
746
- }
747
- }
748
-
749
- fn collect_item (
750
- & mut self ,
751
- item_tree : & ItemTree ,
752
- tree_id : TreeId ,
753
- container : ItemContainerId ,
754
- item : AssocItem ,
755
- ) {
756
- match item {
757
- AssocItem :: Function ( id) => {
758
- let item = & item_tree[ id] ;
759
- let def =
760
- FunctionLoc { container, id : ItemTreeId :: new ( tree_id, id) } . intern ( self . db ) ;
761
- self . items . push ( ( item. name . clone ( ) , def. into ( ) ) ) ;
762
- }
763
- AssocItem :: TypeAlias ( id) => {
764
- let item = & item_tree[ id] ;
765
- let def =
766
- TypeAliasLoc { container, id : ItemTreeId :: new ( tree_id, id) } . intern ( self . db ) ;
767
- self . items . push ( ( item. name . clone ( ) , def. into ( ) ) ) ;
768
- }
769
- AssocItem :: Const ( id) => {
770
- let item = & item_tree[ id] ;
771
- let Some ( name) = item. name . clone ( ) else { return } ;
772
- let def = ConstLoc { container, id : ItemTreeId :: new ( tree_id, id) } . intern ( self . db ) ;
773
- self . items . push ( ( name, def. into ( ) ) ) ;
774
- }
775
- AssocItem :: MacroCall ( call) => {
776
- let file_id = self . expander . current_file_id ( ) ;
777
- let MacroCall { ast_id, expand_to, ctxt, ref path } = item_tree[ call] ;
778
- let module = self . expander . module . local_id ;
779
-
780
- let resolver = |path : & _ | {
781
- self . def_map
782
- . resolve_path (
783
- self . db ,
784
- module,
785
- path,
786
- crate :: item_scope:: BuiltinShadowMode :: Other ,
787
- Some ( MacroSubNs :: Bang ) ,
788
- )
789
- . 0
790
- . take_macros ( )
791
- . map ( |it| self . db . macro_def ( it) )
792
- } ;
793
- match macro_call_as_call_id (
794
- self . db . upcast ( ) ,
795
- & AstIdWithPath :: new ( file_id, ast_id, Clone :: clone ( path) ) ,
796
- ctxt,
797
- expand_to,
798
- self . expander . krate ( ) ,
799
- resolver,
800
- ) {
801
- Ok ( Some ( call_id) ) => {
802
- let res =
803
- self . expander . enter_expand_id :: < ast:: MacroItems > ( self . db , call_id) ;
804
- self . macro_calls . push ( ( InFile :: new ( file_id, ast_id. upcast ( ) ) , call_id) ) ;
805
- self . collect_macro_items ( res) ;
806
- }
807
- Ok ( None ) => ( ) ,
808
- Err ( _) => {
809
- self . diagnostics . push ( DefDiagnostic :: unresolved_macro_call (
810
- self . module_id . local_id ,
811
- MacroCallKind :: FnLike {
812
- ast_id : InFile :: new ( file_id, ast_id) ,
813
- expand_to,
814
- eager : None ,
815
- } ,
816
- Clone :: clone ( path) ,
817
- ) ) ;
818
- }
819
- }
820
- }
821
- }
822
- }
823
-
824
- fn collect_macro_items ( & mut self , res : ExpandResult < Option < ( Mark , Parse < ast:: MacroItems > ) > > ) {
825
- let Some ( ( mark, _parse) ) = res. value else { return } ;
826
-
827
- let tree_id = item_tree:: TreeId :: new ( self . expander . current_file_id ( ) , None ) ;
828
- let item_tree = tree_id. item_tree ( self . db ) ;
829
- let iter: SmallVec < [ _ ; 2 ] > =
830
- item_tree. top_level_items ( ) . iter ( ) . filter_map ( ModItem :: as_assoc_item) . collect ( ) ;
831
-
832
- self . collect ( & item_tree, tree_id, & iter) ;
833
-
834
- self . expander . exit ( mark) ;
835
- }
836
- }
837
-
838
552
fn trait_vis ( db : & dyn DefDatabase , trait_id : TraitId ) -> RawVisibility {
839
553
let ItemLoc { id : tree_id, .. } = trait_id. lookup ( db) ;
840
554
let item_tree = tree_id. item_tree ( db) ;
0 commit comments