]> git.lizzy.rs Git - rust.git/commitdiff
Report fatal lexer errors in `--cfg` command line arguments
authorFabian Wolff <fabian.wolff@alumni.ethz.ch>
Sat, 2 Oct 2021 17:15:45 +0000 (19:15 +0200)
committerFabian Wolff <fabian.wolff@alumni.ethz.ch>
Sat, 2 Oct 2021 17:15:55 +0000 (19:15 +0200)
compiler/rustc_errors/src/emitter.rs
compiler/rustc_interface/src/interface.rs
compiler/rustc_session/src/parse.rs
src/test/ui/conditional-compilation/cfg-arg-invalid-7.rs [new file with mode: 0644]
src/test/ui/conditional-compilation/cfg-arg-invalid-7.stderr [new file with mode: 0644]

index 29f352ae58559bf05f68b617c79d569bc735df47..fa986ce8a7e1252585464387c241eb9dfad58dea 100644 (file)
@@ -15,7 +15,7 @@
 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,
 };
 
@@ -527,14 +527,27 @@ fn supports_color(&self) -> bool {
     }
 }
 
-/// 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.
index 8393826aa12855d367279be6e8613c0ad7cf050d..3233887169cd1cf84e7b23646d7827be66a642c1 100644 (file)
@@ -81,7 +81,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());
 
index a007b530302719a9ee4b22ba2b656a63baf81123..f7246641dca3e952f8369c034a92c80de8988265 100644 (file)
@@ -174,9 +174,14 @@ pub fn with_span_handler(handler: Handler, source_map: Lrc<SourceMap>) -> Self {
         }
     }
 
-    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)
     }
 
diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-7.rs b/src/test/ui/conditional-compilation/cfg-arg-invalid-7.rs
new file mode 100644 (file)
index 0000000..149142f
--- /dev/null
@@ -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
diff --git a/src/test/ui/conditional-compilation/cfg-arg-invalid-7.stderr b/src/test/ui/conditional-compilation/cfg-arg-invalid-7.stderr
new file mode 100644 (file)
index 0000000..919709c
--- /dev/null
@@ -0,0 +1,4 @@
+error[E0765]: unterminated double quote string
+  |
+  = note: this error occurred on the command line: `--cfg=a"`
+