Skip to content

Commit 2863298

Browse files
committed
De-weirdify fatally_break_rust.
The easter egg ICE on `break rust` is weird: it's the one ICE in the entire compiler that doesn't immediately abort, which makes it annoyingly inconsistent. This commit changes it to abort. As part of this, the extra notes are now appended onto the bug dignostic, rather than being printed as individual note diagnostics, which changes the output format a bit. These changes don't interferes with the joke, but they do help with my ongoing cleanups to error handling.
1 parent 072c157 commit 2863298

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

compiler/rustc_errors/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,19 @@ impl DiagCtxt {
968968
DiagnosticBuilder::new(self, Level::Bug, msg)
969969
}
970970

971+
/// Construct a builder at the `Bug` level at the given `span` with the `msg`.
972+
#[rustc_lint_diagnostics]
973+
#[track_caller]
974+
pub fn struct_span_bug(
975+
&self,
976+
span: impl Into<MultiSpan>,
977+
msg: impl Into<DiagnosticMessage>,
978+
) -> DiagnosticBuilder<'_, BugAbort> {
979+
let mut result = self.struct_bug(msg);
980+
result.set_span(span);
981+
result
982+
}
983+
971984
#[rustc_lint_diagnostics]
972985
#[track_caller]
973986
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
722722
if let [segment] = path.segments
723723
&& segment.ident.name == sym::rust
724724
{
725-
fatally_break_rust(self.tcx);
725+
fatally_break_rust(self.tcx, expr.span);
726726
}
727727
}
728728
}

compiler/rustc_hir_typeck/src/lib.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use crate::expectation::Expectation;
5252
use crate::fn_ctxt::RawTy;
5353
use crate::gather_locals::GatherLocalsVisitor;
5454
use rustc_data_structures::unord::UnordSet;
55-
use rustc_errors::{struct_span_err, DiagnosticId, ErrorGuaranteed, MultiSpan};
55+
use rustc_errors::{struct_span_err, DiagnosticId, ErrorGuaranteed};
5656
use rustc_hir as hir;
5757
use rustc_hir::def::{DefKind, Res};
5858
use rustc_hir::intravisit::Visitor;
@@ -412,24 +412,25 @@ enum TupleArgumentsFlag {
412412
TupleArguments,
413413
}
414414

415-
fn fatally_break_rust(tcx: TyCtxt<'_>) {
415+
fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
416416
let dcx = tcx.sess.dcx();
417-
dcx.span_bug_no_panic(
418-
MultiSpan::new(),
417+
let mut diag = dcx.struct_span_bug(
418+
span,
419419
"It looks like you're trying to break rust; would you like some ICE?",
420420
);
421-
dcx.note("the compiler expectedly panicked. this is a feature.");
422-
dcx.note(
421+
diag.note("the compiler expectedly panicked. this is a feature.");
422+
diag.note(
423423
"we would appreciate a joke overview: \
424424
https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675",
425425
);
426-
dcx.note(format!("rustc {} running on {}", tcx.sess.cfg_version, config::host_triple(),));
426+
diag.note(format!("rustc {} running on {}", tcx.sess.cfg_version, config::host_triple(),));
427427
if let Some((flags, excluded_cargo_defaults)) = rustc_session::utils::extra_compiler_flags() {
428-
dcx.note(format!("compiler flags: {}", flags.join(" ")));
428+
diag.note(format!("compiler flags: {}", flags.join(" ")));
429429
if excluded_cargo_defaults {
430-
dcx.note("some of the compiler flags provided by cargo are hidden");
430+
diag.note("some of the compiler flags provided by cargo are hidden");
431431
}
432432
}
433+
diag.emit()
433434
}
434435

435436
/// `expected` here is the expected number of explicit generic arguments on the trait.

tests/ui/track-diagnostics/track.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// compile-flags: -Z track-diagnostics
22
// error-pattern: created at
3+
// rustc-env:RUST_BACKTRACE=0
4+
// failure-status: 101
35

46
// Normalize the emitted location so this doesn't need
57
// updating everytime someone adds or removes a line.

tests/ui/track-diagnostics/track.stderr

+18-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,31 @@ LL | break rust
1313
-Ztrack-diagnostics: created at compiler/rustc_passes/src/loops.rs:LL:CC
1414

1515
error: internal compiler error: It looks like you're trying to break rust; would you like some ICE?
16+
--> $DIR/track.rs:LL:CC
17+
|
18+
LL | break rust
19+
| ^^^^^^^^^^
20+
-Ztrack-diagnostics: created at compiler/rustc_hir_typeck/src/lib.rs:LL:CC
21+
|
22+
= note: the compiler expectedly panicked. this is a feature.
23+
= note: we would appreciate a joke overview: https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675
24+
= note: rustc $VERSION running on $TARGET
25+
= note: compiler flags: ... -Z ui-testing ... -Z track-diagnostics
1626

17-
note: the compiler expectedly panicked. this is a feature.
27+
thread 'rustc' panicked at compiler/rustc_hir_typeck/src/lib.rs:LL:CC:
28+
Box<dyn Any>
29+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1830

19-
note: we would appreciate a joke overview: https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675
31+
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
2032

2133
note: rustc $VERSION running on $TARGET
2234

2335
note: compiler flags: ... -Z ui-testing ... -Z track-diagnostics
2436

37+
query stack during panic:
38+
#0 [typeck] type-checking `main`
39+
#1 [analysis] running analysis passes on this crate
40+
end of query stack
2541
error: aborting due to 3 previous errors
2642

2743
Some errors have detailed explanations: E0268, E0425.

0 commit comments

Comments
 (0)