about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-06-04 19:50:24 +0200
committerGitHub <noreply@github.com>2025-06-04 19:50:24 +0200
commit23ba088502f4e05a9f79cfa6e6a3494161d5f09e (patch)
tree1b3afdafaed098aa309fe99423005f127c80805f
parent8cd8ed30508979985ab026de960feca874b49cef (diff)
parentcbb3a847d25f998486248a8db201773eeb489d60 (diff)
downloadrust-23ba088502f4e05a9f79cfa6e6a3494161d5f09e.tar.gz
rust-23ba088502f4e05a9f79cfa6e6a3494161d5f09e.zip
Rollup merge of #141985 - compiler-errors:cycle-in-dep-graph-print, r=oli-obk
Ensure query keys are printed with reduced queries

Using `-Z query-dep-graph` and debug assertions leads to an ICE that was originally discovered in rust-lang/rust#141700:

> This isn't an incremental bug per se, but instead a bug that has to do with debug printing query keys when debug assertions and `-Z query-dep-graph` is enabled. We end up printing a const (b/c we're using generic const args here) whose debug printing for -Z query-dep-graph requires invoking the same query cyclically 😃
>
> I've pushed a commit which should fix this.

This isn't related to the standard library changes, but instead b/c it seems to be the first usage of `feature(adt_const_params)` in the standard library that ends up being triggered in incremental tests.

r? oli-obk
-rw-r--r--compiler/rustc_middle/src/dep_graph/mod.rs5
-rw-r--r--compiler/rustc_middle/src/ty/print/pretty.rs2
-rw-r--r--compiler/rustc_query_system/src/dep_graph/dep_node.rs11
-rw-r--r--compiler/rustc_query_system/src/dep_graph/mod.rs2
-rw-r--r--tests/incremental/print-dep-node-cycle.rs25
-rw-r--r--tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr2
6 files changed, 40 insertions, 7 deletions
diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs
index 931d67087ac..0a8e6153817 100644
--- a/compiler/rustc_middle/src/dep_graph/mod.rs
+++ b/compiler/rustc_middle/src/dep_graph/mod.rs
@@ -2,6 +2,7 @@ use rustc_data_structures::profiling::SelfProfilerRef;
 use rustc_query_system::ich::StableHashingContext;
 use rustc_session::Session;
 
+use crate::ty::print::with_reduced_queries;
 use crate::ty::{self, TyCtxt};
 
 #[macro_use]
@@ -84,4 +85,8 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
     fn dep_kind_info(&self, dk: DepKind) -> &DepKindStruct<'tcx> {
         &self.query_kinds[dk.as_usize()]
     }
+
+    fn with_reduced_queries<T>(self, f: impl FnOnce() -> T) -> T {
+        with_reduced_queries!(f())
+    }
 }
diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs
index 877bea095f9..673a89a8134 100644
--- a/compiler/rustc_middle/src/ty/print/pretty.rs
+++ b/compiler/rustc_middle/src/ty/print/pretty.rs
@@ -1886,7 +1886,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
     ) -> Result<(), PrintError> {
         define_scoped_cx!(self);
 
-        if self.should_print_verbose() {
+        if with_reduced_queries() || self.should_print_verbose() {
             p!(write("ValTree({:?}: ", cv.valtree), print(cv.ty), ")");
             return Ok(());
         }
diff --git a/compiler/rustc_query_system/src/dep_graph/dep_node.rs b/compiler/rustc_query_system/src/dep_graph/dep_node.rs
index c0b3bd43e25..bdd1d5f3e88 100644
--- a/compiler/rustc_query_system/src/dep_graph/dep_node.rs
+++ b/compiler/rustc_query_system/src/dep_graph/dep_node.rs
@@ -178,9 +178,7 @@ pub trait DepNodeParams<Tcx: DepContext>: fmt::Debug + Sized {
         panic!("Not implemented. Accidentally called on anonymous node?")
     }
 
-    fn to_debug_str(&self, _: Tcx) -> String {
-        format!("{self:?}")
-    }
+    fn to_debug_str(&self, tcx: Tcx) -> String;
 
     /// This method tries to recover the query key from the given `DepNode`,
     /// something which is needed when forcing `DepNode`s during red-green
@@ -210,8 +208,11 @@ where
     }
 
     #[inline(always)]
-    default fn to_debug_str(&self, _: Tcx) -> String {
-        format!("{:?}", *self)
+    default fn to_debug_str(&self, tcx: Tcx) -> String {
+        // Make sure to print dep node params with reduced queries since printing
+        // may themselves call queries, which may lead to (possibly untracked!)
+        // query cycles.
+        tcx.with_reduced_queries(|| format!("{self:?}"))
     }
 
     #[inline(always)]
diff --git a/compiler/rustc_query_system/src/dep_graph/mod.rs b/compiler/rustc_query_system/src/dep_graph/mod.rs
index 89d1db87809..512034a8b2f 100644
--- a/compiler/rustc_query_system/src/dep_graph/mod.rs
+++ b/compiler/rustc_query_system/src/dep_graph/mod.rs
@@ -88,6 +88,8 @@ pub trait DepContext: Copy {
             f(self, dep_node)
         }
     }
+
+    fn with_reduced_queries<T>(self, _: impl FnOnce() -> T) -> T;
 }
 
 pub trait Deps: DynSync {
diff --git a/tests/incremental/print-dep-node-cycle.rs b/tests/incremental/print-dep-node-cycle.rs
new file mode 100644
index 00000000000..931d3da521e
--- /dev/null
+++ b/tests/incremental/print-dep-node-cycle.rs
@@ -0,0 +1,25 @@
+//@ compile-flags: -Z query-dep-graph
+//@ revisions: rpass1
+
+// Exercises a debug-assertions-only query cycle that when printing a valtree const in
+// a dep node's debug representation, we end up invoking a query that also has a valtree
+// const in its dep node's debug representation, which leads to a cycle (and ICE, since
+// deps are not tracked when printing dep nodes' debug representations).
+
+#![feature(adt_const_params)]
+
+use std::marker::ConstParamTy;
+
+#[derive(Debug, ConstParamTy, PartialEq, Eq)]
+enum Foo {
+    A1,
+}
+
+#[inline(never)]
+fn hello<const F: Foo>() {
+    println!("{:#?}", F);
+}
+
+fn main() {
+    hello::<{ Foo::A1 }>();
+}
diff --git a/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr b/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr
index bdf2d3b6a58..1a0563b469c 100644
--- a/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr
+++ b/tests/ui/transmutability/structs/repr/transmute_infinitely_recursive_type.stderr
@@ -12,7 +12,7 @@ LL |     struct ExplicitlyPadded(Box<ExplicitlyPadded>);
 error[E0391]: cycle detected when computing layout of `should_pad_explicitly_packed_field::ExplicitlyPadded`
    |
    = note: ...which immediately requires computing layout of `should_pad_explicitly_packed_field::ExplicitlyPadded` again
-   = note: cycle used when evaluating trait selection obligation `(): core::mem::transmutability::TransmuteFrom<should_pad_explicitly_packed_field::ExplicitlyPadded, core::mem::transmutability::Assume { alignment: false, lifetimes: false, safety: false, validity: false }>`
+   = note: cycle used when evaluating trait selection obligation `(): core::mem::transmutability::TransmuteFrom<should_pad_explicitly_packed_field::ExplicitlyPadded, ValTree(Branch([Leaf(0x00), Leaf(0x00), Leaf(0x00), Leaf(0x00)]): core::mem::transmutability::Assume)>`
    = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
 
 error: aborting due to 2 previous errors