1
Fork 0

Move ty::print methods to Drop-based scope guards

This commit is contained in:
Mark Rousskov 2022-02-16 13:04:48 -05:00
parent 393fdc1048
commit 9763486034
30 changed files with 142 additions and 142 deletions

View file

@ -55,6 +55,7 @@
#![feature(try_reserve_kind)]
#![feature(nonzero_ops)]
#![feature(unwrap_infallible)]
#![feature(decl_macro)]
#![recursion_limit = "512"]
#![cfg_attr(not(bootstrap), allow(rustc::potential_query_instability))]

View file

@ -367,7 +367,7 @@ impl<'tcx> TyCtxt<'tcx> {
let is_in_effect = deprecation_in_effect(depr_attr);
let lint = deprecation_lint(is_in_effect);
if self.lint_level_at_node(lint, id).0 != Level::Allow {
let def_path = &with_no_trimmed_paths(|| self.def_path_str(def_id));
let def_path = with_no_trimmed_paths!(self.def_path_str(def_id));
let def_kind = self.def_kind(def_id).descr(def_id);
late_report_deprecation(
@ -377,7 +377,7 @@ impl<'tcx> TyCtxt<'tcx> {
depr_attr.since,
depr_attr.note,
def_kind,
def_path,
&def_path,
),
depr_attr.suggestion,
lint,

View file

@ -147,7 +147,7 @@ pub struct GlobalId<'tcx> {
impl<'tcx> GlobalId<'tcx> {
pub fn display(self, tcx: TyCtxt<'tcx>) -> String {
let instance_name = with_no_trimmed_paths(|| tcx.def_path_str(self.instance.def.def_id()));
let instance_name = with_no_trimmed_paths!(tcx.def_path_str(self.instance.def.def_id()));
if let Some(promoted) = self.promoted {
format!("{}::{:?}", instance_name, promoted)
} else {

View file

@ -94,10 +94,8 @@ pub fn dump_enabled<'tcx>(tcx: TyCtxt<'tcx>, pass_name: &str, def_id: DefId) ->
None => return false,
Some(ref filters) => filters,
};
let node_path = ty::print::with_forced_impl_filename_line(|| {
// see notes on #41697 below
tcx.def_path_str(def_id)
});
// see notes on #41697 below
let node_path = ty::print::with_forced_impl_filename_line!(tcx.def_path_str(def_id));
filters.split('|').any(|or_filter| {
or_filter.split('&').all(|and_filter| {
let and_filter_trimmed = and_filter.trim();
@ -125,10 +123,9 @@ fn dump_matched_mir_node<'tcx, F>(
let _: io::Result<()> = try {
let mut file =
create_dump_file(tcx, "mir", pass_num, pass_name, disambiguator, body.source)?;
let def_path = ty::print::with_forced_impl_filename_line(|| {
// see notes on #41697 above
tcx.def_path_str(body.source.def_id())
});
// see notes on #41697 above
let def_path =
ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id()));
write!(file, "// MIR for `{}", def_path)?;
match body.source.promoted {
None => write!(file, "`")?,
@ -959,10 +956,10 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn Write) -> io::Res
_ => bug!("Unexpected def kind {:?}", kind),
}
ty::print::with_forced_impl_filename_line(|| {
ty::print::with_forced_impl_filename_line! {
// see notes on #41697 elsewhere
write!(w, "{}", tcx.def_path_str(def_id))
})?;
write!(w, "{}", tcx.def_path_str(def_id))?
}
if body.source.promoted.is_none() && is_function {
write!(w, "(")?;

View file

@ -63,66 +63,59 @@ thread_local! {
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
}
/// Avoids running any queries during any prints that occur
/// during the closure. This may alter the appearance of some
/// types (e.g. forcing verbose printing for opaque types).
/// This method is used during some queries (e.g. `explicit_item_bounds`
/// for opaque types), to ensure that any debug printing that
/// occurs during the query computation does not end up recursively
/// calling the same query.
pub fn with_no_queries<F: FnOnce() -> R, R>(f: F) -> R {
NO_QUERIES.with(|no_queries| {
let old = no_queries.replace(true);
let result = f();
no_queries.set(old);
result
})
macro_rules! define_helper {
($($(#[$a:meta])* fn $name:ident($helper:ident, $tl:ident);)+) => {
$(
#[must_use]
pub struct $helper(bool);
impl $helper {
pub fn new() -> $helper {
$helper($tl.with(|c| c.replace(true)))
}
}
$(#[$a])*
pub macro $name($e:expr) {
{
let _guard = $helper::new();
$e
}
}
impl Drop for $helper {
fn drop(&mut self) {
$tl.with(|c| c.set(self.0))
}
}
)+
}
}
/// Force us to name impls with just the filename/line number. We
/// normally try to use types. But at some points, notably while printing
/// cycle errors, this can result in extra or suboptimal error output,
/// so this variable disables that check.
pub fn with_forced_impl_filename_line<F: FnOnce() -> R, R>(f: F) -> R {
FORCE_IMPL_FILENAME_LINE.with(|force| {
let old = force.replace(true);
let result = f();
force.set(old);
result
})
}
/// Adds the `crate::` prefix to paths where appropriate.
pub fn with_crate_prefix<F: FnOnce() -> R, R>(f: F) -> R {
SHOULD_PREFIX_WITH_CRATE.with(|flag| {
let old = flag.replace(true);
let result = f();
flag.set(old);
result
})
}
/// Prevent path trimming if it is turned on. Path trimming affects `Display` impl
/// of various rustc types, for example `std::vec::Vec` would be trimmed to `Vec`,
/// if no other `Vec` is found.
pub fn with_no_trimmed_paths<F: FnOnce() -> R, R>(f: F) -> R {
NO_TRIMMED_PATH.with(|flag| {
let old = flag.replace(true);
let result = f();
flag.set(old);
result
})
}
/// Prevent selection of visible paths. `Display` impl of DefId will prefer visible (public) reexports of types as paths.
pub fn with_no_visible_paths<F: FnOnce() -> R, R>(f: F) -> R {
NO_VISIBLE_PATH.with(|flag| {
let old = flag.replace(true);
let result = f();
flag.set(old);
result
})
}
define_helper!(
/// Avoids running any queries during any prints that occur
/// during the closure. This may alter the appearance of some
/// types (e.g. forcing verbose printing for opaque types).
/// This method is used during some queries (e.g. `explicit_item_bounds`
/// for opaque types), to ensure that any debug printing that
/// occurs during the query computation does not end up recursively
/// calling the same query.
fn with_no_queries(NoQueriesGuard, NO_QUERIES);
/// Force us to name impls with just the filename/line number. We
/// normally try to use types. But at some points, notably while printing
/// cycle errors, this can result in extra or suboptimal error output,
/// so this variable disables that check.
fn with_forced_impl_filename_line(ForcedImplGuard, FORCE_IMPL_FILENAME_LINE);
/// Adds the `crate::` prefix to paths where appropriate.
fn with_crate_prefix(CratePrefixGuard, SHOULD_PREFIX_WITH_CRATE);
/// Prevent path trimming if it is turned on. Path trimming affects `Display` impl
/// of various rustc types, for example `std::vec::Vec` would be trimmed to `Vec`,
/// if no other `Vec` is found.
fn with_no_trimmed_paths(NoTrimmedGuard, NO_TRIMMED_PATH);
/// Prevent selection of visible paths. `Display` impl of DefId will prefer
/// visible (public) reexports of types as paths.
fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH);
);
/// The "region highlights" are used to control region printing during
/// specific error messages. When a "region highlight" is enabled, it
@ -379,7 +372,7 @@ pub trait PrettyPrinter<'tcx>:
// in cases where the `extern crate foo` has non-trivial
// parents, e.g. it's nested in `impl foo::Trait for Bar`
// (see also issues #55779 and #87932).
self = with_no_visible_paths(|| self.print_def_path(def_id, &[]))?;
self = with_no_visible_paths!(self.print_def_path(def_id, &[])?);
return Ok((self, true));
}
@ -655,7 +648,7 @@ pub trait PrettyPrinter<'tcx>:
return Ok(self);
}
return with_no_queries(|| {
return with_no_queries!({
let def_key = self.tcx().def_key(def_id);
if let Some(name) = def_key.disambiguated_data.data.get_opt_name() {
p!(write("{}", name));

View file

@ -21,9 +21,9 @@ use std::sync::Arc;
impl fmt::Debug for ty::TraitDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
with_no_trimmed_paths(|| {
FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])
})?;
with_no_trimmed_paths!(
FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.def_id, &[])?
);
Ok(())
})
}
@ -32,9 +32,9 @@ impl fmt::Debug for ty::TraitDef {
impl fmt::Debug for ty::AdtDef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
ty::tls::with(|tcx| {
with_no_trimmed_paths(|| {
FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])
})?;
with_no_trimmed_paths!(
FmtPrinter::new(tcx, f, Namespace::TypeNS).print_def_path(self.did, &[])?
);
Ok(())
})
}
@ -49,7 +49,7 @@ impl fmt::Debug for ty::UpvarId {
impl<'tcx> fmt::Debug for ty::ExistentialTraitRef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
with_no_trimmed_paths!(fmt::Display::fmt(self, f))
}
}
@ -125,13 +125,13 @@ impl fmt::Debug for ty::RegionVid {
impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
with_no_trimmed_paths!(fmt::Display::fmt(self, f))
}
}
impl<'tcx> fmt::Debug for Ty<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
with_no_trimmed_paths(|| fmt::Display::fmt(self, f))
with_no_trimmed_paths!(fmt::Display::fmt(self, f))
}
}