Skip to content

Commit 4ca13b1

Browse files
committed
Add skipped sub module inner attributes to module item attributes
Extends the sub module attribute association to also include inner attributes for external sub modules that are skipped by rustfmt.
1 parent be77631 commit 4ca13b1

File tree

5 files changed

+51
-11
lines changed

5 files changed

+51
-11
lines changed

src/modules.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,17 @@ enum SubModKind<'a, 'ast> {
109109
MultiExternal(Vec<(PathBuf, DirectoryOwnership, Module<'ast>)>),
110110
/// `mod foo {}`
111111
Internal(&'a ast::Item),
112+
/// Just like External, but sub modules of this modules are not resolved,
113+
/// and this module is not added to the FileModMap
114+
SkippedExternal(PathBuf, DirectoryOwnership, Module<'ast>),
112115
}
113116

114117
impl<'a, 'ast> SubModKind<'a, 'ast> {
115118
fn attrs(&self) -> Vec<ast::Attribute> {
116119
match self {
117-
Self::External(_, _, module) => module.inner_attr.clone(),
120+
Self::External(_, _, module) | Self::SkippedExternal(_, _, module) => {
121+
module.inner_attr.clone()
122+
}
118123
Self::MultiExternal(modules) => modules
119124
.iter()
120125
.map(|(_, _, module)| module.inner_attr.clone().into_iter())
@@ -263,10 +268,8 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
263268

264269
let sub_mod_kind = self.peek_sub_mod(item, &sub_mod)?;
265270
if let Some(sub_mod_kind) = sub_mod_kind {
266-
if matches!(
267-
sub_mod_kind,
268-
SubModKind::External(..) | SubModKind::MultiExternal(_)
269-
) {
271+
let is_internal_mod = matches!(sub_mod_kind, SubModKind::Internal(_));
272+
if !is_internal_mod {
270273
self.update_mod_item_with_submod_attrs(item, &sub_mod_kind)
271274
}
272275
self.insert_sub_mod(sub_mod_kind.clone())?;
@@ -305,14 +308,12 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
305308
item: &'c ast::Item,
306309
sub_mod: &Module<'ast>,
307310
) -> Result<Option<SubModKind<'c, 'ast>>, ModuleResolutionError> {
308-
if contains_skip(&item.attrs) {
309-
return Ok(None);
310-
}
311-
312311
if is_mod_decl(item) {
313312
// mod foo;
314313
// Look for an extern file.
315314
self.find_external_module(item.ident, &item.attrs, sub_mod)
315+
} else if contains_skip(&item.attrs) {
316+
return Ok(None);
316317
} else {
317318
// An internal module (`mod foo { /* ... */ }`);
318319
Ok(Some(SubModKind::Internal(item)))
@@ -370,6 +371,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
370371
}
371372
Ok(())
372373
}
374+
SubModKind::SkippedExternal(..) => Ok(()),
373375
}
374376
}
375377

@@ -408,7 +410,18 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
408410
return Ok(None);
409411
}
410412
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.span) {
411-
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
413+
Ok((attrs, items, span)) if contains_skip(&attrs) => {
414+
Ok(Some(SubModKind::SkippedExternal(
415+
path,
416+
DirectoryOwnership::Owned { relative: None },
417+
Module::new(
418+
span,
419+
Some(Cow::Owned(ast::ModKind::Unloaded)),
420+
Cow::Owned(items),
421+
Cow::Owned(attrs),
422+
),
423+
)))
424+
}
412425
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
413426
path,
414427
DirectoryOwnership::Owned { relative: None },
@@ -457,7 +470,18 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
457470
}
458471
}
459472
match Parser::parse_file_as_module(self.parse_sess, &file_path, sub_mod.span) {
460-
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
473+
Ok((attrs, items, span)) if contains_skip(&attrs) => {
474+
Ok(Some(SubModKind::SkippedExternal(
475+
file_path,
476+
dir_ownership,
477+
Module::new(
478+
span,
479+
Some(Cow::Owned(ast::ModKind::Unloaded)),
480+
Cow::Owned(items),
481+
Cow::Owned(attrs),
482+
),
483+
)))
484+
}
461485
Ok((attrs, items, span)) if outside_mods_empty => {
462486
Ok(Some(SubModKind::External(
463487
file_path,

src/modules/test.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::attr::contains_name;
77
use crate::config::{Config, FileName};
88
use crate::parse::parser::{DirectoryOwnership, Parser};
99
use crate::parse::session::ParseSess;
10+
use crate::utils::contains_skip;
1011
use crate::Input;
1112

1213
fn validate_file_mod_map<F: Fn(&FileModMap<'_>)>(mod_path: &PathBuf, recursive: bool, f: F) {
@@ -52,5 +53,15 @@ fn external_sub_module_inner_attrs_are_present_in_mod_item_attrs_list() {
5253

5354
let mod_b = get_submodule(module, "b");
5455
assert!(contains_name(&mod_b.attrs, sym::macro_use));
56+
57+
// mod c is annotated with `#[rustfmt::skip]`, but we should still have access to
58+
// the inner attributes
59+
let mod_c = get_submodule(module, "c");
60+
assert!(contains_name(&mod_c.attrs, sym::macro_use));
61+
assert!(contains_skip(&mod_c.attrs));
62+
63+
// mod d is annotated with an inner `#![rustfmt::skip]` attribute.
64+
let mod_d = get_submodule(module, "d");
65+
assert!(contains_skip(&mod_d.attrs));
5566
});
5667
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#![macro_use]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#![rustfmt::skip]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
mod b;
22
mod a;
3+
#[rustfmt::skip]
4+
mod c;
5+
mod d;

0 commit comments

Comments
 (0)