@@ -65,8 +65,11 @@ impl<'a> ParserBuilder<'a> {
6565 let parser = match Self :: parser ( sess. inner ( ) , input) {
6666 Ok ( p) => p,
6767 Err ( db) => {
68- sess. emit_diagnostics ( db) ;
69- return Err ( ParserError :: ParserCreationError ) ;
68+ if let Some ( diagnostics) = db {
69+ sess. emit_diagnostics ( diagnostics) ;
70+ return Err ( ParserError :: ParserCreationError ) ;
71+ }
72+ return Err ( ParserError :: ParsePanicError ) ;
7073 }
7174 } ;
7275
@@ -76,14 +79,18 @@ impl<'a> ParserBuilder<'a> {
7679 fn parser (
7780 sess : & ' a rustc_session:: parse:: ParseSess ,
7881 input : Input ,
79- ) -> Result < rustc_parse:: parser:: Parser < ' a > , Vec < Diagnostic > > {
82+ ) -> Result < rustc_parse:: parser:: Parser < ' a > , Option < Vec < Diagnostic > > > {
8083 match input {
81- Input :: File ( ref file) => Ok ( new_parser_from_file ( sess, file, None ) ) ,
84+ Input :: File ( ref file) => catch_unwind ( AssertUnwindSafe ( move || {
85+ new_parser_from_file ( sess, file, None )
86+ } ) )
87+ . map_err ( |_| None ) ,
8288 Input :: Text ( text) => rustc_parse:: maybe_new_parser_from_source_str (
8389 sess,
8490 rustc_span:: FileName :: Custom ( "stdin" . to_owned ( ) ) ,
8591 text,
86- ) ,
92+ )
93+ . map_err ( |db| Some ( db) ) ,
8794 }
8895 }
8996}
@@ -120,8 +127,10 @@ impl<'a> Parser<'a> {
120127 match parser. parse_mod ( & TokenKind :: Eof , ast:: Unsafe :: No ) {
121128 Ok ( result) => Some ( result) ,
122129 Err ( mut e) => {
123- e. cancel ( ) ;
124- sess. reset_errors ( ) ;
130+ sess. emit_or_cancel_diagnostic ( & mut e) ;
131+ if sess. can_reset_errors ( ) {
132+ sess. reset_errors ( ) ;
133+ }
125134 None
126135 }
127136 }
0 commit comments