]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/util/bug.rs
Rollup merge of #84083 - ltratt:threadid_doc_tweak, r=dtolnay
[rust.git] / compiler / rustc_middle / src / util / bug.rs
1 // These functions are used by macro expansion for bug! and span_bug!
2
3 use crate::ty::{tls, TyCtxt};
4 use rustc_span::{MultiSpan, Span};
5 use std::fmt;
6 use std::panic::{panic_any, Location};
7
8 #[cold]
9 #[inline(never)]
10 #[track_caller]
11 pub fn bug_fmt(args: fmt::Arguments<'_>) -> ! {
12     // this wrapper mostly exists so I don't have to write a fully
13     // qualified path of None::<Span> inside the bug!() macro definition
14     opt_span_bug_fmt(None::<Span>, args, Location::caller());
15 }
16
17 #[cold]
18 #[inline(never)]
19 #[track_caller]
20 pub fn span_bug_fmt<S: Into<MultiSpan>>(span: S, args: fmt::Arguments<'_>) -> ! {
21     opt_span_bug_fmt(Some(span), args, Location::caller());
22 }
23
24 #[track_caller]
25 fn opt_span_bug_fmt<S: Into<MultiSpan>>(
26     span: Option<S>,
27     args: fmt::Arguments<'_>,
28     location: &Location<'_>,
29 ) -> ! {
30     tls::with_opt(move |tcx| {
31         let msg = format!("{}: {}", location, args);
32         match (tcx, span) {
33             (Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg),
34             (Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),
35             (None, _) => panic_any(msg),
36         }
37     });
38     unreachable!();
39 }
40
41 /// A query to trigger a `delay_span_bug`. Clearly, if one has a `tcx` one can already trigger a
42 /// `delay_span_bug`, so what is the point of this? It exists to help us test `delay_span_bug`'s
43 /// interactions with the query system and incremental.
44 pub fn trigger_delay_span_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
45     tcx.sess.delay_span_bug(
46         tcx.def_span(key),
47         "delayed span bug triggered by #[rustc_error(delay_span_bug_from_inside_query)]",
48     );
49 }
50
51 pub fn provide(providers: &mut crate::ty::query::Providers) {
52     *providers = crate::ty::query::Providers { trigger_delay_span_bug, ..*providers };
53 }