Skip to content

Commit e6ffd96

Browse files
authored
Rollup merge of #103764 - SarthakSingh31:issue-94187-2, r=compiler-errors
All verbosity checks in `PrettyPrinter` now go through `PrettyPrinter::should_print_verbose` Follow-up to #103428. That pr only partially fixed #94187. In some cases (like closures) `std::any::type_name` was still producing a different output when `-Zverbose` was enabled. This pr fixes those cases and adds a new function `PrettyPrinter::should_print_verbose`. This function should always be used over `self.tcx().sess.verbose()` inside a `impl PrettyPrinter`. Maybe closes #94187 now. r? ``@compiler-errors``
2 parents 4473fcd + 8609364 commit e6ffd96

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

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

+7-4
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::{with_no_verbose_constants, PrettyPrinter, Print, Printer},
7+
print::{PrettyPrinter, Print, Printer},
88
subst::{GenericArg, GenericArgKind},
99
Ty, TyCtxt,
1010
};
@@ -179,6 +179,11 @@ impl<'tcx> PrettyPrinter<'tcx> for AbsolutePathPrinter<'tcx> {
179179

180180
Ok(self)
181181
}
182+
183+
fn should_print_verbose(&self) -> bool {
184+
// `std::any::type_name` should never print verbose type names
185+
false
186+
}
182187
}
183188

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

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

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

+17-17
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ 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) };
6766
}
6867

6968
macro_rules! define_helper {
@@ -118,9 +117,6 @@ define_helper!(
118117
/// Prevent selection of visible paths. `Display` impl of DefId will prefer
119118
/// visible (public) reexports of types as paths.
120119
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);
124120
);
125121

126122
/// The "region highlights" are used to control region printing during
@@ -600,7 +596,7 @@ pub trait PrettyPrinter<'tcx>:
600596
}
601597
ty::FnPtr(ref bare_fn) => p!(print(bare_fn)),
602598
ty::Infer(infer_ty) => {
603-
let verbose = self.tcx().sess.verbose();
599+
let verbose = self.should_print_verbose();
604600
if let ty::TyVar(ty_vid) = infer_ty {
605601
if let Some(name) = self.ty_infer_name(ty_vid) {
606602
p!(write("{}", name))
@@ -642,7 +638,7 @@ pub trait PrettyPrinter<'tcx>:
642638
p!(print_def_path(def_id, &[]));
643639
}
644640
ty::Projection(ref data) => {
645-
if !(self.tcx().sess.verbose() || NO_QUERIES.with(|q| q.get()))
641+
if !(self.should_print_verbose() || NO_QUERIES.with(|q| q.get()))
646642
&& self.tcx().def_kind(data.item_def_id) == DefKind::ImplTraitPlaceholder
647643
{
648644
return self.pretty_print_opaque_impl_type(data.item_def_id, data.substs);
@@ -658,7 +654,7 @@ pub trait PrettyPrinter<'tcx>:
658654
// only affect certain debug messages (e.g. messages printed
659655
// from `rustc_middle::ty` during the computation of `tcx.predicates_of`),
660656
// and should have no effect on any compiler output.
661-
if self.tcx().sess.verbose() || NO_QUERIES.with(|q| q.get()) {
657+
if self.should_print_verbose() || NO_QUERIES.with(|q| q.get()) {
662658
p!(write("Opaque({:?}, {:?})", def_id, substs));
663659
return Ok(self);
664660
}
@@ -689,7 +685,7 @@ pub trait PrettyPrinter<'tcx>:
689685
hir::Movability::Static => p!("static "),
690686
}
691687

692-
if !self.tcx().sess.verbose() {
688+
if !self.should_print_verbose() {
693689
p!("generator");
694690
// FIXME(eddyb) should use `def_span`.
695691
if let Some(did) = did.as_local() {
@@ -725,7 +721,7 @@ pub trait PrettyPrinter<'tcx>:
725721
}
726722
ty::Closure(did, substs) => {
727723
p!(write("["));
728-
if !self.tcx().sess.verbose() {
724+
if !self.should_print_verbose() {
729725
p!(write("closure"));
730726
// FIXME(eddyb) should use `def_span`.
731727
if let Some(did) = did.as_local() {
@@ -763,7 +759,7 @@ pub trait PrettyPrinter<'tcx>:
763759
}
764760
ty::Array(ty, sz) => {
765761
p!("[", print(ty), "; ");
766-
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
762+
if self.should_print_verbose() {
767763
p!(write("{:?}", sz));
768764
} else if let ty::ConstKind::Unevaluated(..) = sz.kind() {
769765
// Do not try to evaluate unevaluated constants. If we are const evaluating an
@@ -1077,7 +1073,7 @@ pub trait PrettyPrinter<'tcx>:
10771073

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

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

1423-
if !NO_VERBOSE_CONSTANTS.with(|flag| flag.get()) && self.tcx().sess.verbose() {
1419+
if self.should_print_verbose() {
14241420
p!(write("ValTree({:?}: ", valtree), print(ty), ")");
14251421
return Ok(self);
14261422
}
@@ -1564,6 +1560,10 @@ pub trait PrettyPrinter<'tcx>:
15641560
Ok(cx)
15651561
})
15661562
}
1563+
1564+
fn should_print_verbose(&self) -> bool {
1565+
self.tcx().sess.verbose()
1566+
}
15671567
}
15681568

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

1842-
let verbose = self.tcx.sess.verbose();
1842+
let verbose = self.should_print_verbose();
18431843
disambiguated_data.fmt_maybe_verbose(&mut self, verbose)?;
18441844

18451845
self.empty_path = false;
@@ -1940,7 +1940,7 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> {
19401940
return true;
19411941
}
19421942

1943-
if self.tcx.sess.verbose() {
1943+
if self.should_print_verbose() {
19441944
return true;
19451945
}
19461946

@@ -2012,7 +2012,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
20122012
return Ok(self);
20132013
}
20142014

2015-
if self.tcx.sess.verbose() {
2015+
if self.should_print_verbose() {
20162016
p!(write("{:?}", region));
20172017
return Ok(self);
20182018
}
@@ -2218,7 +2218,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> {
22182218
// aren't named. Eventually, we might just want this as the default, but
22192219
// this is not *quite* right and changes the ordering of some output
22202220
// anyways.
2221-
let (new_value, map) = if self.tcx().sess.verbose() {
2221+
let (new_value, map) = if self.should_print_verbose() {
22222222
let regions: Vec<_> = value
22232223
.bound_vars()
22242224
.into_iter()
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
// Check to insure that the output of `std::any::type_name` does not change based on -Zverbose
2-
// when printing constants
1+
// Check to insure that the output of `std::any::type_name` does not change based on `-Zverbose`
32
// run-pass
43
// edition: 2018
54
// revisions: normal verbose
65
// [verbose]compile-flags:-Zverbose
76

8-
struct Wrapper<const VALUE: usize>;
7+
use std::any::type_name;
98

109
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>");
10+
assert_eq!(type_name::<[u32; 0]>(), "[u32; 0]");
11+
12+
struct Wrapper<const VALUE: usize>;
13+
assert_eq!(type_name::<Wrapper<0>>(), "issue_94187_verbose_type_name::main::Wrapper<0>");
14+
15+
assert_eq!(
16+
type_name::<dyn Fn(u32) -> u32>(),
17+
"dyn core::ops::function::Fn<(u32,)>+Output = u32"
18+
);
1319
}

0 commit comments

Comments
 (0)