Skip to content

Commit 477c86e

Browse files
committed
rustc: let ty::print::pretty's p! macro call arbitrary methods.
1 parent 3902d54 commit 477c86e

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

src/librustc/ty/print/pretty.rs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,19 @@ use std::ops::{Deref, DerefMut};
1919
// `pretty` is a separate module only for organization.
2020
use super::*;
2121

22-
macro_rules! print_inner {
23-
(write ($($data:expr),+)) => {
22+
macro_rules! p {
23+
(@write($($data:expr),+)) => {
2424
write!(scoped_cx!(), $($data),+)?
2525
};
26-
($kind:ident ($data:expr)) => {
27-
scoped_cx!() = $data.$kind(scoped_cx!())?
26+
(@print($x:expr)) => {
27+
scoped_cx!() = $x.print(scoped_cx!())?
2828
};
29-
}
30-
macro_rules! p {
31-
($($kind:ident $data:tt),+) => {
32-
{
33-
$(print_inner!($kind $data));+
34-
}
29+
(@$method:ident($($arg:expr),*)) => {
30+
scoped_cx!() = scoped_cx!().$method($($arg),*)?
3531
};
32+
($($kind:ident $data:tt),+) => {{
33+
$(p!(@$kind $data);)+
34+
}};
3635
}
3736
macro_rules! define_scoped_cx {
3837
($cx:ident) => {
@@ -470,9 +469,8 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
470469
}
471470
ty::FnDef(def_id, substs) => {
472471
let sig = self.tcx().fn_sig(def_id).subst(self.tcx(), substs);
473-
p!(print(sig), write(" {{"));
474-
self = self.print_value_path(def_id, Some(substs))?;
475-
p!(write("}}"))
472+
p!(print(sig),
473+
write(" {{"), print_value_path(def_id, Some(substs)), write("}}"));
476474
}
477475
ty::FnPtr(ref bare_fn) => {
478476
p!(print(bare_fn))
@@ -494,7 +492,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
494492
}
495493
}
496494
ty::Adt(def, substs) => {
497-
self = self.print_def_path(def.did, Some(substs))?;
495+
p!(print_def_path(def.did, Some(substs)));
498496
}
499497
ty::Dynamic(data, r) => {
500498
let print_r = self.region_should_not_be_omitted(r);
@@ -507,7 +505,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
507505
}
508506
}
509507
ty::Foreign(def_id) => {
510-
self = self.print_def_path(def_id, None)?;
508+
p!(print_def_path(def_id, None));
511509
}
512510
ty::Projection(ref data) => p!(print(data)),
513511
ty::UnnormalizedProjection(ref data) => {
@@ -608,7 +606,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
608606
p!(write(" "), print(witness), write("]"))
609607
},
610608
ty::GeneratorWitness(types) => {
611-
self = self.in_binder(&types)?;
609+
p!(in_binder(&types));
612610
}
613611
ty::Closure(did, substs) => {
614612
let upvar_tys = substs.upvar_tys(did, self.tcx());
@@ -693,7 +691,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
693691
let mut first = true;
694692

695693
if let Some(principal) = predicates.principal() {
696-
self = self.print_def_path(principal.def_id, None)?;
694+
p!(print_def_path(principal.def_id, None));
697695

698696
let mut resugared = false;
699697

@@ -703,7 +701,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
703701
if let ty::Tuple(ref args) = principal.substs.type_at(0).sty {
704702
let mut projections = predicates.projection_bounds();
705703
if let (Some(proj), None) = (projections.next(), projections.next()) {
706-
self = self.pretty_fn_sig(args, false, proj.ty)?;
704+
p!(pretty_fn_sig(args, false, proj.ty));
707705
resugared = true;
708706
}
709707
}
@@ -742,13 +740,13 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
742740
let args = arg0.into_iter().chain(args);
743741
let projections = projection0.into_iter().chain(projections);
744742

745-
self = self.generic_delimiters(|mut cx| {
743+
p!(generic_delimiters(|mut cx| {
746744
cx = cx.comma_sep(args)?;
747745
if arg0.is_some() && projection0.is_some() {
748746
write!(cx, ", ")?;
749747
}
750748
cx.comma_sep(projections)
751-
})?;
749+
}));
752750
}
753751
}
754752
first = false;
@@ -776,7 +774,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
776774
}
777775
first = false;
778776

779-
self = self.print_def_path(def_id, None)?;
777+
p!(print_def_path(def_id, None));
780778
}
781779

782780
Ok(self)
@@ -1478,7 +1476,7 @@ define_print_and_forward_display! {
14781476
ty::ExistentialPredicate::Trait(x) => p!(print(x)),
14791477
ty::ExistentialPredicate::Projection(x) => p!(print(x)),
14801478
ty::ExistentialPredicate::AutoTrait(def_id) => {
1481-
cx = cx.print_def_path(def_id, None)?;
1479+
p!(print_def_path(def_id, None));
14821480
}
14831481
}
14841482
}
@@ -1492,8 +1490,7 @@ define_print_and_forward_display! {
14921490
p!(write("extern {} ", self.abi));
14931491
}
14941492

1495-
p!(write("fn"));
1496-
cx = cx.pretty_fn_sig(self.inputs(), self.c_variadic, self.output())?;
1493+
p!(write("fn"), pretty_fn_sig(self.inputs(), self.c_variadic, self.output()));
14971494
}
14981495

14991496
ty::InferTy {
@@ -1512,7 +1509,7 @@ define_print_and_forward_display! {
15121509
}
15131510

15141511
ty::TraitRef<'tcx> {
1515-
cx = cx.print_def_path(self.def_id, Some(self.substs))?;
1512+
p!(print_def_path(self.def_id, Some(self.substs)));
15161513
}
15171514

15181515
ConstValue<'tcx> {
@@ -1556,7 +1553,7 @@ define_print_and_forward_display! {
15561553
}
15571554

15581555
ty::ProjectionTy<'tcx> {
1559-
cx = cx.print_def_path(self.item_def_id, Some(self.substs))?;
1556+
p!(print_def_path(self.item_def_id, Some(self.substs)));
15601557
}
15611558

15621559
ty::ClosureKind {
@@ -1576,19 +1573,19 @@ define_print_and_forward_display! {
15761573
ty::Predicate::Projection(ref predicate) => p!(print(predicate)),
15771574
ty::Predicate::WellFormed(ty) => p!(print(ty), write(" well-formed")),
15781575
ty::Predicate::ObjectSafe(trait_def_id) => {
1579-
p!(write("the trait `"));
1580-
cx = cx.print_def_path(trait_def_id, None)?;
1581-
p!(write("` is object-safe"))
1576+
p!(write("the trait `"),
1577+
print_def_path(trait_def_id, None),
1578+
write("` is object-safe"))
15821579
}
15831580
ty::Predicate::ClosureKind(closure_def_id, _closure_substs, kind) => {
1584-
p!(write("the closure `"));
1585-
cx = cx.print_value_path(closure_def_id, None)?;
1586-
p!(write("` implements the trait `{}`", kind))
1581+
p!(write("the closure `"),
1582+
print_value_path(closure_def_id, None),
1583+
write("` implements the trait `{}`", kind))
15871584
}
15881585
ty::Predicate::ConstEvaluatable(def_id, substs) => {
1589-
p!(write("the constant `"));
1590-
cx = cx.print_value_path(def_id, Some(substs))?;
1591-
p!(write("` can be evaluated"))
1586+
p!(write("the constant `"),
1587+
print_value_path(def_id, Some(substs)),
1588+
write("` can be evaluated"))
15921589
}
15931590
}
15941591
}

0 commit comments

Comments
 (0)