Skip to content

Commit 18c504a

Browse files
committed
rustc: split off most of ty::print::PrintCx's fields into a separate struct.
1 parent 6930d3e commit 18c504a

File tree

6 files changed

+86
-66
lines changed

6 files changed

+86
-66
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
499499
// module we could have false positives
500500
if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
501501
let abs_path = |def_id| {
502-
PrintCx::new(self.tcx, AbsolutePathPrinter)
503-
.print_def_path(def_id, None, Namespace::TypeNS, iter::empty())
502+
PrintCx::with(self.tcx, AbsolutePathPrinter, |mut cx| {
503+
cx.print_def_path(def_id, None, Namespace::TypeNS, iter::empty())
504+
})
504505
};
505506

506507
// We compare strings because DefPath can be different

src/librustc/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,8 +2341,8 @@ impl<'tcx> Debug for Rvalue<'tcx> {
23412341
};
23422342

23432343
// When printing regions, add trailing space if necessary.
2344-
ty::print::PrintCx::with(ty::print::FmtPrinter { fmt }, |cx| {
2345-
let region = if cx.is_verbose || cx.identify_regions {
2344+
ty::print::PrintCx::with_tls_tcx(ty::print::FmtPrinter { fmt }, |cx| {
2345+
let region = if cx.config.is_verbose || cx.config.identify_regions {
23462346
let mut region = region.to_string();
23472347
if region.len() > 0 {
23482348
region.push(' ');

src/librustc/ty/print.rs

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector {
6161
}
6262
}
6363

64-
pub struct PrintCx<'a, 'gcx, 'tcx, P> {
65-
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
66-
pub printer: P,
64+
pub(crate) struct PrintConfig {
6765
pub(crate) is_debug: bool,
6866
pub(crate) is_verbose: bool,
6967
pub(crate) identify_regions: bool,
@@ -72,6 +70,25 @@ pub struct PrintCx<'a, 'gcx, 'tcx, P> {
7270
pub(crate) binder_depth: usize,
7371
}
7472

73+
impl PrintConfig {
74+
pub(crate) fn new(tcx: TyCtxt<'_, '_, '_>) -> Self {
75+
PrintConfig {
76+
is_debug: false,
77+
is_verbose: tcx.sess.verbose(),
78+
identify_regions: tcx.sess.opts.debugging_opts.identify_regions,
79+
used_region_names: None,
80+
region_index: 0,
81+
binder_depth: 0,
82+
}
83+
}
84+
}
85+
86+
pub struct PrintCx<'a, 'gcx, 'tcx, P> {
87+
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
88+
pub printer: P,
89+
pub(crate) config: &'a mut PrintConfig,
90+
}
91+
7592
// HACK(eddyb) this is solely for `self: &mut PrintCx<Self>`, e.g. to
7693
// implement traits on the printer and call the methods on the context.
7794
impl<P> Deref for PrintCx<'_, '_, '_, P> {
@@ -81,30 +98,29 @@ impl<P> Deref for PrintCx<'_, '_, '_, P> {
8198
}
8299
}
83100

84-
impl<P> PrintCx<'a, 'gcx, 'tcx, P> {
85-
pub fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>, printer: P) -> Self {
86-
PrintCx {
101+
impl<'a, 'gcx, 'tcx, P> PrintCx<'a, 'gcx, 'tcx, P> {
102+
pub fn with<R>(
103+
tcx: TyCtxt<'a, 'gcx, 'tcx>,
104+
printer: P,
105+
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, P>) -> R,
106+
) -> R {
107+
f(PrintCx {
87108
tcx,
88109
printer,
89-
is_debug: false,
90-
is_verbose: tcx.sess.verbose(),
91-
identify_regions: tcx.sess.opts.debugging_opts.identify_regions,
92-
used_region_names: None,
93-
region_index: 0,
94-
binder_depth: 0,
95-
}
110+
config: &mut PrintConfig::new(tcx),
111+
})
96112
}
97113

98-
pub(crate) fn with<R>(printer: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R {
99-
ty::tls::with(|tcx| f(PrintCx::new(tcx, printer)))
114+
pub(crate) fn with_tls_tcx<R>(printer: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R {
115+
ty::tls::with(|tcx| PrintCx::with(tcx, printer, f))
100116
}
101117
pub(crate) fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
102118
where T: TypeFoldable<'tcx>
103119
{
104120
let mut collector = LateBoundRegionNameCollector(Default::default());
105121
value.visit_with(&mut collector);
106-
self.used_region_names = Some(collector.0);
107-
self.region_index = 0;
122+
self.config.used_region_names = Some(collector.0);
123+
self.config.region_index = 0;
108124
}
109125
}
110126

@@ -117,17 +133,17 @@ pub trait Print<'tcx, P> {
117133
&self,
118134
cx: &mut PrintCx<'_, '_, 'tcx, P>,
119135
) -> Result<Self::Output, Self::Error> {
120-
let old_debug = cx.is_debug;
121-
cx.is_debug = false;
136+
let old_debug = cx.config.is_debug;
137+
cx.config.is_debug = false;
122138
let result = self.print(cx);
123-
cx.is_debug = old_debug;
139+
cx.config.is_debug = old_debug;
124140
result
125141
}
126142
fn print_debug(&self, cx: &mut PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
127-
let old_debug = cx.is_debug;
128-
cx.is_debug = true;
143+
let old_debug = cx.config.is_debug;
144+
cx.config.is_debug = true;
129145
let result = self.print(cx);
130-
cx.is_debug = old_debug;
146+
cx.config.is_debug = old_debug;
131147
result
132148
}
133149
}
@@ -215,8 +231,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
215231
let ns = self.guess_def_namespace(def_id);
216232
debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns);
217233
let mut s = String::new();
218-
let _ = PrintCx::new(self, FmtPrinter { fmt: &mut s })
219-
.print_def_path(def_id, None, ns, iter::empty());
234+
let _ = PrintCx::with(self, FmtPrinter { fmt: &mut s }, |mut cx| {
235+
cx.print_def_path(def_id, None, ns, iter::empty())
236+
});
220237
s
221238
}
222239
}
@@ -616,7 +633,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
616633
});
617634

618635
// Don't print args that are the defaults of their respective parameters.
619-
let num_supplied_defaults = if self.is_verbose {
636+
let num_supplied_defaults = if self.config.is_verbose {
620637
0
621638
} else {
622639
params.iter().rev().take_while(|param| {

src/librustc/util/ppaux.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl RegionHighlightMode {
161161
macro_rules! gen_display_debug_body {
162162
( $with:path ) => {
163163
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164-
PrintCx::with(FmtPrinter { fmt: f }, |mut cx| {
164+
PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| {
165165
$with(&cx.tcx.lift(self).expect("could not lift for printing"), &mut cx)
166166
})
167167
}
@@ -197,7 +197,7 @@ macro_rules! gen_print_impl {
197197
type Error = fmt::Error;
198198
fn print(&$self, $cx: &mut PrintCx<'_, '_, 'tcx, P>) -> fmt::Result {
199199
define_scoped_cx!($cx);
200-
if $cx.is_debug $dbg
200+
if $cx.config.is_debug $dbg
201201
else $disp
202202
}
203203
}
@@ -208,7 +208,7 @@ macro_rules! gen_print_impl {
208208
type Error = fmt::Error;
209209
fn print(&$self, $cx: &mut PrintCx<'_, '_, 'tcx, P>) -> fmt::Result {
210210
define_scoped_cx!($cx);
211-
if $cx.is_debug $dbg
211+
if $cx.config.is_debug $dbg
212212
else $disp
213213
}
214214
}
@@ -316,7 +316,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
316316
// clearly differentiate between named and unnamed regions in
317317
// the output. We'll probably want to tweak this over time to
318318
// decide just how much information to give.
319-
if self.binder_depth == 0 {
319+
if self.config.binder_depth == 0 {
320320
self.prepare_late_bound_region_info(value);
321321
}
322322

@@ -337,7 +337,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
337337
// is disallowed (name resolution thinks `scoped_cx!` is ambiguous).
338338
define_scoped_cx!(self);
339339

340-
let old_region_index = self.region_index;
340+
let old_region_index = self.config.region_index;
341341
let mut region_index = old_region_index;
342342
let new_value = self.tcx.replace_late_bound_regions(value, |br| {
343343
let _ = start_or_continue(self, "for<", ", ");
@@ -365,16 +365,16 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
365365
start_or_continue(self, "", "> ")?;
366366

367367
// Push current state to gcx, and restore after writing new_value.
368-
self.binder_depth += 1;
369-
self.region_index = region_index;
368+
self.config.binder_depth += 1;
369+
self.config.region_index = region_index;
370370
let result = new_value.print_display(self);
371-
self.region_index = old_region_index;
372-
self.binder_depth -= 1;
371+
self.config.region_index = old_region_index;
372+
self.config.binder_depth -= 1;
373373
result
374374
}
375375

376376
fn is_name_used(&self, name: &InternedString) -> bool {
377-
match self.used_region_names {
377+
match self.config.used_region_names {
378378
Some(ref names) => names.contains(name),
379379
None => false,
380380
}
@@ -387,7 +387,7 @@ pub fn parameterized<F: fmt::Write>(
387387
substs: &Substs<'_>,
388388
ns: Namespace,
389389
) -> fmt::Result {
390-
PrintCx::with(FmtPrinter { fmt: f }, |mut cx| {
390+
PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| {
391391
let substs = cx.tcx.lift(&substs).expect("could not lift for printing");
392392
let _ = cx.print_def_path(did, Some(substs), ns, iter::empty())?;
393393
Ok(())
@@ -404,8 +404,9 @@ define_print! {
404404
let mut resugared_principal = false;
405405

406406
// Special-case `Fn(...) -> ...` and resugar it.
407-
if !cx.is_verbose && cx.tcx.lang_items().fn_trait_kind(principal.def_id).is_some() {
408-
if let Tuple(ref args) = principal.substs.type_at(0).sty {
407+
let fn_trait_kind = cx.tcx.lang_items().fn_trait_kind(principal.def_id);
408+
if !cx.config.is_verbose && fn_trait_kind.is_some() {
409+
if let ty::Tuple(ref args) = principal.substs.type_at(0).sty {
409410
let mut projections = self.projection_bounds();
410411
if let (Some(proj), None) = (projections.next(), projections.next()) {
411412
let _ = cx.print_def_path(
@@ -485,7 +486,7 @@ impl fmt::Debug for ty::GenericParamDef {
485486

486487
impl fmt::Debug for ty::TraitDef {
487488
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
488-
PrintCx::with(FmtPrinter { fmt: f }, |mut cx| {
489+
PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| {
489490
let _ = cx.print_def_path(
490491
self.def_id,
491492
None,
@@ -499,7 +500,7 @@ impl fmt::Debug for ty::TraitDef {
499500

500501
impl fmt::Debug for ty::AdtDef {
501502
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
502-
PrintCx::with(FmtPrinter { fmt: f }, |mut cx| {
503+
PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| {
503504
let _ = cx.print_def_path(
504505
self.did,
505506
None,
@@ -521,7 +522,7 @@ impl<'tcx> fmt::Debug for ty::ClosureUpvar<'tcx> {
521522

522523
impl fmt::Debug for ty::UpvarId {
523524
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
524-
PrintCx::with(FmtPrinter { fmt: f }, |mut cx| {
525+
PrintCx::with_tls_tcx(FmtPrinter { fmt: f }, |mut cx| {
525526
define_scoped_cx!(cx);
526527
p!(write("UpvarId({:?};`{}`;{:?})",
527528
self.var_path.hir_id,
@@ -591,7 +592,7 @@ define_print! {
591592
define_print! {
592593
() ty::BoundRegion, (self, cx) {
593594
display {
594-
if cx.is_verbose {
595+
if cx.config.is_verbose {
595596
return self.print_debug(cx);
596597
}
597598

@@ -629,7 +630,7 @@ define_print! {
629630
// NB: this must be kept in sync with the printing logic above.
630631
impl ty::BoundRegion {
631632
fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
632-
if cx.is_verbose {
633+
if cx.config.is_verbose {
633634
return true;
634635
}
635636

@@ -653,7 +654,7 @@ impl ty::BoundRegion {
653654
define_print! {
654655
() ty::PlaceholderRegion, (self, cx) {
655656
display {
656-
if cx.is_verbose {
657+
if cx.config.is_verbose {
657658
return self.print_debug(cx);
658659
}
659660

@@ -672,7 +673,7 @@ define_print! {
672673
// NB: this must be kept in sync with the printing logic above.
673674
impl ty::PlaceholderRegion {
674675
fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
675-
if cx.is_verbose {
676+
if cx.config.is_verbose {
676677
return true;
677678
}
678679

@@ -688,7 +689,7 @@ impl ty::PlaceholderRegion {
688689
define_print! {
689690
() ty::RegionKind, (self, cx) {
690691
display {
691-
if cx.is_verbose {
692+
if cx.config.is_verbose {
692693
return self.print_debug(cx);
693694
}
694695

@@ -716,7 +717,7 @@ define_print! {
716717
ty::RePlaceholder(p) => {
717718
p!(print_display(p))
718719
}
719-
ty::ReScope(scope) if cx.identify_regions => {
720+
ty::ReScope(scope) if cx.config.identify_regions => {
720721
match scope.data {
721722
region::ScopeData::Node =>
722723
p!(write("'{}s", scope.item_local_id().as_usize())),
@@ -733,7 +734,7 @@ define_print! {
733734
)),
734735
}
735736
}
736-
ty::ReVar(region_vid) if cx.identify_regions => {
737+
ty::ReVar(region_vid) if cx.config.identify_regions => {
737738
p!(print_debug(region_vid))
738739
}
739740
ty::ReVar(region_vid) => {
@@ -800,7 +801,7 @@ define_print! {
800801
impl ty::RegionKind {
801802
// HACK(eddyb) `pub(crate)` only for `ty::print`.
802803
pub(crate) fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
803-
if cx.is_verbose {
804+
if cx.config.is_verbose {
804805
return true;
805806
}
806807

@@ -821,7 +822,7 @@ impl ty::RegionKind {
821822
ty::RePlaceholder(p) => p.display_outputs_anything(cx),
822823

823824
ty::ReScope(_) |
824-
ty::ReVar(_) if cx.identify_regions => true,
825+
ty::ReVar(_) if cx.config.identify_regions => true,
825826

826827
ty::ReVar(region_vid) => region_vid.display_outputs_anything(cx),
827828

@@ -898,7 +899,7 @@ impl fmt::Debug for ty::FloatVid {
898899
define_print! {
899900
() ty::RegionVid, (self, cx) {
900901
display {
901-
if cx.is_verbose {
902+
if cx.config.is_verbose {
902903
return self.print_debug(cx);
903904
}
904905

@@ -927,7 +928,7 @@ define_print! {
927928
// NB: this must be kept in sync with the printing logic above.
928929
impl ty::RegionVid {
929930
fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
930-
if cx.is_verbose {
931+
if cx.config.is_verbose {
931932
return true;
932933
}
933934

@@ -943,7 +944,7 @@ impl ty::RegionVid {
943944
define_print! {
944945
() ty::InferTy, (self, cx) {
945946
display {
946-
if cx.is_verbose {
947+
if cx.config.is_verbose {
947948
return self.print_debug(cx);
948949
}
949950
match *self {
@@ -990,7 +991,7 @@ impl fmt::Debug for ty::FloatVarValue {
990991
for<'a> <T as ty::Lift<'a>>::Lifted: fmt::Display + TypeFoldable<'a>
991992
{
992993
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
993-
PrintCx::with(|cx| cx.in_binder(cx.tcx.lift(self)
994+
PrintCx::with_tls_tcx(|cx| cx.in_binder(cx.tcx.lift(self)
994995
.expect("could not lift for printing")))
995996
}
996997
}*/
@@ -1139,7 +1140,7 @@ define_print! {
11391140
p!(write("Placeholder({:?})", placeholder))
11401141
}
11411142
Opaque(def_id, substs) => {
1142-
if cx.is_verbose {
1143+
if cx.config.is_verbose {
11431144
return p!(write("Opaque({:?}, {:?})", def_id, substs));
11441145
}
11451146

0 commit comments

Comments
 (0)