Skip to content

Commit 4239191

Browse files
committed
rustc: remove the ability for pretty-printers to override nesting.
1 parent 85013fb commit 4239191

File tree

2 files changed

+59
-70
lines changed

2 files changed

+59
-70
lines changed

src/librustc/ty/print/pretty.rs

Lines changed: 55 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ use std::ops::{Deref, DerefMut};
1919
use super::*;
2020

2121
macro_rules! nest {
22-
($closure:expr) => {
23-
scoped_cx!() = scoped_cx!().nest($closure)?
22+
($e:expr) => {
23+
scoped_cx!() = PrintCx::new(scoped_cx!().tcx, $e?)
2424
}
2525
}
2626
macro_rules! print_inner {
2727
(write ($($data:expr),+)) => {
2828
write!(scoped_cx!(), $($data),+)?
2929
};
3030
($kind:ident ($data:expr)) => {
31-
nest!(|cx| $data.$kind(cx))
31+
nest!($data.$kind(scoped_cx!()))
3232
};
3333
}
3434
macro_rules! p {
@@ -180,15 +180,6 @@ pub trait PrettyPrinter:
180180
> +
181181
fmt::Write
182182
{
183-
/// Enter a nested print context, for pretty-printing
184-
/// nested components in some larger context.
185-
fn nest<'a, 'gcx, 'tcx, E>(
186-
self: PrintCx<'a, 'gcx, 'tcx, Self>,
187-
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, E>,
188-
) -> Result<PrintCx<'a, 'gcx, 'tcx, Self>, E> {
189-
Ok(PrintCx::new(self.tcx, f(self)?))
190-
}
191-
192183
/// Like `print_def_path` but for value paths.
193184
fn print_value_path(
194185
self: PrintCx<'_, '_, 'tcx, Self>,
@@ -214,11 +205,13 @@ pub trait PrettyPrinter:
214205
) -> Result<Self, Self::Error>
215206
where T: Print<'tcx, Self, Output = Self, Error = Self::Error>
216207
{
208+
define_scoped_cx!(self);
209+
217210
if let Some(first) = elems.next() {
218-
self = self.nest(|cx| first.print(cx))?;
211+
nest!(first.print(self));
219212
for elem in elems {
220213
self.write_str(", ")?;
221-
self = self.nest(|cx| elem.print(cx))?;
214+
nest!(elem.print(self));
222215
}
223216
}
224217
self.ok()
@@ -354,9 +347,9 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
354347
// the entire path will succeed or not. To support printers that do not
355348
// implement `PrettyPrinter`, a `Vec` or linked list on the stack would
356349
// need to be built, before starting to print anything.
357-
let mut prefix_success = false;
358-
nest!(|cx| {
359-
let (path, success) = cx.try_print_visible_def_path(visible_parent)?;
350+
let prefix_success;
351+
nest!({
352+
let (path, success) = self.try_print_visible_def_path(visible_parent)?;
360353
prefix_success = success;
361354
Ok(path)
362355
});
@@ -469,7 +462,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
469462
self_ty: Ty<'tcx>,
470463
trait_ref: Option<ty::TraitRef<'tcx>>,
471464
) -> Result<P::Path, P::Error> {
472-
self = self.nest(print_prefix)?;
465+
self = PrintCx::new(self.tcx, print_prefix(self)?);
473466

474467
self.generic_delimiters(|mut cx| {
475468
define_scoped_cx!(cx);
@@ -491,7 +484,7 @@ pub struct FmtPrinter<F>(Box<FmtPrinterData<F>>);
491484
pub struct FmtPrinterData<F> {
492485
fmt: F,
493486

494-
empty: bool,
487+
empty_path: bool,
495488
in_value: bool,
496489

497490
used_region_names: FxHashSet<InternedString>,
@@ -518,7 +511,7 @@ impl<F> FmtPrinter<F> {
518511
pub fn new(fmt: F, ns: Namespace) -> Self {
519512
FmtPrinter(Box::new(FmtPrinterData {
520513
fmt,
521-
empty: true,
514+
empty_path: false,
522515
in_value: ns == Namespace::ValueNS,
523516
used_region_names: Default::default(),
524517
region_index: 0,
@@ -530,7 +523,6 @@ impl<F> FmtPrinter<F> {
530523

531524
impl<F: fmt::Write> fmt::Write for FmtPrinter<F> {
532525
fn write_str(&mut self, s: &str) -> fmt::Result {
533-
self.empty &= s.is_empty();
534526
self.fmt.write_str(s)
535527
}
536528
}
@@ -548,16 +540,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
548540
def_id: DefId,
549541
substs: Option<&'tcx Substs<'tcx>>,
550542
) -> Result<Self::Path, Self::Error> {
543+
define_scoped_cx!(self);
544+
551545
// FIXME(eddyb) avoid querying `tcx.generics_of` and `tcx.def_key`
552546
// both here and in `default_print_def_path`.
553547
let generics = substs.map(|_| self.tcx.generics_of(def_id));
554548
if generics.as_ref().and_then(|g| g.parent).is_none() {
555-
let mut visible_path_success = false;
556-
self = self.nest(|cx| {
557-
let (path, success) = cx.try_print_visible_def_path(def_id)?;
549+
let visible_path_success;
550+
nest!({
551+
let (path, success) = self.try_print_visible_def_path(def_id)?;
558552
visible_path_success = success;
559553
Ok(path)
560-
})?;
554+
});
561555
if visible_path_success {
562556
return if let (Some(generics), Some(substs)) = (generics, substs) {
563557
let args = self.generic_args_to_print(generics, substs);
@@ -620,15 +614,18 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
620614
mut self: PrintCx<'_, '_, '_, Self>,
621615
cnum: CrateNum,
622616
) -> Result<Self::Path, Self::Error> {
617+
self.empty_path = true;
623618
if cnum == LOCAL_CRATE {
624619
if self.tcx.sess.rust_2018() {
625620
// We add the `crate::` keyword on Rust 2018, only when desired.
626621
if SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get()) {
627622
write!(self, "{}", keywords::Crate.name())?;
623+
self.empty_path = false;
628624
}
629625
}
630626
} else {
631627
write!(self, "{}", self.tcx.crate_name(cnum))?;
628+
self.empty_path = false;
632629
}
633630
self.ok()
634631
}
@@ -637,7 +634,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
637634
self_ty: Ty<'tcx>,
638635
trait_ref: Option<ty::TraitRef<'tcx>>,
639636
) -> Result<Self::Path, Self::Error> {
640-
self.pretty_path_qualified(self_ty, trait_ref)
637+
let mut path = self.pretty_path_qualified(self_ty, trait_ref)?;
638+
path.empty_path = false;
639+
Ok(path)
641640
}
642641

643642
fn path_append_impl<'gcx, 'tcx>(
@@ -648,17 +647,16 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
648647
self_ty: Ty<'tcx>,
649648
trait_ref: Option<ty::TraitRef<'tcx>>,
650649
) -> Result<Self::Path, Self::Error> {
651-
self.pretty_path_append_impl(|cx| {
650+
let mut path = self.pretty_path_append_impl(|cx| {
652651
let mut path = print_prefix(cx)?;
653-
654-
// HACK(eddyb) this accounts for `generic_delimiters`
655-
// printing `::<` instead of `<` if `in_value` is set.
656-
if !path.empty && !path.in_value {
652+
if !path.empty_path {
657653
write!(path, "::")?;
658654
}
659655

660656
Ok(path)
661-
}, self_ty, trait_ref)
657+
}, self_ty, trait_ref)?;
658+
path.empty_path = false;
659+
Ok(path)
662660
}
663661
fn path_append<'gcx, 'tcx>(
664662
self: PrintCx<'_, 'gcx, 'tcx, Self>,
@@ -672,10 +670,11 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
672670
// FIXME(eddyb) `text` should never be empty, but it
673671
// currently is for `extern { ... }` "foreign modules".
674672
if !text.is_empty() {
675-
if !path.empty {
673+
if !path.empty_path {
676674
write!(path, "::")?;
677675
}
678676
write!(path, "{}", text)?;
677+
path.empty_path = false;
679678
}
680679

681680
Ok(path)
@@ -687,7 +686,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
687686
) -> Result<Self::Path, Self::Error>,
688687
args: &[Kind<'tcx>],
689688
) -> Result<Self::Path, Self::Error> {
690-
self = self.nest(print_prefix)?;
689+
define_scoped_cx!(self);
690+
691+
nest!(print_prefix(self));
691692

692693
// Don't print `'_` if there's no unerased regions.
693694
let print_regions = args.iter().any(|arg| {
@@ -704,6 +705,9 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
704705
});
705706

706707
if args.clone().next().is_some() {
708+
if self.in_value {
709+
write!(self, "::")?;
710+
}
707711
self.generic_delimiters(|cx| cx.comma_sep(args))
708712
} else {
709713
self.ok()
@@ -712,17 +716,6 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
712716
}
713717

714718
impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
715-
fn nest<'a, 'gcx, 'tcx, E>(
716-
mut self: PrintCx<'a, 'gcx, 'tcx, Self>,
717-
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, E>,
718-
) -> Result<PrintCx<'a, 'gcx, 'tcx, Self>, E> {
719-
let tcx = self.tcx;
720-
let was_empty = std::mem::replace(&mut self.empty, true);
721-
let mut inner = f(self)?;
722-
inner.empty &= was_empty;
723-
Ok(PrintCx::new(tcx, inner))
724-
}
725-
726719
fn print_value_path(
727720
mut self: PrintCx<'_, '_, 'tcx, Self>,
728721
def_id: DefId,
@@ -748,11 +741,7 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
748741
mut self: PrintCx<'_, 'gcx, 'tcx, Self>,
749742
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, Self::Error>,
750743
) -> Result<Self, Self::Error> {
751-
if !self.empty && self.in_value {
752-
write!(self, "::<")?;
753-
} else {
754-
write!(self, "<")?;
755-
}
744+
write!(self, "<")?;
756745

757746
let was_in_value = std::mem::replace(&mut self.in_value, false);
758747
let mut inner = f(self)?;
@@ -956,7 +945,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
956945
ty::FnDef(def_id, substs) => {
957946
let sig = self.tcx.fn_sig(def_id).subst(self.tcx, substs);
958947
p!(print(sig), write(" {{"));
959-
nest!(|cx| cx.print_value_path(def_id, Some(substs)));
948+
nest!(self.print_value_path(def_id, Some(substs)));
960949
p!(write("}}"))
961950
}
962951
ty::FnPtr(ref bare_fn) => {
@@ -979,7 +968,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
979968
}
980969
}
981970
ty::Adt(def, substs) => {
982-
nest!(|cx| cx.print_def_path(def.did, Some(substs)));
971+
nest!(self.print_def_path(def.did, Some(substs)));
983972
}
984973
ty::Dynamic(data, r) => {
985974
let print_r = self.region_should_not_be_omitted(r);
@@ -992,7 +981,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
992981
}
993982
}
994983
ty::Foreign(def_id) => {
995-
nest!(|cx| cx.print_def_path(def_id, None));
984+
nest!(self.print_def_path(def_id, None));
996985
}
997986
ty::Projection(ref data) => p!(print(data)),
998987
ty::UnnormalizedProjection(ref data) => {
@@ -1093,7 +1082,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
10931082
p!(write(" "), print(witness), write("]"))
10941083
},
10951084
ty::GeneratorWitness(types) => {
1096-
nest!(|cx| cx.in_binder(&types))
1085+
nest!(self.in_binder(&types))
10971086
}
10981087
ty::Closure(did, substs) => {
10991088
let upvar_tys = substs.upvar_tys(did, self.tcx);
@@ -1165,7 +1154,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
11651154
let mut first = true;
11661155

11671156
if let Some(principal) = predicates.principal() {
1168-
nest!(|cx| cx.print_def_path(principal.def_id, None));
1157+
nest!(self.print_def_path(principal.def_id, None));
11691158

11701159
let mut resugared = false;
11711160

@@ -1175,7 +1164,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
11751164
if let ty::Tuple(ref args) = principal.substs.type_at(0).sty {
11761165
let mut projections = predicates.projection_bounds();
11771166
if let (Some(proj), None) = (projections.next(), projections.next()) {
1178-
nest!(|cx| cx.pretty_fn_sig(args, false, proj.ty));
1167+
nest!(self.pretty_fn_sig(args, false, proj.ty));
11791168
resugared = true;
11801169
}
11811170
}
@@ -1214,8 +1203,8 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
12141203
let args = arg0.into_iter().chain(args);
12151204
let projections = projection0.into_iter().chain(projections);
12161205

1217-
nest!(|cx| cx.generic_delimiters(|mut cx| {
1218-
cx = cx.nest(|cx| cx.comma_sep(args))?;
1206+
nest!(self.generic_delimiters(|mut cx| {
1207+
cx = PrintCx::new(cx.tcx, cx.comma_sep(args)?);
12191208
if arg0.is_some() && projection0.is_some() {
12201209
write!(cx, ", ")?;
12211210
}
@@ -1248,7 +1237,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
12481237
}
12491238
first = false;
12501239

1251-
nest!(|cx| cx.print_def_path(def_id, None));
1240+
nest!(self.print_def_path(def_id, None));
12521241
}
12531242

12541243
self.ok()
@@ -1517,7 +1506,7 @@ define_print_and_forward_display! {
15171506
ty::ExistentialPredicate::Trait(x) => p!(print(x)),
15181507
ty::ExistentialPredicate::Projection(x) => p!(print(x)),
15191508
ty::ExistentialPredicate::AutoTrait(def_id) => {
1520-
nest!(|cx| cx.print_def_path(def_id, None))
1509+
nest!(cx.print_def_path(def_id, None))
15211510
}
15221511
}
15231512
}
@@ -1532,7 +1521,7 @@ define_print_and_forward_display! {
15321521
}
15331522

15341523
p!(write("fn"));
1535-
nest!(|cx| cx.pretty_fn_sig(self.inputs(), self.variadic, self.output()));
1524+
nest!(cx.pretty_fn_sig(self.inputs(), self.variadic, self.output()));
15361525
}
15371526

15381527
ty::InferTy {
@@ -1551,7 +1540,7 @@ define_print_and_forward_display! {
15511540
}
15521541

15531542
ty::TraitRef<'tcx> {
1554-
nest!(|cx| cx.print_def_path(self.def_id, Some(self.substs)));
1543+
nest!(cx.print_def_path(self.def_id, Some(self.substs)));
15551544
}
15561545

15571546
ty::ParamTy {
@@ -1571,7 +1560,7 @@ define_print_and_forward_display! {
15711560
}
15721561

15731562
ty::ProjectionTy<'tcx> {
1574-
nest!(|cx| cx.print_def_path(self.item_def_id, Some(self.substs)));
1563+
nest!(cx.print_def_path(self.item_def_id, Some(self.substs)));
15751564
}
15761565

15771566
ty::ClosureKind {
@@ -1592,17 +1581,17 @@ define_print_and_forward_display! {
15921581
ty::Predicate::WellFormed(ty) => p!(print(ty), write(" well-formed")),
15931582
ty::Predicate::ObjectSafe(trait_def_id) => {
15941583
p!(write("the trait `"));
1595-
nest!(|cx| cx.print_def_path(trait_def_id, None));
1584+
nest!(cx.print_def_path(trait_def_id, None));
15961585
p!(write("` is object-safe"))
15971586
}
15981587
ty::Predicate::ClosureKind(closure_def_id, _closure_substs, kind) => {
15991588
p!(write("the closure `"));
1600-
nest!(|cx| cx.print_value_path(closure_def_id, None));
1589+
nest!(cx.print_value_path(closure_def_id, None));
16011590
p!(write("` implements the trait `{}`", kind))
16021591
}
16031592
ty::Predicate::ConstEvaluatable(def_id, substs) => {
16041593
p!(write("the constant `"));
1605-
nest!(|cx| cx.print_value_path(def_id, Some(substs)));
1594+
nest!(cx.print_value_path(def_id, Some(substs)));
16061595
p!(write("` can be evaluated"))
16071596
}
16081597
}

0 commit comments

Comments
 (0)