@@ -3,6 +3,7 @@ use std::collections::BTreeMap;
3
3
use std:: path:: { Path , PathBuf } ;
4
4
5
5
use rustc_ast:: ast;
6
+ use rustc_ast:: attr:: HasAttrs ;
6
7
use rustc_ast:: visit:: Visitor ;
7
8
use rustc_span:: symbol:: { self , sym, Symbol } ;
8
9
use thiserror:: Error ;
@@ -31,6 +32,29 @@ pub(crate) struct Module<'a> {
31
32
inner_attr : Vec < ast:: Attribute > ,
32
33
}
33
34
35
+ impl < ' a > Module < ' a > {
36
+ pub ( crate ) fn new ( ast_mod : Cow < ' a , ast:: Mod > , attrs : & [ ast:: Attribute ] ) -> Self {
37
+ let inner_attr = attrs
38
+ . iter ( )
39
+ . filter ( |attr| attr. style == ast:: AttrStyle :: Inner )
40
+ . cloned ( )
41
+ . collect ( ) ;
42
+ Module {
43
+ ast_mod,
44
+ inner_attr,
45
+ }
46
+ }
47
+ }
48
+
49
+ impl < ' a > HasAttrs for Module < ' a > {
50
+ fn attrs ( & self ) -> & [ ast:: Attribute ] {
51
+ & self . inner_attr
52
+ }
53
+ fn visit_attrs ( & mut self , f : impl FnOnce ( & mut Vec < ast:: Attribute > ) ) {
54
+ f ( & mut self . inner_attr )
55
+ }
56
+ }
57
+
34
58
impl < ' a > AsRef < ast:: Mod > for Module < ' a > {
35
59
fn as_ref ( & self ) -> & ast:: Mod {
36
60
& self . ast_mod
@@ -109,10 +133,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
109
133
110
134
self . file_map . insert (
111
135
root_filename,
112
- Module {
113
- ast_mod : Cow :: Borrowed ( & krate. module ) ,
114
- inner_attr : krate. attrs . clone ( ) ,
115
- } ,
136
+ Module :: new ( Cow :: Borrowed ( & krate. module ) , & krate. attrs ) ,
116
137
) ;
117
138
Ok ( self . file_map )
118
139
}
@@ -125,10 +146,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
125
146
if let ast:: ItemKind :: Mod ( ref sub_mod) = module_item. item . kind {
126
147
self . visit_sub_mod (
127
148
& module_item. item ,
128
- Module {
129
- ast_mod : Cow :: Owned ( sub_mod. clone ( ) ) ,
130
- inner_attr : module_item. item . attrs . clone ( ) ,
131
- } ,
149
+ Module :: new ( Cow :: Owned ( sub_mod. clone ( ) ) , & module_item. item . attrs ) ,
132
150
) ?;
133
151
}
134
152
}
@@ -144,13 +162,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
144
162
}
145
163
146
164
if let ast:: ItemKind :: Mod ( ref sub_mod) = item. kind {
147
- self . visit_sub_mod (
148
- & item,
149
- Module {
150
- ast_mod : Cow :: Owned ( sub_mod. clone ( ) ) ,
151
- inner_attr : item. attrs . clone ( ) ,
152
- } ,
153
- ) ?;
165
+ self . visit_sub_mod ( & item, Module :: new ( Cow :: Owned ( sub_mod. clone ( ) ) , & item. attrs ) ) ?;
154
166
}
155
167
}
156
168
Ok ( ( ) )
@@ -164,13 +176,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
164
176
}
165
177
166
178
if let ast:: ItemKind :: Mod ( ref sub_mod) = item. kind {
167
- self . visit_sub_mod (
168
- item,
169
- Module {
170
- ast_mod : Cow :: Borrowed ( sub_mod) ,
171
- inner_attr : item. attrs . clone ( ) ,
172
- } ,
173
- ) ?;
179
+ self . visit_sub_mod ( item, Module :: new ( Cow :: Borrowed ( sub_mod) , & item. attrs ) ) ?;
174
180
}
175
181
}
176
182
Ok ( ( ) )
@@ -297,10 +303,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
297
303
Ok ( m) => Ok ( Some ( SubModKind :: External (
298
304
path,
299
305
DirectoryOwnership :: Owned { relative : None } ,
300
- Module {
301
- ast_mod : Cow :: Owned ( m. 0 ) ,
302
- inner_attr : m. 1 ,
303
- } ,
306
+ Module :: new ( Cow :: Owned ( m. 0 ) , & m. 1 ) ,
304
307
) ) ) ,
305
308
Err ( ParserError :: ParseError ) => Err ( ModuleResolutionError {
306
309
module : mod_name. to_string ( ) ,
@@ -342,19 +345,13 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
342
345
Ok ( m) if outside_mods_empty => Ok ( Some ( SubModKind :: External (
343
346
path,
344
347
ownership,
345
- Module {
346
- ast_mod : Cow :: Owned ( m. 0 ) ,
347
- inner_attr : m. 1 ,
348
- } ,
348
+ Module :: new ( Cow :: Owned ( m. 0 ) , & m. 1 ) ,
349
349
) ) ) ,
350
350
Ok ( m) => {
351
351
mods_outside_ast. push ( (
352
352
path. clone ( ) ,
353
353
ownership,
354
- Module {
355
- ast_mod : Cow :: Owned ( m. 0 ) ,
356
- inner_attr : m. 1 ,
357
- } ,
354
+ Module :: new ( Cow :: Owned ( m. 0 ) , & m. 1 ) ,
358
355
) ) ;
359
356
if should_insert {
360
357
mods_outside_ast. push ( ( path, ownership, sub_mod. clone ( ) ) ) ;
@@ -457,10 +454,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
457
454
result. push ( (
458
455
actual_path,
459
456
DirectoryOwnership :: Owned { relative : None } ,
460
- Module {
461
- ast_mod : Cow :: Owned ( m. 0 ) ,
462
- inner_attr : m. 1 ,
463
- } ,
457
+ Module :: new ( Cow :: Owned ( m. 0 ) , & m. 1 ) ,
464
458
) )
465
459
}
466
460
result
0 commit comments