Skip to content

Commit

Permalink
Rollup merge of #89468 - FabianWolff:issue-89358, r=jackh726
Browse files Browse the repository at this point in the history
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"`
```
  • Loading branch information
matthiaskrgr committed Oct 23, 2021
2 parents d64b3a7 + 041212f commit 0f81c7f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
21 changes: 17 additions & 4 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_span::{MultiSpan, SourceFile, Span};
use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Style, StyledString};
use crate::styled_buffer::StyledBuffer;
use crate::{
CodeSuggestion, Diagnostic, DiagnosticId, Level, SubDiagnostic, SubstitutionHighlight,
CodeSuggestion, Diagnostic, DiagnosticId, Handler, Level, SubDiagnostic, SubstitutionHighlight,
SuggestionStyle,
};

Expand Down Expand Up @@ -523,14 +523,27 @@ impl Emitter for EmitterWriter {
}
}

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

impl Emitter for SilentEmitter {
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
None
}
fn emit_diagnostic(&mut self, _: &Diagnostic) {}
fn emit_diagnostic(&mut self, d: &Diagnostic) {
if d.level == Level::Fatal {
let mut d = d.clone();
if let Some(ref note) = self.fatal_note {
d.note(note);
}
self.fatal_handler.emit_diagnostic(&d);
}
}
}

/// Maximum number of lines we will print for a multiline suggestion; arbitrary.
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String
let cfg = cfgspecs
.into_iter()
.map(|s| {
let sess = ParseSess::with_silent_emitter();
let sess = ParseSess::with_silent_emitter(Some(format!(
"this error occurred on the command line: `--cfg={}`",
s
)));
let filename = FileName::cfg_spec_source_code(&s);
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());

Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_session/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,14 @@ impl ParseSess {
}
}

pub fn with_silent_emitter() -> Self {
pub fn with_silent_emitter(fatal_note: Option<String>) -> Self {
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter));
let fatal_handler = Handler::with_tty_emitter(ColorConfig::Auto, false, None, None);
let handler = Handler::with_emitter(
false,
None,
Box::new(SilentEmitter { fatal_handler, fatal_note }),
);
ParseSess::with_span_handler(handler, sm)
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/conditional-compilation/cfg-arg-invalid-7.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Regression test for issue #89358.

// compile-flags: --cfg a"
// error-pattern: unterminated double quote string
// error-pattern: this error occurred on the command line
4 changes: 4 additions & 0 deletions src/test/ui/conditional-compilation/cfg-arg-invalid-7.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error[E0765]: unterminated double quote string
|
= note: this error occurred on the command line: `--cfg=a"`

0 comments on commit 0f81c7f

Please sign in to comment.