fix: Avoid incorrect global 'cfg_if' Symbol interning

Fixes #4656
This commit is contained in:
Sean Klein 2021-01-19 13:40:58 -05:00 committed by Caleb Cartwright
parent 110271469a
commit 5e14f760c5
9 changed files with 63 additions and 5 deletions

View File

@ -3,6 +3,7 @@
#[macro_use] #[macro_use]
extern crate derive_new; extern crate derive_new;
#[cfg(test)]
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
#[macro_use] #[macro_use]

View File

@ -21,10 +21,6 @@
type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>; type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
lazy_static! {
static ref CFG_IF: Symbol = Symbol::intern("cfg_if");
}
/// Represents module with its inner attributes. /// Represents module with its inner attributes.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct Module<'a> { pub(crate) struct Module<'a> {
@ -480,7 +476,7 @@ fn is_cfg_if(item: &ast::Item) -> bool {
match item.kind { match item.kind {
ast::ItemKind::MacCall(ref mac) => { ast::ItemKind::MacCall(ref mac) => {
if let Some(first_segment) = mac.path.segments.first() { 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; return true;
} }
} }

View File

@ -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] #[test]
fn stdin_formatting_smoke_test() { fn stdin_formatting_smoke_test() {
init_log(); init_log();

View File

@ -0,0 +1,2 @@
pub fn hello( ) { }

View File

@ -0,0 +1,7 @@
extern crate cfg_if;
cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] {
mod format_me_please;
}
}

View File

@ -0,0 +1,3 @@
its_a_macro! {
// Contents
}

View File

@ -0,0 +1 @@
pub fn hello() {}

View File

@ -0,0 +1,7 @@
extern crate cfg_if;
cfg_if::cfg_if! {
if #[cfg(target_family = "unix")] {
mod format_me_please;
}
}

View File

@ -0,0 +1,3 @@
its_a_macro! {
// Contents
}