Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve some minor parsing bugs #4439

Merged
merged 2 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/formatting/syntux/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ impl<'a> ParserBuilder<'a> {
let parser = match Self::parser(sess.inner(), input) {
Ok(p) => p,
Err(db) => {
sess.emit_diagnostics(db);
return Err(ParserError::ParserCreationError);
if let Some(diagnostics) = db {
sess.emit_diagnostics(diagnostics);
return Err(ParserError::ParserCreationError);
}
return Err(ParserError::ParsePanicError);
}
};

Expand All @@ -77,14 +80,18 @@ impl<'a> ParserBuilder<'a> {
fn parser(
sess: &'a rustc_session::parse::ParseSess,
input: Input,
) -> Result<rustc_parse::parser::Parser<'a>, Vec<Diagnostic>> {
) -> Result<rustc_parse::parser::Parser<'a>, Option<Vec<Diagnostic>>> {
match input {
Input::File(ref file) => Ok(new_parser_from_file(sess, file, None)),
Input::File(ref file) => catch_unwind(AssertUnwindSafe(move || {
new_parser_from_file(sess, file, None)
}))
.map_err(|_| None),
Input::Text(text) => rustc_parse::maybe_new_parser_from_source_str(
sess,
rustc_span::FileName::Custom("stdin".to_owned()),
text,
),
)
.map_err(|db| Some(db)),
Comment on lines +83 to +94
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not terribly thrilled about Option-wrapping the error diagnostics to address this, but since new_parser_from_file doesn't return a Result and will instead panic this seemed like the least invasive/intensive way to resolve the bug

}
}
}
Expand Down Expand Up @@ -121,7 +128,7 @@ impl<'a> Parser<'a> {
match parser.parse_mod(&TokenKind::Eof, ast::Unsafe::No) {
Ok(result) => Some(result),
Err(mut e) => {
e.cancel();
sess.emit_or_cancel_diagnostic(&mut e);
if sess.can_reset_errors() {
sess.reset_errors();
}
Expand Down
11 changes: 11 additions & 0 deletions src/formatting/syntux/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ impl ParseSess {
}
}

pub(crate) fn emit_or_cancel_diagnostic(&self, diagnostic: &mut Diagnostic) {
self.parse_sess.span_diagnostic.emit_diagnostic(diagnostic);
// The Handler will check whether the diagnostic should be emitted
// based on the user's rustfmt configuration and the originating file
// that caused the parser error. If the Handler determined it should skip
// emission then we need to ensure the diagnostic is cancelled.
if !diagnostic.cancelled() {
diagnostic.cancel();
}
}

pub(super) fn can_reset_errors(&self) -> bool {
*self.can_reset_errors.borrow()
}
Expand Down
2 changes: 1 addition & 1 deletion src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub enum OperationError {
#[error("invalid glob pattern found in ignore list: {0}")]
InvalidGlobPattern(ignore::Error),
/// Parse error occurred while parsing the input.
#[error("failed to parse {input:?}")]
#[error("failed to parse {input}")]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the failed to parse Real("...") error detail that was reported in the linked issue to instead display the correct error (e.g. failed to parse /home/caleb/dev/rustfmt-sandbox/bad-syntax/src/openbrace.rs and failed to parse <stdin> for stdin)

ParseError { input: FileName, is_panic: bool },
/// Io error.
#[error("{0}")]
Expand Down
16 changes: 16 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,22 @@ fn parser_errors_in_submods_are_surfaced() {
}
}

#[test]
fn parser_creation_errors_on_entry_new_parser_from_file_panic() {
// See also https://github.com/rust-lang/rustfmt/issues/4418
let filename = "tests/parser/issue_4418.rs";
let file = PathBuf::from(filename);
let (config, operation, _) = read_config(&file);
if let Err(OperationError::ParseError { input, is_panic }) =
format_file(&file, operation, config)
{
assert_eq!(input.as_path().unwrap(), file);
assert!(is_panic);
} else {
panic!("Expected ParseError operation error");
}
}

// For each file, run rustfmt and collect the output.
// Returns the number of files checked and the number of failures.
fn check_files(files: Vec<PathBuf>, opt_config: &Option<PathBuf>) -> (Vec<FormatReport>, u32, u32) {
Expand Down
1 change: 1 addition & 0 deletions tests/parser/issue_4418.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
}