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