@@ -65,8 +65,11 @@ impl<'a> ParserBuilder<'a> {
65
65
let parser = match Self :: parser ( sess. inner ( ) , input) {
66
66
Ok ( p) => p,
67
67
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 ) ;
70
73
}
71
74
} ;
72
75
@@ -76,14 +79,18 @@ impl<'a> ParserBuilder<'a> {
76
79
fn parser (
77
80
sess : & ' a rustc_session:: parse:: ParseSess ,
78
81
input : Input ,
79
- ) -> Result < rustc_parse:: parser:: Parser < ' a > , Vec < Diagnostic > > {
82
+ ) -> Result < rustc_parse:: parser:: Parser < ' a > , Option < Vec < Diagnostic > > > {
80
83
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 ) ,
82
88
Input :: Text ( text) => rustc_parse:: maybe_new_parser_from_source_str (
83
89
sess,
84
90
rustc_span:: FileName :: Custom ( "stdin" . to_owned ( ) ) ,
85
91
text,
86
- ) ,
92
+ )
93
+ . map_err ( |db| Some ( db) ) ,
87
94
}
88
95
}
89
96
}
@@ -120,8 +127,10 @@ impl<'a> Parser<'a> {
120
127
match parser. parse_mod ( & TokenKind :: Eof , ast:: Unsafe :: No ) {
121
128
Ok ( result) => Some ( result) ,
122
129
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
+ }
125
134
None
126
135
}
127
136
}
0 commit comments