Skip to content
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

All verbosity checks in PrettyPrinter now go through PrettyPrinter::should_print_verbose #103764

Merged
merged 1 commit into from
Oct 31, 2022
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
11 changes: 7 additions & 4 deletions compiler/rustc_const_eval/src/interpret/intrinsics/type_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_hir::definitions::DisambiguatedDefPathData;
use rustc_middle::mir::interpret::{Allocation, ConstAllocation};
use rustc_middle::ty::{
self,
print::{with_no_verbose_constants, PrettyPrinter, Print, Printer},
print::{PrettyPrinter, Print, Printer},
subst::{GenericArg, GenericArgKind},
Ty, TyCtxt,
};
Expand Down Expand Up @@ -179,6 +179,11 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {

Ok(self)
}

fn should_print_verbose(&self) -> bool {
// `std::any::type_name` should never print verbose type names
false
}
}

impl Write for AbsolutePathPrinter<'_> {
Expand All @@ -190,9 +195,7 @@ impl Write for AbsolutePathPrinter<'_> {

/// Directly returns an `Allocation` containing an absolute path representation of the given type.
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
let path = with_no_verbose_constants!(
AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path
);
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
tcx.intern_const_alloc(alloc)
}
34 changes: 17 additions & 17 deletions compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ thread_local! {
static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
static NO_QUERIES: Cell<bool> = const { Cell::new(false) };
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
static NO_VERBOSE_CONSTANTS: Cell<bool> = const { Cell::new(false) };
}

macro_rules! define_helper {
Expand Down Expand Up @@ -118,9 +117,6 @@ define_helper!(
/// 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);
/// Prevent verbose printing of constants. Verbose printing of constants is
/// never desirable in some contexts like `std::any::type_name`.
fn with_no_verbose_constants(NoVerboseConstantsGuard, NO_VERBOSE_CONSTANTS);
);

/// The "region highlights" are used to control region printing during
Expand Down Expand Up @@ -600,7 +596,7 @@ pub trait PrettyPrinter<'tcx>:
}
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),
ty::Infer(infer_ty) => {
let verbose = self.tcx().sess.verbose();
let verbose = self.should_print_verbose();
if let ty::TyVar(ty_vid) = infer_ty {
if let Some(name) = self.ty_infer_name(ty_vid) {
p!(write("{}", name))
Expand Down Expand Up @@ -642,7 +638,7 @@ pub trait PrettyPrinter<'tcx>:
p!(print_def_path(def_id, &[]));
}
ty::Projection(ref data) => {
if !(self.tcx().sess.verbose() || NO_QUERIES.with(|q| q.get()))
if !(self.should_print_verbose() || NO_QUERIES.with(|q| q.get()))
&& self.tcx().def_kind(data.item_def_id) == DefKind::ImplTraitPlaceholder
{
return self.pretty_print_opaque_impl_type(data.item_def_id, data.substs);
Expand All @@ -658,7 +654,7 @@ pub trait PrettyPrinter<'tcx>:
// only affect certain debug messages (e.g. messages printed
// from `rustc_middle::ty` during the computation of `tcx.predicates_of`),
// and should have no effect on any compiler output.
if self.tcx().sess.verbose() || NO_QUERIES.with(|q| q.get()) {
if self.should_print_verbose() || NO_QUERIES.with(|q| q.get()) {
p!(write("Opaque({:?}, {:?})", def_id, substs));
return Ok(self);
}
Expand Down Expand Up @@ -689,7 +685,7 @@ pub trait PrettyPrinter<'tcx>:
hir::Movability::Static => p!("static "),
}

if !self.tcx().sess.verbose() {
if !self.should_print_verbose() {
p!("generator");
// FIXME(eddyb) should use `def_span`.
if let Some(did) = did.as_local() {
Expand Down Expand Up @@ -725,7 +721,7 @@ pub trait PrettyPrinter<'tcx>:
}
ty::Closure(did, substs) => {
p!(write("["));
if !self.tcx().sess.verbose() {
if !self.should_print_verbose() {
p!(write("closure"));
// FIXME(eddyb) should use `def_span`.
if let Some(did) = did.as_local() {
Expand Down Expand Up @@ -763,7 +759,7 @@ pub trait PrettyPrinter<'tcx>:
}
ty::Array(ty, sz) => {
p!("[", print(ty), "; ");
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
if self.should_print_verbose() {
p!(write("{:?}", sz));
} else if let ty::ConstKind::Unevaluated(..) = sz.kind() {
// Do not try to evaluate unevaluated constants. If we are const evaluating an
Expand Down Expand Up @@ -1077,7 +1073,7 @@ pub trait PrettyPrinter<'tcx>:

// Special-case `Fn(...) -> ...` and re-sugar it.
let fn_trait_kind = cx.tcx().fn_trait_kind_from_lang_item(principal.def_id);
if !cx.tcx().sess.verbose() && fn_trait_kind.is_some() {
if !cx.should_print_verbose() && fn_trait_kind.is_some() {
if let ty::Tuple(tys) = principal.substs.type_at(0).kind() {
let mut projections = predicates.projection_bounds();
if let (Some(proj), None) = (projections.next(), projections.next()) {
Expand Down Expand Up @@ -1185,7 +1181,7 @@ pub trait PrettyPrinter<'tcx>:
) -> Result<Self::Const, Self::Error> {
define_scoped_cx!(self);

if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
if self.should_print_verbose() {
p!(write("Const({:?}: {:?})", ct.kind(), ct.ty()));
return Ok(self);
}
Expand Down Expand Up @@ -1420,7 +1416,7 @@ pub trait PrettyPrinter<'tcx>:
) -> Result<Self::Const, Self::Error> {
define_scoped_cx!(self);

if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
if self.should_print_verbose() {
p!(write("ValTree({:?}: ", valtree), print(ty), ")");
return Ok(self);
}
Expand Down Expand Up @@ -1564,6 +1560,10 @@ pub trait PrettyPrinter<'tcx>:
Ok(cx)
})
}

fn should_print_verbose(&self) -> bool {
self.tcx().sess.verbose()
}
}

// HACK(eddyb) boxed to avoid moving around a large struct by-value.
Expand Down Expand Up @@ -1839,7 +1839,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> {
}
}

let verbose = self.tcx.sess.verbose();
let verbose = self.should_print_verbose();
disambiguated_data.fmt_maybe_verbose(&mut self, verbose)?;

self.empty_path = false;
Expand Down Expand Up @@ -1940,7 +1940,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
return true;
}

if self.tcx.sess.verbose() {
if self.should_print_verbose() {
return true;
}

Expand Down Expand Up @@ -2012,7 +2012,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
return Ok(self);
}

if self.tcx.sess.verbose() {
if self.should_print_verbose() {
p!(write("{:?}", region));
return Ok(self);
}
Expand Down Expand Up @@ -2218,7 +2218,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
// aren't named. Eventually, we might just want this as the default, but
// this is not *quite* right and changes the ordering of some output
// anyways.
let (new_value, map) = if self.tcx().sess.verbose() {
let (new_value, map) = if self.should_print_verbose() {
let regions: Vec<_> = value
.bound_vars()
.into_iter()
Expand Down
16 changes: 11 additions & 5 deletions src/test/ui/type/issue-94187-verbose-type-name.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Check to insure that the output of `std::any::type_name` does not change based on -Zverbose
// when printing constants
// Check to insure that the output of `std::any::type_name` does not change based on `-Zverbose`
// run-pass
// edition: 2018
// revisions: normal verbose
// [verbose]compile-flags:-Zverbose

struct Wrapper<const VALUE: usize>;
use std::any::type_name;

fn main() {
assert_eq!(std::any::type_name::<[u32; 0]>(), "[u32; 0]");
assert_eq!(std::any::type_name::<Wrapper<0>>(), "issue_94187_verbose_type_name::Wrapper<0>");
assert_eq!(type_name::<[u32; 0]>(), "[u32; 0]");

struct Wrapper<const VALUE: usize>;
assert_eq!(type_name::<Wrapper<0>>(), "issue_94187_verbose_type_name::main::Wrapper<0>");

assert_eq!(
type_name::<dyn Fn(u32) -> u32>(),
"dyn core::ops::function::Fn<(u32,)>+Output = u32"
);
}