Skip to content

Commit 6f1a58d

Browse files
feat: continue on recovered parse errors in ignore files
1 parent 9ad9df0 commit 6f1a58d

File tree

7 files changed

+69
-14
lines changed

7 files changed

+69
-14
lines changed

src/formatting.rs

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// High level formatting functions.
22

3+
use std::cell::Cell;
34
use std::collections::HashMap;
45
use std::io::{self, Write};
56
use std::panic::{catch_unwind, AssertUnwindSafe};
@@ -27,6 +28,8 @@ mod newline_style;
2728
pub(crate) type SourceFile = Vec<FileRecord>;
2829
pub(crate) type FileRecord = (FileName, String);
2930

31+
thread_local!(static CAN_RESET_CRATE_PARSE_ERRORS: Cell<bool> = Cell::new(false));
32+
3033
impl<'b, T: Write + 'b> Session<'b, T> {
3134
pub(crate) fn format_input_inner(&mut self, input: Input) -> Result<FormatReport, ErrorKind> {
3235
if !self.config.version_meets_requirement() {
@@ -70,17 +73,16 @@ fn format_project<T: FormatHandler>(
7073
Ok(set) => set,
7174
Err(e) => return Err(ErrorKind::InvalidGlobPattern(e)),
7275
};
73-
let is_ignore_match = ignore_path_set.is_match(&main_file);
74-
if config.skip_children() && is_ignore_match {
76+
if config.skip_children() && ignore_path_set.is_match(&main_file) {
7577
return Ok(FormatReport::new());
7678
}
79+
let _ = CAN_RESET_CRATE_PARSE_ERRORS.try_with(|r| {
80+
r.set(false);
81+
});
7782

7883
// Parse the crate.
7984
let source_map = Rc::new(SourceMap::new(FilePathMapping::empty()));
8085
let mut parse_session = make_parse_sess(source_map.clone(), config, &ignore_path_set);
81-
if is_ignore_match {
82-
parse_session.span_diagnostic = Handler::with_emitter(true, None, silent_emitter());
83-
}
8486
let mut report = FormatReport::new();
8587
let directory_ownership = input.to_directory_ownership();
8688
let krate = match parse_crate(
@@ -92,13 +94,7 @@ fn format_project<T: FormatHandler>(
9294
) {
9395
Ok(krate) => krate,
9496
// Surface parse error via Session (errors are merged there from report)
95-
Err(ErrorKind::ParseError) => {
96-
// https://github.com/rust-lang/rustfmt/issues/3779
97-
if is_ignore_match {
98-
return Ok(FormatReport::new());
99-
}
100-
return Ok(report);
101-
}
97+
Err(ErrorKind::ParseError) => return Ok(report),
10298
Err(e) => return Err(e),
10399
};
104100
timer = timer.done_parsing();
@@ -677,6 +673,19 @@ fn parse_crate(
677673
if !parse_session.span_diagnostic.has_errors() {
678674
return Ok(c);
679675
}
676+
// This scenario occurs when the parser encountered errors
677+
// but was still able to recover. If all of the parser errors
678+
// occurred in files that are ignored, then reset
679+
// the error count and continue.
680+
// https://github.com/rust-lang/rustfmt/issues/3779
681+
let mut can_reset = false;
682+
let _ = CAN_RESET_CRATE_PARSE_ERRORS.try_with(|r| {
683+
can_reset = r.get();
684+
});
685+
if can_reset {
686+
parse_session.span_diagnostic.reset_err_count();
687+
return Ok(c);
688+
}
680689
}
681690
Ok(Err(mut diagnostics)) => diagnostics.iter_mut().for_each(DiagnosticBuilder::emit),
682691
Err(_) => {
@@ -697,6 +706,8 @@ struct SilentOnIgnoredFilesEmitter {
697706
ignore_path_set: IgnorePathSet,
698707
source_map: Rc<SourceMap>,
699708
emitter: EmitterWriter,
709+
can_reset: bool,
710+
has_non_ignorable_parser_errors: bool,
700711
}
701712

702713
impl Emitter for SilentOnIgnoredFilesEmitter {
@@ -709,13 +720,26 @@ impl Emitter for SilentOnIgnoredFilesEmitter {
709720
.ignore_path_set
710721
.is_match(&FileName::Real(path.to_path_buf()))
711722
{
712-
db.handler.reset_err_count();
723+
if !self.has_non_ignorable_parser_errors && !self.can_reset {
724+
let _ = CAN_RESET_CRATE_PARSE_ERRORS.try_with(|r| {
725+
r.set(true);
726+
});
727+
self.can_reset = true;
728+
}
713729
return;
714730
}
715731
}
716732
_ => (),
717733
};
718734
}
735+
736+
self.has_non_ignorable_parser_errors = true;
737+
if self.can_reset {
738+
let _ = CAN_RESET_CRATE_PARSE_ERRORS.try_with(|r| {
739+
r.set(false);
740+
});
741+
}
742+
self.can_reset = false;
719743
self.emitter.emit_diagnostic(db);
720744
}
721745
}
@@ -746,9 +770,12 @@ fn make_parse_sess(
746770
} else {
747771
ColorConfig::Never
748772
};
773+
749774
let emitter_writer =
750-
EmitterWriter::stderr(color_cfg, Some(source_map.clone()), false, false);
775+
EmitterWriter::stderr(color_cfg, Some(source_map.clone()), false, false, None);
751776
let emitter = Box::new(SilentOnIgnoredFilesEmitter {
777+
has_non_ignorable_parser_errors: false,
778+
can_reset: false,
752779
ignore_path_set: ignore_path_set.clone(),
753780
source_map: source_map.clone(),
754781
emitter: emitter_writer,

src/test/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const SKIP_FILE_WHITE_LIST: &[&str] = &[
2727
"configs/skip_children/foo/mod.rs",
2828
"issue-3434/no_entry.rs",
2929
"issue-3665/sub_mod.rs",
30+
// Testing for issue-3779
31+
"issue-3779/ice.rs",
3032
// These files and directory are a part of modules defined inside `cfg_if!`.
3133
"cfg_if/mod.rs",
3234
"cfg_if/detect",

tests/config/issue-3779.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
unstable_features = true
2+
ignore = [
3+
"tests/**/issue-3779/ice.rs"
4+
]

tests/source/issue-3779/ice.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn bar() {
2+
1x;
3+
}

tests/source/issue-3779/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-config: issue-3779.toml
2+
3+
#[path = "ice.rs"]
4+
mod ice;
5+
6+
fn foo() {
7+
println!("abc") ;
8+
}

tests/target/issue-3779/ice.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn bar() {
2+
1x;
3+
}

tests/target/issue-3779/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-config: issue-3779.toml
2+
3+
#[path = "ice.rs"]
4+
mod ice;
5+
6+
fn foo() {
7+
println!("abc");
8+
}

0 commit comments

Comments
 (0)