Skip to content

Ensure query keys are printed with reduced queries #141985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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())
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_query_system/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_query_system/src/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions tests/incremental/print-dep-node-cycle.rs
Original file line number Diff line number Diff line change
@@ -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 }>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading