summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_middle/src/util
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /compiler/rustc_middle/src/util
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_middle/src/util')
-rw-r--r--compiler/rustc_middle/src/util/bug.rs54
-rw-r--r--compiler/rustc_middle/src/util/common.rs67
-rw-r--r--compiler/rustc_middle/src/util/common/tests.rs14
3 files changed, 135 insertions, 0 deletions
diff --git a/compiler/rustc_middle/src/util/bug.rs b/compiler/rustc_middle/src/util/bug.rs
new file mode 100644
index 000000000..fd7045d6a
--- /dev/null
+++ b/compiler/rustc_middle/src/util/bug.rs
@@ -0,0 +1,54 @@
+// These functions are used by macro expansion for bug! and span_bug!
+
+use crate::ty::{tls, TyCtxt};
+use rustc_errors::MultiSpan;
+use rustc_span::Span;
+use std::fmt;
+use std::panic::{panic_any, Location};
+
+#[cold]
+#[inline(never)]
+#[track_caller]
+pub fn bug_fmt(args: fmt::Arguments<'_>) -> ! {
+ // this wrapper mostly exists so I don't have to write a fully
+ // qualified path of None::<Span> inside the bug!() macro definition
+ opt_span_bug_fmt(None::<Span>, args, Location::caller());
+}
+
+#[cold]
+#[inline(never)]
+#[track_caller]
+pub fn span_bug_fmt<S: Into<MultiSpan>>(span: S, args: fmt::Arguments<'_>) -> ! {
+ opt_span_bug_fmt(Some(span), args, Location::caller());
+}
+
+#[track_caller]
+fn opt_span_bug_fmt<S: Into<MultiSpan>>(
+ span: Option<S>,
+ args: fmt::Arguments<'_>,
+ location: &Location<'_>,
+) -> ! {
+ tls::with_opt(move |tcx| {
+ let msg = format!("{}: {}", location, args);
+ match (tcx, span) {
+ (Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, &msg),
+ (Some(tcx), None) => tcx.sess.diagnostic().bug(&msg),
+ (None, _) => panic_any(msg),
+ }
+ });
+ unreachable!();
+}
+
+/// A query to trigger a `delay_span_bug`. Clearly, if one has a `tcx` one can already trigger a
+/// `delay_span_bug`, so what is the point of this? It exists to help us test `delay_span_bug`'s
+/// interactions with the query system and incremental.
+pub fn trigger_delay_span_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
+ tcx.sess.delay_span_bug(
+ tcx.def_span(key),
+ "delayed span bug triggered by #[rustc_error(delay_span_bug_from_inside_query)]",
+ );
+}
+
+pub fn provide(providers: &mut crate::ty::query::Providers) {
+ *providers = crate::ty::query::Providers { trigger_delay_span_bug, ..*providers };
+}
diff --git a/compiler/rustc_middle/src/util/common.rs b/compiler/rustc_middle/src/util/common.rs
new file mode 100644
index 000000000..08977049d
--- /dev/null
+++ b/compiler/rustc_middle/src/util/common.rs
@@ -0,0 +1,67 @@
+use rustc_data_structures::sync::Lock;
+
+use std::fmt::Debug;
+use std::time::{Duration, Instant};
+
+#[cfg(test)]
+mod tests;
+
+pub fn to_readable_str(mut val: usize) -> String {
+ let mut groups = vec![];
+ loop {
+ let group = val % 1000;
+
+ val /= 1000;
+
+ if val == 0 {
+ groups.push(group.to_string());
+ break;
+ } else {
+ groups.push(format!("{:03}", group));
+ }
+ }
+
+ groups.reverse();
+
+ groups.join("_")
+}
+
+pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T
+where
+ F: FnOnce() -> T,
+{
+ let start = Instant::now();
+ let rv = f();
+ let duration = start.elapsed();
+ let mut accu = accu.lock();
+ *accu += duration;
+ rv
+}
+
+pub fn indent<R, F>(op: F) -> R
+where
+ R: Debug,
+ F: FnOnce() -> R,
+{
+ // Use in conjunction with the log post-processor like `src/etc/indenter`
+ // to make debug output more readable.
+ debug!(">>");
+ let r = op();
+ debug!("<< (Result = {:?})", r);
+ r
+}
+
+pub struct Indenter {
+ _cannot_construct_outside_of_this_module: (),
+}
+
+impl Drop for Indenter {
+ fn drop(&mut self) {
+ debug!("<<");
+ }
+}
+
+pub fn indenter() -> Indenter {
+ debug!(">>");
+ Indenter { _cannot_construct_outside_of_this_module: () }
+}
diff --git a/compiler/rustc_middle/src/util/common/tests.rs b/compiler/rustc_middle/src/util/common/tests.rs
new file mode 100644
index 000000000..9a9fb203c
--- /dev/null
+++ b/compiler/rustc_middle/src/util/common/tests.rs
@@ -0,0 +1,14 @@
+use super::*;
+
+#[test]
+fn test_to_readable_str() {
+ assert_eq!("0", to_readable_str(0));
+ assert_eq!("1", to_readable_str(1));
+ assert_eq!("99", to_readable_str(99));
+ assert_eq!("999", to_readable_str(999));
+ assert_eq!("1_000", to_readable_str(1_000));
+ assert_eq!("1_001", to_readable_str(1_001));
+ assert_eq!("999_999", to_readable_str(999_999));
+ assert_eq!("1_000_000", to_readable_str(1_000_000));
+ assert_eq!("1_234_567", to_readable_str(1_234_567));
+}