Skip to content

Commit 0f81c7f

Browse files
authored
Rollup merge of #89468 - FabianWolff:issue-89358, r=jackh726
Report fatal lexer errors in `--cfg` command line arguments Fixes #89358. The erroneous behavior was apparently introduced by `@Mark-Simulacrum` in a678e31; the idea is to silence individual parser errors and instead emit one catch-all error message after parsing. However, for the example in #89358, a fatal lexer error is created here: https://github.com/rust-lang/rust/blob/edebf77e0090195bf80c0d8cda821e1bf9d03053/compiler/rustc_parse/src/lexer/mod.rs#L340-L349 This fatal error aborts the compilation, and so the call to `new_parser_from_source_str()` never returns and the catch-all error message is never emitted. I have therefore changed the `SilentEmitter` to silence only non-fatal errors; with my changes, for the rustc invocation described in #89358: ```sh rustc --cfg "abc\"" ``` I get the following output: ``` error[E0765]: unterminated double quote string | = note: this error occurred on the command line: `--cfg=abc"` ```
2 parents d64b3a7 + 041212f commit 0f81c7f

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

compiler/rustc_errors/src/emitter.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::{MultiSpan, SourceFile, Span};
1515
use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Style, StyledString};
1616
use crate::styled_buffer::StyledBuffer;
1717
use crate::{
18-
CodeSuggestion, Diagnostic, DiagnosticId, Level, SubDiagnostic, SubstitutionHighlight,
18+
CodeSuggestion, Diagnostic, DiagnosticId, Handler, Level, SubDiagnostic, SubstitutionHighlight,
1919
SuggestionStyle,
2020
};
2121

@@ -523,14 +523,27 @@ impl Emitter for EmitterWriter {
523523
}
524524
}
525525

526-
/// An emitter that does nothing when emitting a diagnostic.
527-
pub struct SilentEmitter;
526+
/// An emitter that does nothing when emitting a non-fatal diagnostic.
527+
/// Fatal diagnostics are forwarded to `fatal_handler` to avoid silent
528+
/// failures of rustc, as witnessed e.g. in issue #89358.
529+
pub struct SilentEmitter {
530+
pub fatal_handler: Handler,
531+
pub fatal_note: Option<String>,
532+
}
528533

529534
impl Emitter for SilentEmitter {
530535
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
531536
None
532537
}
533-
fn emit_diagnostic(&mut self, _: &Diagnostic) {}
538+
fn emit_diagnostic(&mut self, d: &Diagnostic) {
539+
if d.level == Level::Fatal {
540+
let mut d = d.clone();
541+
if let Some(ref note) = self.fatal_note {
542+
d.note(note);
543+
}
544+
self.fatal_handler.emit_diagnostic(&d);
545+
}
546+
}
534547
}
535548

536549
/// Maximum number of lines we will print for a multiline suggestion; arbitrary.

compiler/rustc_interface/src/interface.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
7575
let cfg = cfgspecs
7676
.into_iter()
7777
.map(|s| {
78-
let sess = ParseSess::with_silent_emitter();
78+
let sess = ParseSess::with_silent_emitter(Some(format!(
79+
"this error occurred on the command line: `--cfg={}`",
80+
s
81+
)));
7982
let filename = FileName::cfg_spec_source_code(&s);
8083
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
8184

compiler/rustc_session/src/parse.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,14 @@ impl ParseSess {
174174
}
175175
}
176176

177-
pub fn with_silent_emitter() -> Self {
177+
pub fn with_silent_emitter(fatal_note: Option<String>) -> Self {
178178
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
179-
let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter));
179+
let fatal_handler = Handler::with_tty_emitter(ColorConfig::Auto, false, None, None);
180+
let handler = Handler::with_emitter(
181+
false,
182+
None,
183+
Box::new(SilentEmitter { fatal_handler, fatal_note }),
184+
);
180185
ParseSess::with_span_handler(handler, sm)
181186
}
182187

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Regression test for issue #89358.
2+
3+
// compile-flags: --cfg a"
4+
// error-pattern: unterminated double quote string
5+
// error-pattern: this error occurred on the command line
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error[E0765]: unterminated double quote string
2+
|
3+
= note: this error occurred on the command line: `--cfg=a"`
4+

0 commit comments

Comments
 (0)