fix: backport fix for submod parser errors
This commit is contained in:
parent
dead3a807d
commit
ce1a3efff0
@ -62,7 +62,7 @@ fn format_project<T: FormatHandler>(
|
||||
let main_file = input.file_name();
|
||||
let input_is_stdin = main_file == FileName::Stdin;
|
||||
|
||||
let mut parse_session = ParseSess::new(config)?;
|
||||
let parse_session = ParseSess::new(config)?;
|
||||
if config.skip_children() && parse_session.ignore_file(&main_file) {
|
||||
return Ok(FormatReport::new());
|
||||
}
|
||||
@ -82,10 +82,6 @@ fn format_project<T: FormatHandler>(
|
||||
return Ok(report);
|
||||
}
|
||||
};
|
||||
timer = timer.done_parsing();
|
||||
|
||||
// Suppress error output if we have to do any further parsing.
|
||||
parse_session.set_silent_emitter();
|
||||
|
||||
let mut context = FormatContext::new(&krate, report, parse_session, config, handler);
|
||||
let files = modules::ModResolver::new(
|
||||
@ -93,8 +89,12 @@ fn format_project<T: FormatHandler>(
|
||||
directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaMod),
|
||||
!input_is_stdin && !config.skip_children(),
|
||||
)
|
||||
.visit_crate(&krate)
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
|
||||
.visit_crate(&krate)?;
|
||||
|
||||
timer = timer.done_parsing();
|
||||
|
||||
// Suppress error output if we have to do any further parsing.
|
||||
context.parse_session.set_silent_emitter();
|
||||
|
||||
for (path, module) in files {
|
||||
let should_ignore = !input_is_stdin && context.ignore_file(&path);
|
||||
|
@ -36,8 +36,8 @@ pub(crate) struct ModResolver<'ast, 'sess> {
|
||||
#[error("failed to resolve mod `{module}`: {kind}")]
|
||||
#[derive(Debug, Error)]
|
||||
pub struct ModuleResolutionError {
|
||||
module: String,
|
||||
kind: ModuleResolutionErrorKind,
|
||||
pub(crate) module: String,
|
||||
pub(crate) kind: ModuleResolutionErrorKind,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
|
@ -119,7 +119,17 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
}));
|
||||
match result {
|
||||
Ok(Some(m)) => Ok(m),
|
||||
Ok(Some(m)) => {
|
||||
if !sess.has_errors() {
|
||||
return Ok(m);
|
||||
}
|
||||
|
||||
if sess.can_reset_errors() {
|
||||
sess.reset_errors();
|
||||
return Ok(m);
|
||||
}
|
||||
Err(ParserError::ParseError)
|
||||
}
|
||||
Ok(None) => Err(ParserError::ParseError),
|
||||
Err(..) if path.exists() => Err(ParserError::ParseError),
|
||||
Err(_) => Err(ParserError::ParsePanicError),
|
||||
|
@ -11,10 +11,12 @@ use std::thread;
|
||||
|
||||
use crate::config::{Color, Config, EmitMode, FileName, NewlineStyle, ReportTactic};
|
||||
use crate::formatting::{ReportedErrors, SourceFile};
|
||||
use crate::is_nightly_channel;
|
||||
use crate::modules::{ModuleResolutionError, ModuleResolutionErrorKind};
|
||||
use crate::rustfmt_diff::{make_diff, print_diff, DiffLine, Mismatch, ModifiedChunk, OutputWriter};
|
||||
use crate::source_file;
|
||||
use crate::{FormatReport, FormatReportFormatterBuilder, Input, Session};
|
||||
use crate::{
|
||||
is_nightly_channel, ErrorKind, FormatReport, FormatReportFormatterBuilder, Input, Session,
|
||||
};
|
||||
|
||||
mod configuration_snippet;
|
||||
|
||||
@ -483,6 +485,34 @@ fn format_lines_errors_are_reported_with_tabs() {
|
||||
assert!(session.has_formatting_errors());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parser_errors_in_submods_are_surfaced() {
|
||||
// See also https://github.com/rust-lang/rustfmt/issues/4126
|
||||
let filename = "tests/parser/issue-4126/lib.rs";
|
||||
let input_file = PathBuf::from(filename);
|
||||
let exp_mod_name = "invalid";
|
||||
let config = read_config(&input_file);
|
||||
let mut session = Session::<io::Stdout>::new(config, None);
|
||||
if let Err(ErrorKind::ModuleResolutionError(ModuleResolutionError { module, kind })) =
|
||||
session.format(Input::File(filename.into()))
|
||||
{
|
||||
assert_eq!(&module, exp_mod_name);
|
||||
if let ModuleResolutionErrorKind::ParseError {
|
||||
file: unparseable_file,
|
||||
} = kind
|
||||
{
|
||||
assert_eq!(
|
||||
unparseable_file,
|
||||
PathBuf::from("tests/parser/issue-4126/invalid.rs"),
|
||||
);
|
||||
} else {
|
||||
panic!("Expected parser error");
|
||||
}
|
||||
} else {
|
||||
panic!("Expected ModuleResolution 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) {
|
||||
|
6
tests/parser/issue-4126/invalid.rs
Normal file
6
tests/parser/issue-4126/invalid.rs
Normal file
@ -0,0 +1,6 @@
|
||||
fn foo() {
|
||||
if bar && if !baz {
|
||||
next_is_none = Some(true);
|
||||
}
|
||||
println!("foo");
|
||||
}
|
1
tests/parser/issue-4126/lib.rs
Normal file
1
tests/parser/issue-4126/lib.rs
Normal file
@ -0,0 +1 @@
|
||||
mod invalid;
|
Loading…
x
Reference in New Issue
Block a user