Skip to content

Commit 0047e25

Browse files
committed
Add some docs to bug, span_bug and delay_span_bug
1 parent e5e5fcb commit 0047e25

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

compiler/rustc_errors/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ impl Handler {
973973
self.inner.borrow_mut().span_bug(span, msg)
974974
}
975975

976+
/// For documentation on this, see `Session::delay_span_bug`.
976977
#[track_caller]
977978
pub fn delay_span_bug(
978979
&self,
@@ -1518,6 +1519,7 @@ impl HandlerInner {
15181519
self.emit_diagnostic(diag.set_span(sp));
15191520
}
15201521

1522+
/// For documentation on this, see `Session::delay_span_bug`.
15211523
#[track_caller]
15221524
fn delay_span_bug(
15231525
&mut self,

compiler/rustc_middle/src/macros.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
/// A macro for triggering an ICE.
2+
/// Calling `bug` instead of panicking will result in a nicer error message and should
3+
/// therefore be prefered over `panic`/`unreachable` or others.
4+
///
5+
/// If you have a span available, you should use [`span_bug`] instead.
6+
///
7+
/// If the bug should only be emitted when compilation didn't fail, [`Session::delay_span_bug`] may be useful.
8+
///
9+
/// [`Session::delay_span_bug`]: rustc_session::Session::delay_span_bug
10+
/// [`span_bug`]: crate::span_bug
111
#[macro_export]
212
macro_rules! bug {
313
() => ( $crate::bug!("impossible case reached") );
@@ -8,6 +18,14 @@ macro_rules! bug {
818
});
919
}
1020

21+
/// A macro for triggering an ICE with a span.
22+
/// Calling `span_bug!` instead of panicking will result in a nicer error message and point
23+
/// at the code the compiler was compiling when it ICEd. This is the preferred way to trigger
24+
/// ICEs.
25+
///
26+
/// If the bug should only be emitted when compilation didn't fail, [`Session::delay_span_bug`] may be useful.
27+
///
28+
/// [`Session::delay_span_bug`]: rustc_session::Session::delay_span_bug
1129
#[macro_export]
1230
macro_rules! span_bug {
1331
($span:expr, $msg:expr) => ({ $crate::util::bug::span_bug_fmt($span, ::std::format_args!($msg)) });

compiler/rustc_middle/src/util/bug.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ fn opt_span_bug_fmt<S: Into<MultiSpan>>(
3535
(Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),
3636
(None, _) => panic_any(msg),
3737
}
38-
});
39-
unreachable!();
38+
})
4039
}
4140

4241
/// A query to trigger a `delay_span_bug`. Clearly, if one has a `tcx` one can already trigger a

compiler/rustc_session/src/session.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,19 @@ impl Session {
590590
pub fn warn(&self, msg: impl Into<DiagnosticMessage>) {
591591
self.diagnostic().warn(msg)
592592
}
593-
/// Delay a span_bug() call until abort_if_errors()
593+
594+
/// Ensures that compilation cannot succeed.
595+
///
596+
/// If this function has been called but no errors have been emitted and
597+
/// compilation succeeds, it will cause an internal compiler error (ICE).
598+
///
599+
/// This can be used in code paths that should never run on successful compilations.
600+
/// For example, it can be used to create an [`ErrorGuaranteed`]
601+
/// (but you should prefer threading through the [`ErrorGuaranteed`] from an error emission directly).
602+
///
603+
/// If no span is available, use [`DUMMY_SP`].
604+
///
605+
/// [`DUMMY_SP`]: rustc_span::DUMMY_SP
594606
#[track_caller]
595607
pub fn delay_span_bug<S: Into<MultiSpan>>(
596608
&self,

0 commit comments

Comments
 (0)