Skip to content

Commit 37e9185

Browse files
committed
rustc: split off most of ty::print::PrintCx's fields into a separate struct.
1 parent 5616ca8 commit 37e9185

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
@@ -498,8 +498,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
498498
// module we could have false positives
499499
if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
500500
let abs_path = |def_id| {
501-
PrintCx::new(self.tcx, AbsolutePathPrinter)
502-
.print_def_path(def_id, None, Namespace::TypeNS, iter::empty())
501+
PrintCx::with(self.tcx, AbsolutePathPrinter, |mut cx| {
502+
cx.print_def_path(def_id, None, Namespace::TypeNS, iter::empty())
503+
})
503504
};
504505

505506
// 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
@@ -2369,8 +2369,8 @@ impl<'tcx> Debug for Rvalue<'tcx> {
23692369
};
23702370

23712371
// When printing regions, add trailing space if necessary.
2372-
ty::print::PrintCx::with(ty::print::FmtPrinter { fmt }, |cx| {
2373-
let region = if cx.is_verbose || cx.identify_regions {
2372+
ty::print::PrintCx::with_tls_tcx(ty::print::FmtPrinter { fmt }, |cx| {
2373+
let region = if cx.config.is_verbose || cx.config.identify_regions {
23742374
let mut region = region.to_string();
23752375
if region.len() > 0 {
23762376
region.push(' ');

src/librustc/ty/print.rs

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

63-
pub struct PrintCx<'a, 'gcx, 'tcx, P> {
64-
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
65-
pub printer: P,
63+
pub(crate) struct PrintConfig {
6664
pub(crate) is_debug: bool,
6765
pub(crate) is_verbose: bool,
6866
pub(crate) identify_regions: bool,
@@ -71,6 +69,25 @@ pub struct PrintCx<'a, 'gcx, 'tcx, P> {
7169
pub(crate) binder_depth: usize,
7270
}
7371

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

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

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

@@ -116,17 +132,17 @@ pub trait Print<'tcx, P> {
116132
&self,
117133
cx: &mut PrintCx<'_, '_, 'tcx, P>,
118134
) -> Result<Self::Output, Self::Error> {
119-
let old_debug = cx.is_debug;
120-
cx.is_debug = false;
135+
let old_debug = cx.config.is_debug;
136+
cx.config.is_debug = false;
121137
let result = self.print(cx);
122-
cx.is_debug = old_debug;
138+
cx.config.is_debug = old_debug;
123139
result
124140
}
125141
fn print_debug(&self, cx: &mut PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
126-
let old_debug = cx.is_debug;
127-
cx.is_debug = true;
142+
let old_debug = cx.config.is_debug;
143+
cx.config.is_debug = true;
128144
let result = self.print(cx);
129-
cx.is_debug = old_debug;
145+
cx.config.is_debug = old_debug;
130146
result
131147
}
132148
}
@@ -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
}
@@ -613,7 +630,7 @@ impl<P: PrettyPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
613630
});
614631

615632
// Don't print args that are the defaults of their respective parameters.
616-
let num_supplied_defaults = if self.is_verbose {
633+
let num_supplied_defaults = if self.config.is_verbose {
617634
0
618635
} else {
619636
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: SubstsRef<'_>,
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(
@@ -486,7 +487,7 @@ impl fmt::Debug for ty::GenericParamDef {
486487

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

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

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

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

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

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

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

@@ -717,7 +718,7 @@ define_print! {
717718
ty::RePlaceholder(p) => {
718719
p!(print_display(p))
719720
}
720-
ty::ReScope(scope) if cx.identify_regions => {
721+
ty::ReScope(scope) if cx.config.identify_regions => {
721722
match scope.data {
722723
region::ScopeData::Node =>
723724
p!(write("'{}s", scope.item_local_id().as_usize())),
@@ -734,7 +735,7 @@ define_print! {
734735
)),
735736
}
736737
}
737-
ty::ReVar(region_vid) if cx.identify_regions => {
738+
ty::ReVar(region_vid) if cx.config.identify_regions => {
738739
p!(print_debug(region_vid))
739740
}
740741
ty::ReVar(region_vid) => {
@@ -801,7 +802,7 @@ define_print! {
801802
impl ty::RegionKind {
802803
// HACK(eddyb) `pub(crate)` only for `ty::print`.
803804
pub(crate) fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
804-
if cx.is_verbose {
805+
if cx.config.is_verbose {
805806
return true;
806807
}
807808

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

824825
ty::ReScope(_) |
825-
ty::ReVar(_) if cx.identify_regions => true,
826+
ty::ReVar(_) if cx.config.identify_regions => true,
826827

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

@@ -905,7 +906,7 @@ impl fmt::Debug for ty::FloatVid {
905906
define_print! {
906907
() ty::RegionVid, (self, cx) {
907908
display {
908-
if cx.is_verbose {
909+
if cx.config.is_verbose {
909910
return self.print_debug(cx);
910911
}
911912

@@ -934,7 +935,7 @@ define_print! {
934935
// NB: this must be kept in sync with the printing logic above.
935936
impl ty::RegionVid {
936937
fn display_outputs_anything<P>(&self, cx: &mut PrintCx<'_, '_, '_, P>) -> bool {
937-
if cx.is_verbose {
938+
if cx.config.is_verbose {
938939
return true;
939940
}
940941

@@ -950,7 +951,7 @@ impl ty::RegionVid {
950951
define_print! {
951952
() ty::InferTy, (self, cx) {
952953
display {
953-
if cx.is_verbose {
954+
if cx.config.is_verbose {
954955
return self.print_debug(cx);
955956
}
956957
match *self {
@@ -997,7 +998,7 @@ impl fmt::Debug for ty::FloatVarValue {
997998
for<'a> <T as ty::Lift<'a>>::Lifted: fmt::Display + TypeFoldable<'a>
998999
{
9991000
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1000-
PrintCx::with(|cx| cx.in_binder(cx.tcx.lift(self)
1001+
PrintCx::with_tls_tcx(|cx| cx.in_binder(cx.tcx.lift(self)
10011002
.expect("could not lift for printing")))
10021003
}
10031004
}*/
@@ -1146,7 +1147,7 @@ define_print! {
11461147
p!(write("Placeholder({:?})", placeholder))
11471148
}
11481149
Opaque(def_id, substs) => {
1149-
if cx.is_verbose {
1150+
if cx.config.is_verbose {
11501151
return p!(write("Opaque({:?}, {:?})", def_id, substs));
11511152
}
11521153

0 commit comments

Comments
 (0)