Skip to content

Commit

Permalink
fix: Avoid incorrect global 'cfg_if' Symbol interning
Browse files Browse the repository at this point in the history
Fixes #4656
  • Loading branch information
smklein authored and calebcartwright committed Jan 28, 2021
1 parent 1102714 commit 5e14f76
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#[macro_use]
extern crate derive_new;
#[cfg(test)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
Expand Down
6 changes: 1 addition & 5 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ mod visitor;

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

lazy_static! {
static ref CFG_IF: Symbol = Symbol::intern("cfg_if");
}

/// Represents module with its inner attributes.
#[derive(Debug, Clone)]
pub(crate) struct Module<'a> {
Expand Down Expand Up @@ -480,7 +476,7 @@ fn is_cfg_if(item: &ast::Item) -> bool {
match item.kind {
ast::ItemKind::MacCall(ref mac) => {
if let Some(first_segment) = mac.path.segments.first() {
if first_segment.ident.name == *CFG_IF {
if first_segment.ident.name == Symbol::intern("cfg_if") {
return true;
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,44 @@ fn self_tests() {
);
}

#[test]
fn format_files_find_new_files_via_cfg_if() {
init_log();
run_test_with(&TestSetting::default(), || {
// To repro issue-4656, it is necessary that these files are parsed
// as a part of the same session (hence this separate test runner).
let files = vec![
Path::new("tests/source/issue-4656/lib2.rs"),
Path::new("tests/source/issue-4656/lib.rs"),
];

let config = Config::default();
let mut session = Session::<io::Stdout>::new(config, None);

let mut write_result = HashMap::new();
for file in files {
assert!(file.exists());
let result = session.format(Input::File(file.into())).unwrap();
assert!(!session.has_formatting_errors());
assert!(!result.has_warnings());
let mut source_file = SourceFile::new();
mem::swap(&mut session.source_file, &mut source_file);

for (filename, text) in source_file {
if let FileName::Real(ref filename) = filename {
write_result.insert(filename.to_owned(), text);
}
}
}
assert_eq!(
3,
write_result.len(),
"Should have uncovered an extra file (format_me_please.rs) via lib.rs"
);
assert!(handle_result(write_result, None).is_ok());
});
}

#[test]
fn stdin_formatting_smoke_test() {
init_log();
Expand Down
2 changes: 2 additions & 0 deletions tests/source/issue-4656/format_me_please.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

pub fn hello( ) { }
7 changes: 7 additions & 0 deletions tests/source/issue-4656/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern crate cfg_if;

cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] {
mod format_me_please;
}
}
3 changes: 3 additions & 0 deletions tests/source/issue-4656/lib2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
its_a_macro! {
// Contents
}
1 change: 1 addition & 0 deletions tests/target/issue-4656/format_me_please.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn hello() {}
7 changes: 7 additions & 0 deletions tests/target/issue-4656/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern crate cfg_if;

cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] {
mod format_me_please;
}
}
3 changes: 3 additions & 0 deletions tests/target/issue-4656/lib2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
its_a_macro! {
// Contents
}

0 comments on commit 5e14f76

Please sign in to comment.