rustc_middle/util/
bug.rs
1use std::fmt;
4use std::panic::{Location, panic_any};
5
6use rustc_errors::MultiSpan;
7use rustc_span::Span;
8
9use crate::ty::{TyCtxt, tls};
10
11#[cold]
13#[inline(never)]
14#[track_caller]
15pub fn bug_fmt(args: fmt::Arguments<'_>) -> ! {
16 opt_span_bug_fmt(None::<Span>, args, Location::caller());
17}
18
19#[cold]
21#[inline(never)]
22#[track_caller]
23pub fn span_bug_fmt<S: Into<MultiSpan>>(span: S, args: fmt::Arguments<'_>) -> ! {
24 opt_span_bug_fmt(Some(span), args, Location::caller());
25}
26
27#[track_caller]
28fn opt_span_bug_fmt<S: Into<MultiSpan>>(
29 span: Option<S>,
30 args: fmt::Arguments<'_>,
31 location: &Location<'_>,
32) -> ! {
33 tls::with_opt(
34 #[track_caller]
35 move |tcx| {
36 let msg = format!("{location}: {args}");
37 match (tcx, span) {
38 (Some(tcx), Some(span)) => tcx.dcx().span_bug(span, msg),
39 (Some(tcx), None) => tcx.dcx().bug(msg),
40 (None, _) => panic_any(msg),
41 }
42 },
43 )
44}
45
46pub fn trigger_delayed_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
50 tcx.dcx().span_delayed_bug(
51 tcx.def_span(key),
52 "delayed bug triggered by #[rustc_error(delayed_bug_from_inside_query)]",
53 );
54}
55
56pub fn provide(providers: &mut crate::query::Providers) {
57 *providers = crate::query::Providers { trigger_delayed_bug, ..*providers };
58}