Skip to content

Commit ffde0f7

Browse files
authored
Rollup merge of rust-lang#103428 - SarthakSingh31:issue-94187, r=compiler-errors
Removed verbose printing from the `PrettyPrinter` when printing constants Partially solves rust-lang#94187 by completing the first step described in [this comment](rust-lang#94187 (comment)).
2 parents b03fa1a + 5e46d86 commit ffde0f7

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics/type_name.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir::definitions::DisambiguatedDefPathData;
44
use rustc_middle::mir::interpret::{Allocation, ConstAllocation};
55
use rustc_middle::ty::{
66
self,
7-
print::{PrettyPrinter, Print, Printer},
7+
print::{with_no_verbose_constants, PrettyPrinter, Print, Printer},
88
subst::{GenericArg, GenericArgKind},
99
Ty, TyCtxt,
1010
};
@@ -190,7 +190,9 @@ impl Write for AbsolutePathPrinter<'_> {
190190

191191
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
192192
pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
193-
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
193+
let path = with_no_verbose_constants!(
194+
AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path
195+
);
194196
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
195197
tcx.intern_const_alloc(alloc)
196198
}

compiler/rustc_middle/src/ty/print/pretty.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ thread_local! {
6363
static NO_TRIMMED_PATH: Cell<bool> = const { Cell::new(false) };
6464
static NO_QUERIES: Cell<bool> = const { Cell::new(false) };
6565
static NO_VISIBLE_PATH: Cell<bool> = const { Cell::new(false) };
66+
static NO_VERBOSE_CONSTANTS: Cell<bool> = const { Cell::new(false) };
6667
}
6768

6869
macro_rules! define_helper {
@@ -117,6 +118,9 @@ define_helper!(
117118
/// Prevent selection of visible paths. `Display` impl of DefId will prefer
118119
/// visible (public) reexports of types as paths.
119120
fn with_no_visible_paths(NoVisibleGuard, NO_VISIBLE_PATH);
121+
/// Prevent verbose printing of constants. Verbose printing of constants is
122+
/// never desirable in some contexts like `std::any::type_name`.
123+
fn with_no_verbose_constants(NoVerboseConstantsGuard, NO_VERBOSE_CONSTANTS);
120124
);
121125

122126
/// The "region highlights" are used to control region printing during
@@ -759,7 +763,7 @@ pub trait PrettyPrinter<'tcx>:
759763
}
760764
ty::Array(ty, sz) => {
761765
p!("[", print(ty), "; ");
762-
if self.tcx().sess.verbose() {
766+
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
763767
p!(write("{:?}", sz));
764768
} else if let ty::ConstKind::Unevaluated(..) = sz.kind() {
765769
// Do not try to evaluate unevaluated constants. If we are const evaluating an
@@ -1181,7 +1185,7 @@ pub trait PrettyPrinter<'tcx>:
11811185
) -> Result<Self::Const, Self::Error> {
11821186
define_scoped_cx!(self);
11831187

1184-
if self.tcx().sess.verbose() {
1188+
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
11851189
p!(write("Const({:?}: {:?})", ct.kind(), ct.ty()));
11861190
return Ok(self);
11871191
}
@@ -1416,7 +1420,7 @@ pub trait PrettyPrinter<'tcx>:
14161420
) -> Result<Self::Const, Self::Error> {
14171421
define_scoped_cx!(self);
14181422

1419-
if self.tcx().sess.verbose() {
1423+
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
14201424
p!(write("ValTree({:?}: ", valtree), print(ty), ")");
14211425
return Ok(self);
14221426
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Check to insure that the output of `std::any::type_name` does not change based on -Zverbose
2+
// when printing constants
3+
// run-pass
4+
// edition: 2018
5+
// revisions: normal verbose
6+
// [verbose]compile-flags:-Zverbose
7+
8+
struct Wrapper<const VALUE: usize>;
9+
10+
fn main() {
11+
assert_eq!(std::any::type_name::<[u32; 0]>(), "[u32; 0]");
12+
assert_eq!(std::any::type_name::<Wrapper<0>>(), "issue_94187_verbose_type_name::Wrapper<0>");
13+
}

0 commit comments

Comments
 (0)