Skip to content

Commit a2042a6

Browse files
committed
Use non-panicking maybe_parser_from_source_str
1 parent 10b9afa commit a2042a6

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/formatting.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::{Duration, Instant};
88

99
use syntax::ast;
1010
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
11-
use syntax::errors::Handler;
11+
use syntax::errors::{DiagnosticBuilder, Handler};
1212
use syntax::parse::{self, ParseSess};
1313
use syntax::source_map::{FilePathMapping, SourceMap, Span};
1414

@@ -604,30 +604,41 @@ fn parse_crate(
604604
) -> Result<ast::Crate, ErrorKind> {
605605
let input_is_stdin = input.is_text();
606606

607-
let mut parser = match input {
608-
Input::File(file) => parse::new_parser_from_file(parse_session, &file),
609-
Input::Text(text) => parse::new_parser_from_source_str(
607+
let parser = match input {
608+
Input::File(file) => Ok(parse::new_parser_from_file(parse_session, &file)),
609+
Input::Text(text) => parse::maybe_new_parser_from_source_str(
610610
parse_session,
611611
syntax::source_map::FileName::Custom("stdin".to_owned()),
612612
text,
613-
),
613+
)
614+
.map_err(|diags| {
615+
diags
616+
.into_iter()
617+
.map(|d| DiagnosticBuilder::new_diagnostic(&parse_session.span_diagnostic, d))
618+
.collect::<Vec<_>>()
619+
}),
614620
};
615621

616-
parser.cfg_mods = false;
617-
if config.skip_children() {
618-
parser.recurse_into_file_modules = false;
619-
}
622+
let result = match parser {
623+
Ok(mut parser) => {
624+
parser.cfg_mods = false;
625+
if config.skip_children() {
626+
parser.recurse_into_file_modules = false;
627+
}
620628

621-
let mut parser = AssertUnwindSafe(parser);
622-
let result = catch_unwind(move || parser.0.parse_crate_mod());
629+
let mut parser = AssertUnwindSafe(parser);
630+
catch_unwind(move || parser.0.parse_crate_mod().map_err(|d| vec![d]))
631+
}
632+
Err(db) => Ok(Err(db)),
633+
};
623634

624635
match result {
625636
Ok(Ok(c)) => {
626637
if !parse_session.span_diagnostic.has_errors() {
627638
return Ok(c);
628639
}
629640
}
630-
Ok(Err(mut e)) => e.emit(),
641+
Ok(Err(mut diagnostics)) => diagnostics.iter_mut().for_each(|d| d.emit()),
631642
Err(_) => {
632643
// Note that if you see this message and want more information,
633644
// then run the `parse_crate_mod` function above without

0 commit comments

Comments
 (0)