Add a proper with_no_queries to printing

This commit is contained in:
John Kåre Alsaker 2024-03-03 04:37:33 +01:00
parent 6db96de66c
commit 6fb4ac64ec
3 changed files with 23 additions and 20 deletions

View File

@ -64,7 +64,7 @@ fn opaque_type_bounds<'tcx>(
item_ty: Ty<'tcx>, item_ty: Ty<'tcx>,
span: Span, span: Span,
) -> &'tcx [(ty::Clause<'tcx>, Span)] { ) -> &'tcx [(ty::Clause<'tcx>, Span)] {
ty::print::with_no_queries!({ ty::print::with_reduced_queries!({
let icx = ItemCtxt::new(tcx, opaque_def_id); let icx = ItemCtxt::new(tcx, opaque_def_id);
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, PredicateFilter::All); let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, PredicateFilter::All);
// Opaque types are implicitly sized unless a `?Sized` bound is found // Opaque types are implicitly sized unless a `?Sized` bound is found

View File

@ -64,7 +64,7 @@ thread_local! {
static SHOULD_PREFIX_WITH_CRATE: Cell<bool> = const { Cell::new(false) }; static SHOULD_PREFIX_WITH_CRATE: Cell<bool> = const { Cell::new(false) };
static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) }; static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) }; static FORCE_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
static NO_QUERIES: Cell<bool> = const { Cell::new(false) }; static REDUCED_QUERIES: Cell<bool> = const { Cell::new(false) };
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) }; static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
} }
@ -102,14 +102,14 @@ macro_rules! define_helper {
} }
define_helper!( define_helper!(
/// Avoids running any queries during any prints that occur /// Avoids running select queries during any prints that occur
/// during the closure. This may alter the appearance of some /// during the closure. This may alter the appearance of some
/// types (e.g. forcing verbose printing for opaque types). /// types (e.g. forcing verbose printing for opaque types).
/// This method is used during some queries (e.g. `explicit_item_bounds` /// This method is used during some queries (e.g. `explicit_item_bounds`
/// for opaque types), to ensure that any debug printing that /// for opaque types), to ensure that any debug printing that
/// occurs during the query computation does not end up recursively /// occurs during the query computation does not end up recursively
/// calling the same query. /// calling the same query.
fn with_no_queries(NoQueriesGuard, NO_QUERIES); fn with_reduced_queries(ReducedQueriesGuard, REDUCED_QUERIES);
/// Force us to name impls with just the filename/line number. We /// Force us to name impls with just the filename/line number. We
/// normally try to use types. But at some points, notably while printing /// normally try to use types. But at some points, notably while printing
/// cycle errors, this can result in extra or suboptimal error output, /// cycle errors, this can result in extra or suboptimal error output,
@ -127,6 +127,15 @@ define_helper!(
fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH); fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH);
); );
/// Avoids running any queries during prints.
pub macro with_no_queries($e:expr) {{
$crate::ty::print::with_reduced_queries!($crate::ty::print::with_forced_impl_filename_line!(
$crate::ty::print::with_no_trimmed_paths!($crate::ty::print::with_no_visible_paths!(
$crate::ty::print::with_forced_impl_filename_line!($e)
))
))
}}
/// The "region highlights" are used to control region printing during /// The "region highlights" are used to control region printing during
/// specific error messages. When a "region highlight" is enabled, it /// specific error messages. When a "region highlight" is enabled, it
/// gives an alternate way to print specific regions. For now, we /// gives an alternate way to print specific regions. For now, we
@ -659,7 +668,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
p!(")") p!(")")
} }
ty::FnDef(def_id, args) => { ty::FnDef(def_id, args) => {
if with_no_queries() { if with_reduced_queries() {
p!(print_def_path(def_id, args)); p!(print_def_path(def_id, args));
} else { } else {
let sig = self.tcx().fn_sig(def_id).instantiate(self.tcx(), args); let sig = self.tcx().fn_sig(def_id).instantiate(self.tcx(), args);
@ -759,7 +768,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
return Ok(()); return Ok(());
} }
_ => { _ => {
if with_no_queries() { if with_reduced_queries() {
p!(print_def_path(def_id, &[])); p!(print_def_path(def_id, &[]));
return Ok(()); return Ok(());
} else { } else {
@ -1876,7 +1885,8 @@ impl DerefMut for FmtPrinter<'_, '_> {
impl<'a, 'tcx> FmtPrinter<'a, 'tcx> { impl<'a, 'tcx> FmtPrinter<'a, 'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, ns: Namespace) -> Self { pub fn new(tcx: TyCtxt<'tcx>, ns: Namespace) -> Self {
let limit = if with_no_queries() { Limit::new(1048576) } else { tcx.type_length_limit() }; let limit =
if with_reduced_queries() { Limit::new(1048576) } else { tcx.type_length_limit() };
Self::new_with_limit(tcx, ns, limit) Self::new_with_limit(tcx, ns, limit)
} }
@ -2962,7 +2972,7 @@ define_print_and_forward_display! {
} }
TraitRefPrintSugared<'tcx> { TraitRefPrintSugared<'tcx> {
if !with_no_queries() if !with_reduced_queries()
&& let Some(kind) = cx.tcx().fn_trait_kind_from_def_id(self.0.def_id) && let Some(kind) = cx.tcx().fn_trait_kind_from_def_id(self.0.def_id)
&& let ty::Tuple(args) = self.0.args.type_at(1).kind() && let ty::Tuple(args) = self.0.args.type_at(1).kind()
{ {
@ -3050,7 +3060,7 @@ define_print_and_forward_display! {
// If we're printing verbosely, or don't want to invoke queries // If we're printing verbosely, or don't want to invoke queries
// (`is_impl_trait_in_trait`), then fall back to printing the def path. // (`is_impl_trait_in_trait`), then fall back to printing the def path.
// This is likely what you want if you're debugging the compiler anyways. // This is likely what you want if you're debugging the compiler anyways.
if !(cx.should_print_verbose() || with_no_queries()) if !(cx.should_print_verbose() || with_reduced_queries())
&& cx.tcx().is_impl_trait_in_trait(self.def_id) && cx.tcx().is_impl_trait_in_trait(self.def_id)
{ {
return cx.pretty_print_opaque_impl_type(self.def_id, self.args); return cx.pretty_print_opaque_impl_type(self.def_id, self.args);

View File

@ -18,7 +18,7 @@ use rustc_middle::query::on_disk_cache::AbsoluteBytePos;
use rustc_middle::query::on_disk_cache::{CacheDecoder, CacheEncoder, EncodedDepNodeIndex}; use rustc_middle::query::on_disk_cache::{CacheDecoder, CacheEncoder, EncodedDepNodeIndex};
use rustc_middle::query::Key; use rustc_middle::query::Key;
use rustc_middle::ty::tls::{self, ImplicitCtxt}; use rustc_middle::ty::tls::{self, ImplicitCtxt};
use rustc_middle::ty::{self, print::with_no_queries, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext}; use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
use rustc_query_system::ich::StableHashingContext; use rustc_query_system::ich::StableHashingContext;
use rustc_query_system::query::{ use rustc_query_system::query::{
@ -305,20 +305,13 @@ pub(crate) fn create_query_frame<
name: &'static str, name: &'static str,
) -> QueryStackFrame { ) -> QueryStackFrame {
// Avoid calling queries while formatting the description // Avoid calling queries while formatting the description
let description = ty::print::with_no_queries!( let description = ty::print::with_no_queries!(do_describe(tcx, key));
// Disable visible paths printing for performance reasons.
// Showing visible path instead of any path is not that important in production.
ty::print::with_no_visible_paths!(
// Force filename-line mode to avoid invoking `type_of` query.
ty::print::with_forced_impl_filename_line!(do_describe(tcx, key))
)
);
let description = if tcx.sess.verbose_internals() { let description = if tcx.sess.verbose_internals() {
format!("{description} [{name:?}]") format!("{description} [{name:?}]")
} else { } else {
description description
}; };
let span = if kind == dep_graph::dep_kinds::def_span || with_no_queries() { let span = if kind == dep_graph::dep_kinds::def_span {
// The `def_span` query is used to calculate `default_span`, // The `def_span` query is used to calculate `default_span`,
// so exit to avoid infinite recursion. // so exit to avoid infinite recursion.
None None
@ -326,7 +319,7 @@ pub(crate) fn create_query_frame<
Some(key.default_span(tcx)) Some(key.default_span(tcx))
}; };
let def_id = key.key_as_def_id(); let def_id = key.key_as_def_id();
let def_kind = if kind == dep_graph::dep_kinds::def_kind || with_no_queries() { let def_kind = if kind == dep_graph::dep_kinds::def_kind {
// Try to avoid infinite recursion. // Try to avoid infinite recursion.
None None
} else { } else {