2018-08-04 12:51:33 +00:00
|
|
|
// These functions are used by macro expansion for bug! and span_bug!
|
|
|
|
|
2020-03-12 23:07:58 +00:00
|
|
|
use crate::ty::{tls, TyCtxt};
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_errors::MultiSpan;
|
|
|
|
use rustc_span::Span;
|
2018-08-04 12:51:33 +00:00
|
|
|
use std::fmt;
|
2021-02-01 23:17:51 +00:00
|
|
|
use std::panic::{panic_any, Location};
|
2018-08-04 12:51:33 +00:00
|
|
|
|
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
2020-06-15 14:17:58 +00:00
|
|
|
#[track_caller]
|
|
|
|
pub fn bug_fmt(args: fmt::Arguments<'_>) -> ! {
|
2018-08-04 12:51:33 +00:00
|
|
|
// this wrapper mostly exists so I don't have to write a fully
|
|
|
|
// qualified path of None::<Span> inside the bug!() macro definition
|
2020-06-15 14:17:58 +00:00
|
|
|
opt_span_bug_fmt(None::<Span>, args, Location::caller());
|
2018-08-04 12:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
2020-06-15 14:17:58 +00:00
|
|
|
#[track_caller]
|
|
|
|
pub fn span_bug_fmt<S: Into<MultiSpan>>(span: S, args: fmt::Arguments<'_>) -> ! {
|
|
|
|
opt_span_bug_fmt(Some(span), args, Location::caller());
|
2018-08-04 12:51:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 04:16:40 +00:00
|
|
|
#[track_caller]
|
2018-08-04 12:51:33 +00:00
|
|
|
fn opt_span_bug_fmt<S: Into<MultiSpan>>(
|
|
|
|
span: Option<S>,
|
2018-08-30 05:02:42 +00:00
|
|
|
args: fmt::Arguments<'_>,
|
2020-06-15 14:17:58 +00:00
|
|
|
location: &Location<'_>,
|
2018-08-04 12:51:33 +00:00
|
|
|
) -> ! {
|
|
|
|
tls::with_opt(move |tcx| {
|
2023-07-25 20:00:13 +00:00
|
|
|
let msg = format!("{location}: {args}");
|
2018-08-04 12:51:33 +00:00
|
|
|
match (tcx, span) {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 03:26:58 +00:00
|
|
|
(Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, msg),
|
|
|
|
(Some(tcx), None) => tcx.sess.diagnostic().bug(msg),
|
2021-02-01 23:17:51 +00:00
|
|
|
(None, _) => panic_any(msg),
|
2018-08-04 12:51:33 +00:00
|
|
|
}
|
2022-12-30 12:38:34 +00:00
|
|
|
})
|
2018-08-04 12:51:33 +00:00
|
|
|
}
|
2020-03-12 23:07:58 +00:00
|
|
|
|
2023-11-30 04:01:11 +00:00
|
|
|
/// A query to trigger a `span_delayed_bug`. Clearly, if one has a `tcx` one can already trigger a
|
|
|
|
/// `span_delayed_bug`, so what is the point of this? It exists to help us test `span_delayed_bug`'s
|
2020-03-12 23:07:58 +00:00
|
|
|
/// interactions with the query system and incremental.
|
2023-11-30 04:01:11 +00:00
|
|
|
pub fn trigger_span_delayed_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
|
|
|
|
tcx.sess.span_delayed_bug(
|
2020-03-12 23:07:58 +00:00
|
|
|
tcx.def_span(key),
|
2023-11-30 04:01:11 +00:00
|
|
|
"delayed span bug triggered by #[rustc_error(span_delayed_bug_from_inside_query)]",
|
2020-03-12 23:07:58 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-15 04:24:45 +00:00
|
|
|
pub fn provide(providers: &mut crate::query::Providers) {
|
2023-11-30 04:01:11 +00:00
|
|
|
*providers = crate::query::Providers { trigger_span_delayed_bug, ..*providers };
|
2020-03-12 23:07:58 +00:00
|
|
|
}
|