Skip to content

Commit ad5d9fb

Browse files
calebcartwrighttopecongiro
authored andcommitted
fix formatting mods inside cfg_if macro (#3763)
1 parent f800ce4 commit ad5d9fb

File tree

15 files changed

+114
-5
lines changed

15 files changed

+114
-5
lines changed

src/modules.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
105105
visitor.visit_item(&item);
106106
for module_item in visitor.mods() {
107107
if let ast::ItemKind::Mod(ref sub_mod) = module_item.item.node {
108-
self.visit_sub_mod(&item, Cow::Owned(sub_mod.clone()))?;
108+
self.visit_sub_mod(&module_item.item, Cow::Owned(sub_mod.clone()))?;
109109
}
110110
}
111111
Ok(())
@@ -477,7 +477,14 @@ fn parse_mod_items<'a>(parser: &mut parser::Parser<'a>, inner_lo: Span) -> PResu
477477

478478
fn is_cfg_if(item: &ast::Item) -> bool {
479479
match item.node {
480-
ast::ItemKind::Mac(..) if item.ident.name == Symbol::intern("cfg_if") => true,
480+
ast::ItemKind::Mac(ref mac) => {
481+
if let Some(first_segment) = mac.node.path.segments.first() {
482+
if first_segment.ident.name == Symbol::intern("cfg_if") {
483+
return true;
484+
}
485+
}
486+
false
487+
}
481488
_ => false,
482489
}
483490
}

src/modules/visitor.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,27 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CfgIfVisitor<'a> {
4343

4444
impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
4545
fn visit_mac_inner(&mut self, mac: &'ast ast::Mac) -> Result<(), &'static str> {
46-
if mac.node.path != Symbol::intern("cfg_if") {
47-
return Err("Expected cfg_if");
48-
}
46+
// Support both:
47+
// ```
48+
// extern crate cfg_if;
49+
// cfg_if::cfg_if! {..}
50+
// ```
51+
// And:
52+
// ```
53+
// #[macro_use]
54+
// extern crate cfg_if;
55+
// cfg_if! {..}
56+
// ```
57+
match mac.node.path.segments.first() {
58+
Some(first_segment) => {
59+
if first_segment.ident.name != Symbol::intern("cfg_if") {
60+
return Err("Expected cfg_if");
61+
}
62+
}
63+
None => {
64+
return Err("Expected cfg_if");
65+
}
66+
};
4967

5068
let mut parser = stream_to_parser_with_base_dir(
5169
self.parse_sess,

src/test/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const SKIP_FILE_WHITE_LIST: &[&str] = &[
3030
// These files and directory are a part of modules defined inside `cfg_if!`.
3131
"cfg_if/mod.rs",
3232
"cfg_if/detect",
33+
"issue-3253/foo.rs",
34+
"issue-3253/bar.rs",
35+
"issue-3253/paths",
3336
// These files and directory are a part of modules defined inside `cfg_attr(..)`.
3437
"cfg_mod/dir",
3538
"cfg_mod/bar.rs",

tests/source/issue-3253/bar.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Empty
2+
fn empty() {
3+
4+
}

tests/source/issue-3253/foo.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub fn hello( )
2+
{
3+
println!("Hello World!");
4+
5+
}
6+

tests/source/issue-3253/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[macro_use]
2+
extern crate cfg_if;
3+
4+
cfg_if! {
5+
if #[cfg(target_family = "unix")] {
6+
mod foo;
7+
#[path = "paths/bar_foo.rs"]
8+
mod bar_foo;
9+
} else {
10+
mod bar;
11+
#[path = "paths/foo_bar.rs"]
12+
mod foo_bar;
13+
}
14+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo_decl_item(x: &mut i32) {
2+
x = 3;
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This module is not imported in the cfg_if macro in lib.rs so it is ignored
2+
// while the foo and bar mods are formatted.
3+
// Check the corresponding file in tests/target/issue-3253/paths/excluded.rs
4+
trait CoolerTypes { fn dummy(&self) {
5+
}
6+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
fn Foo<T>() where T: Bar {
4+
}

tests/target/issue-3253/bar.rs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Empty
2+
fn empty() {}

tests/target/issue-3253/foo.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn hello() {
2+
println!("Hello World!");
3+
}

tests/target/issue-3253/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[macro_use]
2+
extern crate cfg_if;
3+
4+
cfg_if! {
5+
if #[cfg(target_family = "unix")] {
6+
mod foo;
7+
#[path = "paths/bar_foo.rs"]
8+
mod bar_foo;
9+
} else {
10+
mod bar;
11+
#[path = "paths/foo_bar.rs"]
12+
mod foo_bar;
13+
}
14+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn foo_decl_item(x: &mut i32) {
2+
x = 3;
3+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This module is not imported in the cfg_if macro in lib.rs so it is ignored
2+
// while the foo and bar mods are formatted.
3+
// Check the corresponding file in tests/source/issue-3253/paths/excluded.rs
4+
5+
6+
7+
8+
fn Foo<T>() where T: Bar {
9+
}
10+
11+
12+
13+
trait CoolerTypes { fn dummy(&self) {
14+
}
15+
}
16+
17+
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn Foo<T>()
2+
where
3+
T: Bar,
4+
{
5+
}

0 commit comments

Comments
 (0)