From 8eb5843a592afbee8ae5352828f8f0b74034b9e0 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Fri, 3 Mar 2023 22:25:18 +0000 Subject: On nightly, dump ICE backtraces to disk Implement rust-lang/compiler-team#578. When an ICE is encountered on nightly releases, the new rustc panic handler will also write the contents of the backtrace to disk. If any `delay_span_bug`s are encountered, their backtrace is also added to the file. The platform and rustc version will also be collected. --- compiler/rustc_errors/src/json/tests.rs | 2 +- compiler/rustc_errors/src/lib.rs | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/json/tests.rs b/compiler/rustc_errors/src/json/tests.rs index 671dc449eaa..db0dd4ffe8e 100644 --- a/compiler/rustc_errors/src/json/tests.rs +++ b/compiler/rustc_errors/src/json/tests.rs @@ -64,7 +64,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { ); let span = Span::with_root_ctxt(BytePos(span.0), BytePos(span.1)); - let handler = Handler::with_emitter(true, None, Box::new(je)); + let handler = Handler::with_emitter(true, None, Box::new(je), None); handler.span_err(span, "foo"); let bytes = output.lock().unwrap(); diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index b9db25103a3..31410c39d36 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -47,9 +47,10 @@ use std::borrow::Cow; use std::error::Report; use std::fmt; use std::hash::Hash; +use std::io::Write; use std::num::NonZeroUsize; use std::panic; -use std::path::Path; +use std::path::{Path, PathBuf}; use termcolor::{Color, ColorSpec}; @@ -461,6 +462,10 @@ struct HandlerInner { /// /// [RFC-2383]: https://rust-lang.github.io/rfcs/2383-lint-reasons.html fulfilled_expectations: FxHashSet, + + /// The file where the ICE information is stored. This allows delayed_span_bug backtraces to be + /// stored along side the main panic backtrace. + ice_file: Option, } /// A key denoting where from a diagnostic was stashed. @@ -550,6 +555,7 @@ impl Handler { sm: Option>, fluent_bundle: Option>, fallback_bundle: LazyFallbackBundle, + ice_file: Option, ) -> Self { Self::with_tty_emitter_and_flags( color_config, @@ -557,6 +563,7 @@ impl Handler { fluent_bundle, fallback_bundle, HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() }, + ice_file, ) } @@ -566,6 +573,7 @@ impl Handler { fluent_bundle: Option>, fallback_bundle: LazyFallbackBundle, flags: HandlerFlags, + ice_file: Option, ) -> Self { let emitter = Box::new(EmitterWriter::stderr( color_config, @@ -579,23 +587,26 @@ impl Handler { flags.track_diagnostics, TerminalUrl::No, )); - Self::with_emitter_and_flags(emitter, flags) + Self::with_emitter_and_flags(emitter, flags, ice_file) } pub fn with_emitter( can_emit_warnings: bool, treat_err_as_bug: Option, emitter: Box, + ice_file: Option, ) -> Self { Handler::with_emitter_and_flags( emitter, HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() }, + ice_file, ) } pub fn with_emitter_and_flags( emitter: Box, flags: HandlerFlags, + ice_file: Option, ) -> Self { Self { flags, @@ -618,6 +629,7 @@ impl Handler { check_unstable_expect_diagnostics: false, unstable_expect_diagnostics: Vec::new(), fulfilled_expectations: Default::default(), + ice_file, }), } } @@ -1657,8 +1669,21 @@ impl HandlerInner { explanation: impl Into + Copy, ) { let mut no_bugs = true; + // If backtraces are enabled, also print the query stack + let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(true, |x| &x != "0"); for bug in bugs { - let mut bug = bug.decorate(); + if let Some(file) = self.ice_file.as_ref() + && let Ok(mut out) = std::fs::File::options().append(true).open(file) + { + let _ = write!( + &mut out, + "\n\ndelayed span bug: {}\n{}", + bug.inner.styled_message().iter().filter_map(|(msg, _)| msg.as_str()).collect::(), + &bug.note + ); + } + let mut bug = + if backtrace || self.ice_file.is_none() { bug.decorate() } else { bug.inner }; if no_bugs { // Put the overall explanation before the `DelayedBug`s, to -- cgit 1.4.1-3-g733a5 From 20a3b9a2152ff705af029cbb359bc09a4b43a9c7 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Wed, 19 Jul 2023 23:48:43 +0000 Subject: Use the correct span for displaying the line following a derive suggestion --- compiler/rustc_errors/src/emitter.rs | 2 +- tests/ui/modules/issue-107649.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 9d4d159fd96..a0fa4115c3e 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -1982,7 +1982,7 @@ impl EmitterWriter { // We special case `#[derive(_)]\n` and other attribute suggestions, because those // are the ones where context is most useful. let file_lines = sm - .span_to_lines(span.primary_span().unwrap().shrink_to_hi()) + .span_to_lines(parts[0].span.shrink_to_hi()) .expect("span_to_lines failed when emitting suggestion"); let line_num = sm.lookup_char_pos(parts[0].span.lo()).line; if let Some(line) = file_lines.file.get_line(line_num - 1) { diff --git a/tests/ui/modules/issue-107649.stderr b/tests/ui/modules/issue-107649.stderr index 38a910b57b4..5705e84e0d9 100644 --- a/tests/ui/modules/issue-107649.stderr +++ b/tests/ui/modules/issue-107649.stderr @@ -11,7 +11,7 @@ help: consider annotating `Dummy` with `#[derive(Debug)]` --> $DIR/auxiliary/dummy_lib.rs:2:1 | 2 + #[derive(Debug)] -3 | #[path = "auxiliary/dummy_lib.rs"] +3 | pub struct Dummy; | error: aborting due to previous error -- cgit 1.4.1-3-g733a5 From d97ec97b9486fe5798938e6842bda2385e906cbc Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 20 Jul 2023 09:51:47 +0000 Subject: Don't translate compiler-internal bug messages --- compiler/rustc_errors/src/lib.rs | 14 ++++---- compiler/rustc_expand/src/base.rs | 2 +- compiler/rustc_middle/src/ty/sty.rs | 3 +- compiler/rustc_parse/src/parser/diagnostics.rs | 2 +- compiler/rustc_parse/src/parser/pat.rs | 49 +++++++++----------------- compiler/rustc_session/src/session.rs | 2 +- 6 files changed, 27 insertions(+), 45 deletions(-) (limited to 'compiler/rustc_errors/src') diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 31410c39d36..1da02e1bb01 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -1003,7 +1003,7 @@ impl Handler { self.emit_diag_at_span(Diagnostic::new_with_code(Warning(None), Some(code), msg), span); } - pub fn span_bug(&self, span: impl Into, msg: impl Into) -> ! { + pub fn span_bug(&self, span: impl Into, msg: impl Into) -> ! { self.inner.borrow_mut().span_bug(span, msg) } @@ -1012,7 +1012,7 @@ impl Handler { pub fn delay_span_bug( &self, span: impl Into, - msg: impl Into, + msg: impl Into, ) -> ErrorGuaranteed { self.inner.borrow_mut().delay_span_bug(span, msg) } @@ -1596,8 +1596,8 @@ impl HandlerInner { } #[track_caller] - fn span_bug(&mut self, sp: impl Into, msg: impl Into) -> ! { - self.emit_diag_at_span(Diagnostic::new(Bug, msg), sp); + fn span_bug(&mut self, sp: impl Into, msg: impl Into) -> ! { + self.emit_diag_at_span(Diagnostic::new(Bug, msg.into()), sp); panic::panic_any(ExplicitBug); } @@ -1610,7 +1610,7 @@ impl HandlerInner { fn delay_span_bug( &mut self, sp: impl Into, - msg: impl Into, + msg: impl Into, ) -> ErrorGuaranteed { // This is technically `self.treat_err_as_bug()` but `delay_span_bug` is called before // incrementing `err_count` by one, so we need to +1 the comparing. @@ -1619,9 +1619,9 @@ impl HandlerInner { self.err_count() + self.lint_err_count + self.delayed_bug_count() + 1 >= c.get() }) { // FIXME: don't abort here if report_delayed_bugs is off - self.span_bug(sp, msg); + self.span_bug(sp, msg.into()); } - let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg); + let mut diagnostic = Diagnostic::new(Level::DelayedBug, msg.into()); diagnostic.set_span(sp.into()); self.emit_diagnostic(&mut diagnostic).unwrap() } diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 4b0907cf15a..12473a2bb0b 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -1147,7 +1147,7 @@ impl<'a> ExtCtxt<'a> { pub fn span_warn>(&self, sp: S, msg: impl Into) { self.sess.parse_sess.span_diagnostic.span_warn(sp, msg); } - pub fn span_bug>(&self, sp: S, msg: impl Into) -> ! { + pub fn span_bug>(&self, sp: S, msg: impl Into) -> ! { self.sess.parse_sess.span_diagnostic.span_bug(sp, msg); } pub fn trace_macros_diag(&mut self) { diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index cdb0b2240a4..f9c1ca9a8b1 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -15,7 +15,6 @@ use hir::def::DefKind; use polonius_engine::Atom; use rustc_data_structures::captures::Captures; use rustc_data_structures::intern::Interned; -use rustc_error_messages::DiagnosticMessage; use rustc_errors::{DiagnosticArgValue, ErrorGuaranteed, IntoDiagnosticArg, MultiSpan}; use rustc_hir as hir; use rustc_hir::def_id::DefId; @@ -1991,7 +1990,7 @@ impl<'tcx> Ty<'tcx> { pub fn new_error_with_message>( tcx: TyCtxt<'tcx>, span: S, - msg: impl Into, + msg: impl Into, ) -> Ty<'tcx> { let reported = tcx.sess.delay_span_bug(span, msg); Ty::new(tcx, Error(reported)) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 8455803ad0c..c3cf6437afa 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -247,7 +247,7 @@ impl<'a> Parser<'a> { self.sess.span_diagnostic.struct_span_err(sp, m) } - pub fn span_bug>(&self, sp: S, m: impl Into) -> ! { + pub fn span_bug>(&self, sp: S, m: impl Into) -> ! { self.sess.span_diagnostic.span_bug(sp, m) } diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index fdf36517847..14891c45d81 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -8,7 +8,6 @@ use crate::errors::{ TrailingVertNotAllowed, UnexpectedLifetimeInPattern, UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, }; -use crate::fluent_generated as fluent; use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole}; use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor}; use rustc_ast::ptr::P; @@ -214,41 +213,25 @@ impl<'a> Parser<'a> { if let PatKind::Or(pats) = &pat.kind { let span = pat.span; - - if trailing_vert { - // We already emitted an error and suggestion to remove the trailing vert. Don't - // emit again. - - // FIXME(#100717): pass `TopLevelOrPatternNotAllowed::* { sub: None }` to - // `delay_span_bug()` instead of fluent message - self.sess.span_diagnostic.delay_span_bug( - span, - match syntax_loc { - PatternLocation::LetBinding => { - fluent::parse_or_pattern_not_allowed_in_let_binding - } - PatternLocation::FunctionParameter => { - fluent::parse_or_pattern_not_allowed_in_fn_parameters - } - }, - ); + let pat = pprust::pat_to_string(&pat); + let sub = if pats.len() == 1 { + Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { span, pat }) } else { - let pat = pprust::pat_to_string(&pat); - let sub = if pats.len() == 1 { - Some(TopLevelOrPatternNotAllowedSugg::RemoveLeadingVert { span, pat }) - } else { - Some(TopLevelOrPatternNotAllowedSugg::WrapInParens { span, pat }) - }; + Some(TopLevelOrPatternNotAllowedSugg::WrapInParens { span, pat }) + }; - self.sess.emit_err(match syntax_loc { - PatternLocation::LetBinding => { - TopLevelOrPatternNotAllowed::LetBinding { span, sub } - } - PatternLocation::FunctionParameter => { - TopLevelOrPatternNotAllowed::FunctionParameter { span, sub } - } - }); + let mut err = self.sess.create_err(match syntax_loc { + PatternLocation::LetBinding => { + TopLevelOrPatternNotAllowed::LetBinding { span, sub } + } + PatternLocation::FunctionParameter => { + TopLevelOrPatternNotAllowed::FunctionParameter { span, sub } + } + }); + if trailing_vert { + err.delay_as_bug(); } + err.emit(); } Ok((pat, colon)) diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 0f5d3b291db..146bb11bd3a 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -677,7 +677,7 @@ impl Session { pub fn delay_span_bug>( &self, sp: S, - msg: impl Into, + msg: impl Into, ) -> ErrorGuaranteed { self.diagnostic().delay_span_bug(sp, msg) } -- cgit 1.4.1-3-g733a5