@@ -51,6 +51,18 @@ impl<'a> Module<'a> {
51
51
}
52
52
}
53
53
54
+ pub ( crate ) fn from_item ( item : & ' a ast:: Item ) -> Module < ' a > {
55
+ let mod_kind = match & item. kind {
56
+ ast:: ItemKind :: Mod ( _, mod_kind) => Some ( mod_kind) ,
57
+ _ => None ,
58
+ } ;
59
+ let items = match & item. kind {
60
+ ast:: ItemKind :: Mod ( _, ast:: ModKind :: Loaded ( items, ..) ) => & * * items,
61
+ _ => & [ ] ,
62
+ } ;
63
+ Module :: new ( item. span , mod_kind, items, & item. attrs )
64
+ }
65
+
54
66
pub ( crate ) fn attrs ( & self ) -> & [ ast:: Attribute ] {
55
67
& self . inner_attr
56
68
}
@@ -185,7 +197,7 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
185
197
if is_mod_decl ( item) {
186
198
// mod foo;
187
199
// Look for an extern file.
188
- let Some ( kind) = self . find_external_module ( item. ident , & item . attrs , & sub_mod ) ? else {
200
+ let Some ( kind) = self . find_external_module ( item) ? else {
189
201
return Ok ( ( ) ) ;
190
202
} ;
191
203
self . insert_sub_mod ( kind. clone ( ) ) ?;
@@ -264,19 +276,18 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
264
276
/// Find a file path in the filesystem which corresponds to the given module.
265
277
fn find_external_module (
266
278
& self ,
267
- mod_name : symbol:: Ident ,
268
- attrs : & [ ast:: Attribute ] ,
269
- sub_mod : & Module < ' ast > ,
279
+ item : & ' ast ast:: Item ,
270
280
) -> Result < Option < SubModKind < ' ast > > , ModuleResolutionError > {
281
+ let mod_name = item. ident ;
271
282
let relative = match self . directory . ownership {
272
283
DirectoryOwnership :: Owned { relative } => relative,
273
284
DirectoryOwnership :: UnownedViaBlock => None ,
274
285
} ;
275
- if let Some ( path) = Parser :: submod_path_from_attr ( attrs, & self . directory . path ) {
286
+ if let Some ( path) = Parser :: submod_path_from_attr ( & item . attrs , & self . directory . path ) {
276
287
if self . parse_sess . is_file_parsed ( & path) {
277
288
return Ok ( None ) ;
278
289
}
279
- return match Parser :: parse_file_as_module ( self . parse_sess , & path, sub_mod . span ) {
290
+ return match Parser :: parse_file_as_module ( self . parse_sess , & path, item . span ) {
280
291
Ok ( ( ref attrs, _, _) ) if contains_skip ( attrs) => Ok ( None ) ,
281
292
Ok ( ( attrs, items, span) ) => Ok ( Some ( SubModKind :: External (
282
293
path,
@@ -300,7 +311,7 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
300
311
}
301
312
302
313
// Look for nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
303
- let mut mods_outside_ast = self . find_mods_outside_of_ast ( attrs , sub_mod ) ;
314
+ let mut mods_outside_ast = self . find_mods_outside_of_ast ( item ) ;
304
315
305
316
match self
306
317
. parse_sess
@@ -320,12 +331,16 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
320
331
return Ok ( None ) ;
321
332
} else {
322
333
if should_insert {
323
- mods_outside_ast. push ( ( file_path, dir_ownership, sub_mod. clone ( ) ) ) ;
334
+ mods_outside_ast. push ( (
335
+ file_path,
336
+ dir_ownership,
337
+ Module :: from_item ( item) ,
338
+ ) ) ;
324
339
}
325
340
return Ok ( Some ( SubModKind :: MultiExternal ( mods_outside_ast) ) ) ;
326
341
}
327
342
}
328
- match Parser :: parse_file_as_module ( self . parse_sess , & file_path, sub_mod . span ) {
343
+ match Parser :: parse_file_as_module ( self . parse_sess , & file_path, item . span ) {
329
344
Ok ( ( ref attrs, _, _) ) if contains_skip ( attrs) => Ok ( None ) ,
330
345
Ok ( ( attrs, items, span) ) if outside_mods_empty => {
331
346
Ok ( Some ( SubModKind :: External (
@@ -351,7 +366,11 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
351
366
) ,
352
367
) ) ;
353
368
if should_insert {
354
- mods_outside_ast. push ( ( file_path, dir_ownership, sub_mod. clone ( ) ) ) ;
369
+ mods_outside_ast. push ( (
370
+ file_path,
371
+ dir_ownership,
372
+ Module :: from_item ( item) ,
373
+ ) ) ;
355
374
}
356
375
Ok ( Some ( SubModKind :: MultiExternal ( mods_outside_ast) ) )
357
376
}
@@ -365,7 +384,11 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
365
384
} ) ,
366
385
Err ( ..) => {
367
386
if should_insert {
368
- mods_outside_ast. push ( ( file_path, dir_ownership, sub_mod. clone ( ) ) ) ;
387
+ mods_outside_ast. push ( (
388
+ file_path,
389
+ dir_ownership,
390
+ Module :: from_item ( item) ,
391
+ ) ) ;
369
392
}
370
393
Ok ( Some ( SubModKind :: MultiExternal ( mods_outside_ast) ) )
371
394
}
@@ -446,12 +469,11 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
446
469
447
470
fn find_mods_outside_of_ast (
448
471
& self ,
449
- attrs : & [ ast:: Attribute ] ,
450
- sub_mod : & Module < ' ast > ,
472
+ item : & ' ast ast:: Item ,
451
473
) -> Vec < ( PathBuf , DirectoryOwnership , Module < ' ast > ) > {
452
474
// Filter nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
453
475
let mut path_visitor = visitor:: PathVisitor :: default ( ) ;
454
- for attr in attrs. iter ( ) {
476
+ for attr in item . attrs . iter ( ) {
455
477
if let Some ( meta) = attr. meta ( ) {
456
478
path_visitor. visit_meta_item ( & meta)
457
479
}
@@ -467,12 +489,12 @@ impl<'ast, 'sess> ModResolver<'ast, 'sess> {
467
489
result. push ( (
468
490
actual_path,
469
491
DirectoryOwnership :: Owned { relative : None } ,
470
- sub_mod . clone ( ) ,
492
+ Module :: from_item ( item ) ,
471
493
) ) ;
472
494
continue ;
473
495
}
474
496
let ( attrs, items, span) =
475
- match Parser :: parse_file_as_module ( self . parse_sess , & actual_path, sub_mod . span ) {
497
+ match Parser :: parse_file_as_module ( self . parse_sess , & actual_path, item . span ) {
476
498
Ok ( ( ref attrs, _, _) ) if contains_skip ( attrs) => continue ,
477
499
Ok ( m) => m,
478
500
Err ( ..) => continue ,
0 commit comments