Skip to content

Commit 36b4199

Browse files
committed
Don't rely on Debug impl for Erased
1 parent 785459d commit 36b4199

File tree

7 files changed

+33
-23
lines changed

7 files changed

+33
-23
lines changed

compiler/rustc_middle/src/query/erase.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
use crate::ty;
22
use std::intrinsics::type_name;
3-
use std::{
4-
fmt,
5-
mem::{size_of, transmute_copy, MaybeUninit},
6-
};
3+
use std::mem::{size_of, transmute_copy, MaybeUninit};
74

85
#[derive(Copy, Clone)]
96
pub struct Erased<T: Copy> {
107
data: MaybeUninit<T>,
118
}
129

13-
impl<T: Copy> fmt::Debug for Erased<T> {
14-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15-
write!(f, "Erased")
16-
}
17-
}
18-
1910
pub trait EraseType: Copy {
2011
type Result: Copy;
2112
}

compiler/rustc_middle/src/ty/query.rs

+1
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ macro_rules! define_feedable {
501501

502502
match try_get_cached(tcx, cache, &key) {
503503
Some(old) => {
504+
let old = restore::<$V>(old);
504505
bug!(
505506
"Trying to feed an already recorded value for query {} key={key:?}:\nold value: {old:?}\nnew value: {value:?}",
506507
stringify!($name),

compiler/rustc_query_impl/src/plumbing.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,11 @@ macro_rules! define_queries {
486486
stringify!($name)
487487
}
488488

489+
#[inline]
490+
fn format_value(self) -> fn(&Self::Value) -> String {
491+
|value| format!("{:?}", restore::<query_values::$name<'tcx>>(*value))
492+
}
493+
489494
#[inline]
490495
fn cache_on_disk(self, tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
491496
::rustc_middle::query::cached::$name(tcx, key)
@@ -819,7 +824,7 @@ macro_rules! define_queries_struct {
819824

820825
$($(#[$attr])*
821826
#[inline(always)]
822-
#[tracing::instrument(level = "trace", skip(self, tcx), ret)]
827+
#[tracing::instrument(level = "trace", skip(self, tcx))]
823828
fn $name(
824829
&'tcx self,
825830
tcx: TyCtxt<'tcx>,

compiler/rustc_query_system/src/dep_graph/graph.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,14 @@ impl<K: DepKind> DepGraph<K> {
538538
if let Some(prev_index) = data.previous.node_to_index_opt(&node) {
539539
let dep_node_index = data.current.prev_index_to_index.lock()[prev_index];
540540
if let Some(dep_node_index) = dep_node_index {
541-
crate::query::incremental_verify_ich(cx, data, result, prev_index, hash_result);
541+
crate::query::incremental_verify_ich(
542+
cx,
543+
data,
544+
result,
545+
prev_index,
546+
hash_result,
547+
|value| format!("{:?}", value),
548+
);
542549

543550
#[cfg(debug_assertions)]
544551
if hash_result.is_some() {

compiler/rustc_query_system/src/query/caches.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub trait CacheSelector<'tcx, V> {
1818

1919
pub trait QueryCache: Sized {
2020
type Key: Hash + Eq + Copy + Debug;
21-
type Value: Copy + Debug;
21+
type Value: Copy;
2222

2323
/// Checks if the query is already computed and in the cache.
2424
fn lookup(&self, key: &Self::Key) -> Option<(Self::Value, DepNodeIndex)>;
@@ -52,7 +52,7 @@ impl<K, V> Default for DefaultCache<K, V> {
5252
impl<K, V> QueryCache for DefaultCache<K, V>
5353
where
5454
K: Eq + Hash + Copy + Debug,
55-
V: Copy + Debug,
55+
V: Copy,
5656
{
5757
type Key = K;
5858
type Value = V;
@@ -120,7 +120,7 @@ impl<V> Default for SingleCache<V> {
120120

121121
impl<V> QueryCache for SingleCache<V>
122122
where
123-
V: Copy + Debug,
123+
V: Copy,
124124
{
125125
type Key = ();
126126
type Value = V;
@@ -164,7 +164,7 @@ impl<K: Idx, V> Default for VecCache<K, V> {
164164
impl<K, V> QueryCache for VecCache<K, V>
165165
where
166166
K: Eq + Idx + Copy + Debug,
167-
V: Copy + Debug,
167+
V: Copy,
168168
{
169169
type Key = K;
170170
type Value = V;

compiler/rustc_query_system/src/query/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ pub trait QueryConfig<Qcx: QueryContext>: Copy {
2020
// `Key` and `Value` are `Copy` instead of `Clone` to ensure copying them stays cheap,
2121
// but it isn't necessary.
2222
type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Copy + Debug;
23-
type Value: Debug + Copy;
23+
type Value: Copy;
2424

2525
type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
2626

27+
fn format_value(self) -> fn(&Self::Value) -> String;
28+
2729
// Don't use this method to access query results, instead use the methods on TyCtxt
2830
fn query_state<'a>(self, tcx: Qcx) -> &'a QueryState<Self::Key, Qcx::DepKind>
2931
where

compiler/rustc_query_system/src/query/plumbing.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,8 @@ where
411411
// get evaluated first, and re-feed the query.
412412
if let Some((cached_result, _)) = cache.lookup(&key) {
413413
panic!(
414-
"fed query later has its value computed. The already cached value: {cached_result:?}"
414+
"fed query later has its value computed. The already cached value: {}",
415+
(query.format_value())(&cached_result)
415416
);
416417
}
417418
}
@@ -582,6 +583,7 @@ where
582583
&result,
583584
prev_dep_node_index,
584585
query.hash_result(),
586+
query.format_value(),
585587
);
586588
}
587589

@@ -627,19 +629,21 @@ where
627629
&result,
628630
prev_dep_node_index,
629631
query.hash_result(),
632+
query.format_value(),
630633
);
631634

632635
Some((result, dep_node_index))
633636
}
634637

635638
#[inline]
636-
#[instrument(skip(tcx, dep_graph_data, result, hash_result), level = "debug")]
637-
pub(crate) fn incremental_verify_ich<Tcx, V: Debug>(
639+
#[instrument(skip(tcx, dep_graph_data, result, hash_result, format_value), level = "debug")]
640+
pub(crate) fn incremental_verify_ich<Tcx, V>(
638641
tcx: Tcx,
639642
dep_graph_data: &DepGraphData<Tcx::DepKind>,
640643
result: &V,
641644
prev_index: SerializedDepNodeIndex,
642645
hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
646+
format_value: fn(&V) -> String,
643647
) where
644648
Tcx: DepContext,
645649
{
@@ -654,7 +658,7 @@ pub(crate) fn incremental_verify_ich<Tcx, V: Debug>(
654658
let old_hash = dep_graph_data.prev_fingerprint_of(prev_index);
655659

656660
if new_hash != old_hash {
657-
incremental_verify_ich_failed(tcx, prev_index, result);
661+
incremental_verify_ich_failed(tcx, prev_index, &|| format_value(&result));
658662
}
659663
}
660664

@@ -678,7 +682,7 @@ where
678682
fn incremental_verify_ich_failed<Tcx>(
679683
tcx: Tcx,
680684
prev_index: SerializedDepNodeIndex,
681-
result: &dyn Debug,
685+
result: &dyn Fn() -> String,
682686
) where
683687
Tcx: DepContext,
684688
{
@@ -708,7 +712,7 @@ fn incremental_verify_ich_failed<Tcx>(
708712
run_cmd,
709713
dep_node: format!("{dep_node:?}"),
710714
});
711-
panic!("Found unstable fingerprints for {dep_node:?}: {result:?}");
715+
panic!("Found unstable fingerprints for {dep_node:?}: {}", result());
712716
}
713717

714718
INSIDE_VERIFY_PANIC.with(|in_panic| in_panic.set(old_in_panic));

0 commit comments

Comments
 (0)