]> git.lizzy.rs Git - rust.git/commitdiff
Make sure the compiler actually panics on `delay_span_bug`
authorOliver Schneider <github35764891676564198441@oli-obk.de>
Thu, 19 Jul 2018 15:53:44 +0000 (17:53 +0200)
committerOliver Schneider <github35764891676564198441@oli-obk.de>
Fri, 20 Jul 2018 13:59:21 +0000 (15:59 +0200)
Even if that is just happening because of `abort_if_errors`

src/librustc/session/config.rs
src/librustc/session/mod.rs
src/librustc_errors/diagnostic_builder.rs
src/librustc_errors/lib.rs
src/librustdoc/core.rs

index 55752141e30ebd4f8e7647be6a8b2f775d58851c..293b5c63cf06aa69ec5a1aab4e5b8f1f4fe3cf6d 100644 (file)
@@ -1193,6 +1193,8 @@ fn parse_cross_lang_lto(slot: &mut CrossLangLto, v: Option<&str>) -> bool {
           "run all passes except codegen; no output"),
     treat_err_as_bug: bool = (false, parse_bool, [TRACKED],
           "treat all errors that occur as bugs"),
+    report_delayed_bugs: bool = (false, parse_bool, [TRACKED],
+          "immediately print bugs registered with `delay_span_bug`"),
     external_macro_backtrace: bool = (false, parse_bool, [UNTRACKED],
           "show macro backtraces even for non-local macros"),
     teach: bool = (false, parse_bool, [TRACKED],
@@ -3133,6 +3135,10 @@ fn test_debugging_options_tracking_hash() {
         opts.debugging_opts.treat_err_as_bug = true;
         assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
 
+        opts = reference.clone();
+        opts.debugging_opts.report_delayed_bugs = true;
+        assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
+
         opts = reference.clone();
         opts.debugging_opts.continue_parse_after_error = true;
         assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
index ad1df0a1348c53917b67d85dfc373942e97aaa3c..77a1129f66d27726dc67aab94a3bec227a88a177 100644 (file)
@@ -1000,6 +1000,7 @@ pub fn build_session_with_codemap(
     let can_emit_warnings = !(warnings_allow || cap_lints_allow);
 
     let treat_err_as_bug = sopts.debugging_opts.treat_err_as_bug;
+    let report_delayed_bugs = sopts.debugging_opts.report_delayed_bugs;
 
     let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace;
 
@@ -1045,6 +1046,7 @@ pub fn build_session_with_codemap(
         errors::HandlerFlags {
             can_emit_warnings,
             treat_err_as_bug,
+            report_delayed_bugs,
             external_macro_backtrace,
             ..Default::default()
         },
index 9c7b7ea3395402dc715ba030e55d3706fb1064ac..24ece514a47b5c668f5982c8f21e0abc5d14cde9 100644 (file)
@@ -132,7 +132,7 @@ pub fn sub<S: Into<MultiSpan>>(
     /// locally in whichever way makes the most sense.
     pub fn delay_as_bug(&mut self) {
         self.level = Level::Bug;
-        *self.handler.delayed_span_bug.borrow_mut() = Some(self.diagnostic.clone());
+        self.handler.delay_as_bug(self.diagnostic.clone());
         self.cancel();
     }
 
index f18a7bd9136a209b9e994055a6ee76c20eca405b..06c49b3a103d74fa9977ee46ffb452708f9d6551 100644 (file)
@@ -274,7 +274,7 @@ pub struct Handler {
     err_count: AtomicUsize,
     emitter: Lock<Box<dyn Emitter + sync::Send>>,
     continue_after_error: LockCell<bool>,
-    delayed_span_bug: Lock<Option<Diagnostic>>,
+    delayed_span_bug: Lock<Vec<Diagnostic>>,
 
     // This set contains the `DiagnosticId` of all emitted diagnostics to avoid
     // emitting the same diagnostic with extended help (`--teach`) twice, which
@@ -299,9 +299,25 @@ fn default_track_diagnostic(_: &Diagnostic) {}
 pub struct HandlerFlags {
     pub can_emit_warnings: bool,
     pub treat_err_as_bug: bool,
+    pub report_delayed_bugs: bool,
     pub external_macro_backtrace: bool,
 }
 
+impl Drop for Handler {
+    fn drop(&mut self) {
+        if self.err_count() == 0 {
+            let mut bugs = self.delayed_span_bug.borrow_mut();
+            let has_bugs = !bugs.is_empty();
+            for bug in bugs.drain(..) {
+                DiagnosticBuilder::new_diagnostic(self, bug).emit();
+            }
+            if has_bugs {
+                panic!("no errors encountered even though `delay_span_bug` issued");
+            }
+        }
+    }
+}
+
 impl Handler {
     pub fn with_tty_emitter(color_config: ColorConfig,
                             can_emit_warnings: bool,
@@ -346,7 +362,7 @@ pub fn with_emitter_and_flags(e: Box<dyn Emitter + sync::Send>, flags: HandlerFl
             err_count: AtomicUsize::new(0),
             emitter: Lock::new(e),
             continue_after_error: LockCell::new(true),
-            delayed_span_bug: Lock::new(None),
+            delayed_span_bug: Lock::new(Vec::new()),
             taught_diagnostics: Lock::new(FxHashSet()),
             emitted_diagnostic_codes: Lock::new(FxHashSet()),
             emitted_diagnostics: Lock::new(FxHashSet()),
@@ -503,11 +519,18 @@ pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
     }
     pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
         if self.flags.treat_err_as_bug {
+            // FIXME: don't abort here if report_delayed_bugs is off
             self.span_bug(sp, msg);
         }
         let mut diagnostic = Diagnostic::new(Level::Bug, msg);
         diagnostic.set_span(sp.into());
-        *self.delayed_span_bug.borrow_mut() = Some(diagnostic);
+        self.delay_as_bug(diagnostic);
+    }
+    fn delay_as_bug(&self, diagnostic: Diagnostic) {
+        if self.flags.report_delayed_bugs {
+            DiagnosticBuilder::new_diagnostic(self, diagnostic.clone()).emit();
+        }
+        self.delayed_span_bug.borrow_mut().push(diagnostic);
     }
     pub fn span_bug_no_panic<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {
         self.emit(&sp.into(), msg, Bug);
@@ -615,9 +638,6 @@ pub fn print_error_count(&self) {
 
     pub fn abort_if_errors(&self) {
         if self.err_count() == 0 {
-            if let Some(bug) = self.delayed_span_bug.borrow_mut().take() {
-                DiagnosticBuilder::new_diagnostic(self, bug).emit();
-            }
             return;
         }
         FatalError.raise();
index 0a56c639220b464406056cebdff1a704926ddf6e..a31357349a4b68e4a10e1f8b9b11113a75d941c8 100644 (file)
@@ -161,6 +161,7 @@ pub fn new_handler(error_format: ErrorOutputType, codemap: Option<Lrc<codemap::C
         errors::HandlerFlags {
             can_emit_warnings: true,
             treat_err_as_bug: false,
+            report_delayed_bugs: false,
             external_macro_backtrace: false,
             ..Default::default()
         },