]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_middle/src/util/bug.rs
New upstream version 1.48.0~beta.8+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_span::{MultiSpan, Span};
5 use std::fmt;
6 use std::panic::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 fn opt_span_bug_fmt<S: Into<MultiSpan>>(
25 span: Option<S>,
26 args: fmt::Arguments<'_>,
27 location: &Location<'_>,
28 ) -> ! {
29 tls::with_opt(move |tcx| {
30 let msg = format!("{}: {}", location, args);
31 match (tcx, span) {
32 (Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg),
33 (Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),
34 (None, _) => panic!(msg),
35 }
36 });
37 unreachable!();
38 }
39
40 /// A query to trigger a `delay_span_bug`. Clearly, if one has a `tcx` one can already trigger a
41 /// `delay_span_bug`, so what is the point of this? It exists to help us test `delay_span_bug`'s
42 /// interactions with the query system and incremental.
43 pub fn trigger_delay_span_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
44 tcx.sess.delay_span_bug(
45 tcx.def_span(key),
46 "delayed span bug triggered by #[rustc_error(delay_span_bug_from_inside_query)]",
47 );
48 }
49
50 pub fn provide(providers: &mut crate::ty::query::Providers) {
51 *providers = crate::ty::query::Providers { trigger_delay_span_bug, ..*providers };
52 }