Skip to content

Commit 041212f

Browse files
committed
Report fatal lexer errors in --cfg command line arguments
1 parent ed93759 commit 041212f

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

@@ -527,14 +527,27 @@ impl Emitter for EmitterWriter {
527527
}
528528
}
529529

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

533538
impl Emitter for SilentEmitter {
534539
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
535540
None
536541
}
537-
fn emit_diagnostic(&mut self, _: &Diagnostic) {}
542+
fn emit_diagnostic(&mut self, d: &Diagnostic) {
543+
if d.level == Level::Fatal {
544+
let mut d = d.clone();
545+
if let Some(ref note) = self.fatal_note {
546+
d.note(note);
547+
}
548+
self.fatal_handler.emit_diagnostic(&d);
549+
}
550+
}
538551
}
539552

540553
/// 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
@@ -81,7 +81,10 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
8181
let cfg = cfgspecs
8282
.into_iter()
8383
.map(|s| {
84-
let sess = ParseSess::with_silent_emitter();
84+
let sess = ParseSess::with_silent_emitter(Some(format!(
85+
"this error occurred on the command line: `--cfg={}`",
86+
s
87+
)));
8588
let filename = FileName::cfg_spec_source_code(&s);
8689
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
8790

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)