3
3
use std:: { collections:: hash_map:: Entry , mem, sync:: Arc } ;
4
4
5
5
use hir_expand:: { ast_id_map:: AstIdMap , hygiene:: Hygiene , name:: known, HirFileId } ;
6
- use syntax:: {
7
- ast:: { self , HasModuleItem } ,
8
- SyntaxNode , WalkEvent ,
9
- } ;
6
+ use syntax:: ast:: { self , HasModuleItem } ;
10
7
11
8
use crate :: {
12
9
generics:: { GenericParams , TypeParamData , TypeParamProvenance } ,
@@ -42,7 +39,7 @@ impl<'a> Ctx<'a> {
42
39
43
40
pub ( super ) fn lower_module_items ( mut self , item_owner : & dyn HasModuleItem ) -> ItemTree {
44
41
self . tree . top_level =
45
- item_owner. items ( ) . flat_map ( |item| self . lower_mod_item ( & item, false ) ) . collect ( ) ;
42
+ item_owner. items ( ) . flat_map ( |item| self . lower_mod_item ( & item) ) . collect ( ) ;
46
43
self . tree
47
44
}
48
45
@@ -62,63 +59,35 @@ impl<'a> Ctx<'a> {
62
59
} ,
63
60
_ => None ,
64
61
} )
65
- . flat_map ( |item| self . lower_mod_item ( & item, false ) )
62
+ . flat_map ( |item| self . lower_mod_item ( & item) )
66
63
. collect ( ) ;
67
64
68
- // Non-items need to have their inner items collected.
69
- for stmt in stmts. statements ( ) {
70
- match stmt {
71
- ast:: Stmt :: ExprStmt ( _) | ast:: Stmt :: LetStmt ( _) => {
72
- self . collect_inner_items ( stmt. syntax ( ) )
73
- }
74
- _ => { }
75
- }
76
- }
77
- if let Some ( expr) = stmts. expr ( ) {
78
- self . collect_inner_items ( expr. syntax ( ) ) ;
79
- }
80
65
self . tree
81
66
}
82
67
83
- pub ( super ) fn lower_inner_items ( mut self , within : & SyntaxNode ) -> ItemTree {
84
- self . collect_inner_items ( within) ;
68
+ pub ( super ) fn lower_block ( mut self , block : & ast:: BlockExpr ) -> ItemTree {
69
+ self . tree . top_level = block
70
+ . statements ( )
71
+ . filter_map ( |stmt| match stmt {
72
+ ast:: Stmt :: Item ( item) => self . lower_mod_item ( & item) ,
73
+ // Macro calls can be both items and expressions. The syntax library always treats
74
+ // them as expressions here, so we undo that.
75
+ ast:: Stmt :: ExprStmt ( es) => match es. expr ( ) ? {
76
+ ast:: Expr :: MacroCall ( call) => self . lower_mod_item ( & call. into ( ) ) ,
77
+ _ => None ,
78
+ } ,
79
+ _ => None ,
80
+ } )
81
+ . collect ( ) ;
82
+
85
83
self . tree
86
84
}
87
85
88
86
fn data ( & mut self ) -> & mut ItemTreeData {
89
87
self . tree . data_mut ( )
90
88
}
91
89
92
- fn lower_mod_item ( & mut self , item : & ast:: Item , inner : bool ) -> Option < ModItem > {
93
- // Collect inner items for 1-to-1-lowered items.
94
- match item {
95
- ast:: Item :: Struct ( _)
96
- | ast:: Item :: Union ( _)
97
- | ast:: Item :: Enum ( _)
98
- | ast:: Item :: Fn ( _)
99
- | ast:: Item :: TypeAlias ( _)
100
- | ast:: Item :: Const ( _)
101
- | ast:: Item :: Static ( _) => {
102
- // Skip this if we're already collecting inner items. We'll descend into all nodes
103
- // already.
104
- if !inner {
105
- self . collect_inner_items ( item. syntax ( ) ) ;
106
- }
107
- }
108
-
109
- // These are handled in their respective `lower_X` method (since we can't just blindly
110
- // walk them).
111
- ast:: Item :: Trait ( _) | ast:: Item :: Impl ( _) | ast:: Item :: ExternBlock ( _) => { }
112
-
113
- // These don't have inner items.
114
- ast:: Item :: Module ( _)
115
- | ast:: Item :: ExternCrate ( _)
116
- | ast:: Item :: Use ( _)
117
- | ast:: Item :: MacroCall ( _)
118
- | ast:: Item :: MacroRules ( _)
119
- | ast:: Item :: MacroDef ( _) => { }
120
- } ;
121
-
90
+ fn lower_mod_item ( & mut self , item : & ast:: Item ) -> Option < ModItem > {
122
91
let attrs = RawAttrs :: new ( self . db , item, & self . hygiene ) ;
123
92
let item: ModItem = match item {
124
93
ast:: Item :: Struct ( ast) => self . lower_struct ( ast) ?. into ( ) ,
@@ -155,47 +124,6 @@ impl<'a> Ctx<'a> {
155
124
}
156
125
}
157
126
158
- fn collect_inner_items ( & mut self , container : & SyntaxNode ) {
159
- let forced_vis = self . forced_visibility . take ( ) ;
160
-
161
- let mut block_stack = Vec :: new ( ) ;
162
-
163
- // if container itself is block, add it to the stack
164
- if let Some ( block) = ast:: BlockExpr :: cast ( container. clone ( ) ) {
165
- block_stack. push ( self . source_ast_id_map . ast_id ( & block) ) ;
166
- }
167
-
168
- for event in container. preorder ( ) . skip ( 1 ) {
169
- match event {
170
- WalkEvent :: Enter ( node) => {
171
- match_ast ! {
172
- match node {
173
- ast:: BlockExpr ( block) => {
174
- block_stack. push( self . source_ast_id_map. ast_id( & block) ) ;
175
- } ,
176
- ast:: Item ( item) => {
177
- // FIXME: This triggers for macro calls in expression/pattern/type position
178
- let mod_item = self . lower_mod_item( & item, true ) ;
179
- let current_block = block_stack. last( ) ;
180
- if let ( Some ( mod_item) , Some ( block) ) = ( mod_item, current_block) {
181
- self . data( ) . inner_items. entry( * block) . or_default( ) . push( mod_item) ;
182
- }
183
- } ,
184
- _ => { }
185
- }
186
- }
187
- }
188
- WalkEvent :: Leave ( node) => {
189
- if ast:: BlockExpr :: cast ( node) . is_some ( ) {
190
- block_stack. pop ( ) ;
191
- }
192
- }
193
- }
194
- }
195
-
196
- self . forced_visibility = forced_vis;
197
- }
198
-
199
127
fn lower_assoc_item ( & mut self , item : & ast:: AssocItem ) -> Option < AssocItem > {
200
128
match item {
201
129
ast:: AssocItem :: Fn ( ast) => self . lower_function ( ast) . map ( Into :: into) ,
@@ -470,9 +398,7 @@ impl<'a> Ctx<'a> {
470
398
ModKind :: Inline {
471
399
items : module
472
400
. item_list ( )
473
- . map ( |list| {
474
- list. items ( ) . flat_map ( |item| self . lower_mod_item ( & item, false ) ) . collect ( )
475
- } )
401
+ . map ( |list| list. items ( ) . flat_map ( |item| self . lower_mod_item ( & item) ) . collect ( ) )
476
402
. unwrap_or_else ( || {
477
403
cov_mark:: hit!( name_res_works_for_broken_modules) ;
478
404
Box :: new ( [ ] ) as Box < [ _ ] >
@@ -487,8 +413,7 @@ impl<'a> Ctx<'a> {
487
413
fn lower_trait ( & mut self , trait_def : & ast:: Trait ) -> Option < FileItemTreeId < Trait > > {
488
414
let name = trait_def. name ( ) ?. as_name ( ) ;
489
415
let visibility = self . lower_visibility ( trait_def) ;
490
- let generic_params =
491
- self . lower_generic_params_and_inner_items ( GenericsOwner :: Trait ( trait_def) , trait_def) ;
416
+ let generic_params = self . lower_generic_params ( GenericsOwner :: Trait ( trait_def) , trait_def) ;
492
417
let is_auto = trait_def. auto_token ( ) . is_some ( ) ;
493
418
let is_unsafe = trait_def. unsafe_token ( ) . is_some ( ) ;
494
419
let items = trait_def. assoc_item_list ( ) . map ( |list| {
@@ -497,7 +422,6 @@ impl<'a> Ctx<'a> {
497
422
list. assoc_items ( )
498
423
. filter_map ( |item| {
499
424
let attrs = RawAttrs :: new ( db, & item, & this. hygiene ) ;
500
- this. collect_inner_items ( item. syntax ( ) ) ;
501
425
this. lower_assoc_item ( & item) . map ( |item| {
502
426
this. add_attrs ( ModItem :: from ( item) . into ( ) , attrs) ;
503
427
item
@@ -520,8 +444,7 @@ impl<'a> Ctx<'a> {
520
444
}
521
445
522
446
fn lower_impl ( & mut self , impl_def : & ast:: Impl ) -> Option < FileItemTreeId < Impl > > {
523
- let generic_params =
524
- self . lower_generic_params_and_inner_items ( GenericsOwner :: Impl , impl_def) ;
447
+ let generic_params = self . lower_generic_params ( GenericsOwner :: Impl , impl_def) ;
525
448
// FIXME: If trait lowering fails, due to a non PathType for example, we treat this impl
526
449
// as if it was an non-trait impl. Ideally we want to create a unique missing ref that only
527
450
// equals itself.
@@ -535,7 +458,6 @@ impl<'a> Ctx<'a> {
535
458
. into_iter ( )
536
459
. flat_map ( |it| it. assoc_items ( ) )
537
460
. filter_map ( |item| {
538
- self . collect_inner_items ( item. syntax ( ) ) ;
539
461
let assoc = self . lower_assoc_item ( & item) ?;
540
462
let attrs = RawAttrs :: new ( self . db , & item, & self . hygiene ) ;
541
463
self . add_attrs ( ModItem :: from ( assoc) . into ( ) , attrs) ;
@@ -603,7 +525,6 @@ impl<'a> Ctx<'a> {
603
525
let children: Box < [ _ ] > = block. extern_item_list ( ) . map_or ( Box :: new ( [ ] ) , |list| {
604
526
list. extern_items ( )
605
527
. filter_map ( |item| {
606
- self . collect_inner_items ( item. syntax ( ) ) ;
607
528
let attrs = RawAttrs :: new ( self . db , & item, & self . hygiene ) ;
608
529
let id: ModItem = match item {
609
530
ast:: ExternItem :: Fn ( ast) => {
@@ -641,23 +562,6 @@ impl<'a> Ctx<'a> {
641
562
id ( self . data ( ) . extern_blocks . alloc ( res) )
642
563
}
643
564
644
- /// Lowers generics defined on `node` and collects inner items defined within.
645
- fn lower_generic_params_and_inner_items (
646
- & mut self ,
647
- owner : GenericsOwner < ' _ > ,
648
- node : & dyn ast:: HasGenericParams ,
649
- ) -> Interned < GenericParams > {
650
- // Generics are part of item headers and may contain inner items we need to collect.
651
- if let Some ( params) = node. generic_param_list ( ) {
652
- self . collect_inner_items ( params. syntax ( ) ) ;
653
- }
654
- if let Some ( clause) = node. where_clause ( ) {
655
- self . collect_inner_items ( clause. syntax ( ) ) ;
656
- }
657
-
658
- self . lower_generic_params ( owner, node)
659
- }
660
-
661
565
fn lower_generic_params (
662
566
& mut self ,
663
567
owner : GenericsOwner < ' _ > ,
0 commit comments