@@ -147,6 +147,9 @@ pub enum RustcStringStyle {
147
147
///
148
148
/// If the input is accepted, returns a list of tokens, in [`RustcToken`] form.
149
149
/// Otherwise returns at least one error message.
150
+ ///
151
+ /// If rustc panics (ie, it would report an ICE), the panic message is sent to
152
+ /// standard error and this function returns CompilerError.
150
153
pub fn analyse ( input : & str , edition : Edition ) -> Analysis {
151
154
let error_list = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
152
155
fn extract_errors ( error_list : ErrorAccumulator ) -> Vec < String > {
@@ -158,27 +161,29 @@ pub fn analyse(input: &str, edition: Edition) -> Analysis {
158
161
Edition :: E2021 => rustc_span:: edition:: Edition :: Edition2021 ,
159
162
} ;
160
163
161
- match rustc_driver:: catch_fatal_errors ( || {
162
- rustc_span:: create_session_globals_then ( rustc_edition, || {
163
- run_lexer ( input, error_list. clone ( ) )
164
- } )
165
- } ) {
166
- Ok ( rustc_tokens) => {
167
- let messages = extract_errors ( error_list) ;
168
- if messages. is_empty ( ) {
169
- // Lexing succeeded
170
- Analysis :: Accepts ( rustc_tokens)
171
- } else {
172
- // Lexing reported a non-fatal error
173
- Analysis :: Rejects ( rustc_tokens, messages)
164
+ std:: panic:: catch_unwind ( || {
165
+ match rustc_driver:: catch_fatal_errors ( || {
166
+ rustc_span:: create_session_globals_then ( rustc_edition, || {
167
+ run_lexer ( input, error_list. clone ( ) )
168
+ } )
169
+ } ) {
170
+ Ok ( rustc_tokens) => {
171
+ let messages = extract_errors ( error_list) ;
172
+ if messages. is_empty ( ) {
173
+ // Lexing succeeded
174
+ Analysis :: Accepts ( rustc_tokens)
175
+ } else {
176
+ // Lexing reported a non-fatal error
177
+ Analysis :: Rejects ( rustc_tokens, messages)
178
+ }
179
+ }
180
+ Err ( _) => {
181
+ let mut messages = extract_errors ( error_list) ;
182
+ messages. push ( "reported fatal error (panicked)" . into ( ) ) ;
183
+ Analysis :: Rejects ( Vec :: new ( ) , messages)
174
184
}
175
185
}
176
- Err ( _) => {
177
- let mut messages = extract_errors ( error_list) ;
178
- messages. push ( "reported fatal error (panicked)" . into ( ) ) ;
179
- Analysis :: Rejects ( Vec :: new ( ) , messages)
180
- }
181
- }
186
+ } ) . unwrap_or ( Analysis :: CompilerError )
182
187
}
183
188
184
189
/// Result of running lexical analysis on a string.
@@ -192,6 +197,8 @@ pub enum Analysis {
192
197
///
193
198
/// The strings are error messages. There's always at least one message.
194
199
Rejects ( Vec < RustcToken > , Vec < String > ) ,
200
+ /// The input provoked an internal compiler error.
201
+ CompilerError ,
195
202
}
196
203
197
204
/// Runs rustc's lexical analysis on the specified input.
0 commit comments