Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5e14f76

Browse files
smkleincalebcartwright
authored andcommitted
fix: Avoid incorrect global 'cfg_if' Symbol interning
Fixes rust-lang#4656
1 parent 1102714 commit 5e14f76

File tree

9 files changed

+63
-5
lines changed

9 files changed

+63
-5
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#[macro_use]
55
extern crate derive_new;
6+
#[cfg(test)]
67
#[macro_use]
78
extern crate lazy_static;
89
#[macro_use]

src/modules.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ mod visitor;
2121

2222
type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
2323

24-
lazy_static! {
25-
static ref CFG_IF: Symbol = Symbol::intern("cfg_if");
26-
}
27-
2824
/// Represents module with its inner attributes.
2925
#[derive(Debug, Clone)]
3026
pub(crate) struct Module<'a> {
@@ -480,7 +476,7 @@ fn is_cfg_if(item: &ast::Item) -> bool {
480476
match item.kind {
481477
ast::ItemKind::MacCall(ref mac) => {
482478
if let Some(first_segment) = mac.path.segments.first() {
483-
if first_segment.ident.name == *CFG_IF {
479+
if first_segment.ident.name == Symbol::intern("cfg_if") {
484480
return true;
485481
}
486482
}

src/test/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,44 @@ fn self_tests() {
372372
);
373373
}
374374

375+
#[test]
376+
fn format_files_find_new_files_via_cfg_if() {
377+
init_log();
378+
run_test_with(&TestSetting::default(), || {
379+
// To repro issue-4656, it is necessary that these files are parsed
380+
// as a part of the same session (hence this separate test runner).
381+
let files = vec![
382+
Path::new("tests/source/issue-4656/lib2.rs"),
383+
Path::new("tests/source/issue-4656/lib.rs"),
384+
];
385+
386+
let config = Config::default();
387+
let mut session = Session::<io::Stdout>::new(config, None);
388+
389+
let mut write_result = HashMap::new();
390+
for file in files {
391+
assert!(file.exists());
392+
let result = session.format(Input::File(file.into())).unwrap();
393+
assert!(!session.has_formatting_errors());
394+
assert!(!result.has_warnings());
395+
let mut source_file = SourceFile::new();
396+
mem::swap(&mut session.source_file, &mut source_file);
397+
398+
for (filename, text) in source_file {
399+
if let FileName::Real(ref filename) = filename {
400+
write_result.insert(filename.to_owned(), text);
401+
}
402+
}
403+
}
404+
assert_eq!(
405+
3,
406+
write_result.len(),
407+
"Should have uncovered an extra file (format_me_please.rs) via lib.rs"
408+
);
409+
assert!(handle_result(write_result, None).is_ok());
410+
});
411+
}
412+
375413
#[test]
376414
fn stdin_formatting_smoke_test() {
377415
init_log();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
pub fn hello( ) { }

tests/source/issue-4656/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern crate cfg_if;
2+
3+
cfg_if::cfg_if! {
4+
if #[cfg(target_family = "unix")] {
5+
mod format_me_please;
6+
}
7+
}

tests/source/issue-4656/lib2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
its_a_macro! {
2+
// Contents
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn hello() {}

tests/target/issue-4656/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extern crate cfg_if;
2+
3+
cfg_if::cfg_if! {
4+
if #[cfg(target_family = "unix")] {
5+
mod format_me_please;
6+
}
7+
}

tests/target/issue-4656/lib2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
its_a_macro! {
2+
// Contents
3+
}

0 commit comments

Comments
 (0)