Skip to content

Commit 549f9dc

Browse files
committed
rustc: let ty::print::pretty's p! macro call arbitrary methods.
1 parent 233d513 commit 549f9dc

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
@@ -18,20 +18,19 @@ use std::ops::{Deref, DerefMut};
1818
// `pretty` is a separate module only for organization.
1919
use super::*;
2020

21-
macro_rules! print_inner {
22-
(write ($($data:expr),+)) => {
21+
macro_rules! p {
22+
(@write($($data:expr),+)) => {
2323
write!(scoped_cx!(), $($data),+)?
2424
};
25-
($kind:ident ($data:expr)) => {
26-
scoped_cx!() = $data.$kind(scoped_cx!())?
25+
(@print($x:expr)) => {
26+
scoped_cx!() = $x.print(scoped_cx!())?
2727
};
28-
}
29-
macro_rules! p {
30-
($($kind:ident $data:tt),+) => {
31-
{
32-
$(print_inner!($kind $data));+
33-
}
28+
(@$method:ident($($arg:expr),*)) => {
29+
scoped_cx!() = scoped_cx!().$method($($arg),*)?
3430
};
31+
($($kind:ident $data:tt),+) => {{
32+
$(p!(@$kind $data);)+
33+
}};
3534
}
3635
macro_rules! define_scoped_cx {
3736
($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());
@@ -680,7 +678,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
680678
let mut first = true;
681679

682680
if let Some(principal) = predicates.principal() {
683-
self = self.print_def_path(principal.def_id, None)?;
681+
p!(print_def_path(principal.def_id, None));
684682

685683
let mut resugared = false;
686684

@@ -690,7 +688,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
690688
if let ty::Tuple(ref args) = principal.substs.type_at(0).sty {
691689
let mut projections = predicates.projection_bounds();
692690
if let (Some(proj), None) = (projections.next(), projections.next()) {
693-
self = self.pretty_fn_sig(args, false, proj.ty)?;
691+
p!(pretty_fn_sig(args, false, proj.ty));
694692
resugared = true;
695693
}
696694
}
@@ -729,13 +727,13 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
729727
let args = arg0.into_iter().chain(args);
730728
let projections = projection0.into_iter().chain(projections);
731729

732-
self = self.generic_delimiters(|mut cx| {
730+
p!(generic_delimiters(|mut cx| {
733731
cx = cx.comma_sep(args)?;
734732
if arg0.is_some() && projection0.is_some() {
735733
write!(cx, ", ")?;
736734
}
737735
cx.comma_sep(projections)
738-
})?;
736+
}));
739737
}
740738
}
741739
first = false;
@@ -763,7 +761,7 @@ pub trait PrettyPrinter<'gcx: 'tcx, 'tcx>:
763761
}
764762
first = false;
765763

766-
self = self.print_def_path(def_id, None)?;
764+
p!(print_def_path(def_id, None));
767765
}
768766

769767
Ok(self)
@@ -1464,7 +1462,7 @@ define_print_and_forward_display! {
14641462
ty::ExistentialPredicate::Trait(x) => p!(print(x)),
14651463
ty::ExistentialPredicate::Projection(x) => p!(print(x)),
14661464
ty::ExistentialPredicate::AutoTrait(def_id) => {
1467-
cx = cx.print_def_path(def_id, None)?;
1465+
p!(print_def_path(def_id, None));
14681466
}
14691467
}
14701468
}
@@ -1478,8 +1476,7 @@ define_print_and_forward_display! {
14781476
p!(write("extern {} ", self.abi));
14791477
}
14801478

1481-
p!(write("fn"));
1482-
cx = cx.pretty_fn_sig(self.inputs(), self.variadic, self.output())?;
1479+
p!(write("fn"), pretty_fn_sig(self.inputs(), self.variadic, self.output()));
14831480
}
14841481

14851482
ty::InferTy {
@@ -1498,7 +1495,7 @@ define_print_and_forward_display! {
14981495
}
14991496

15001497
ty::TraitRef<'tcx> {
1501-
cx = cx.print_def_path(self.def_id, Some(self.substs))?;
1498+
p!(print_def_path(self.def_id, Some(self.substs)));
15021499
}
15031500

15041501
ty::ParamTy {
@@ -1518,7 +1515,7 @@ define_print_and_forward_display! {
15181515
}
15191516

15201517
ty::ProjectionTy<'tcx> {
1521-
cx = cx.print_def_path(self.item_def_id, Some(self.substs))?;
1518+
p!(print_def_path(self.item_def_id, Some(self.substs)));
15221519
}
15231520

15241521
ty::ClosureKind {
@@ -1538,19 +1535,19 @@ define_print_and_forward_display! {
15381535
ty::Predicate::Projection(ref predicate) => p!(print(predicate)),
15391536
ty::Predicate::WellFormed(ty) => p!(print(ty), write(" well-formed")),
15401537
ty::Predicate::ObjectSafe(trait_def_id) => {
1541-
p!(write("the trait `"));
1542-
cx = cx.print_def_path(trait_def_id, None)?;
1543-
p!(write("` is object-safe"))
1538+
p!(write("the trait `"),
1539+
print_def_path(trait_def_id, None),
1540+
write("` is object-safe"))
15441541
}
15451542
ty::Predicate::ClosureKind(closure_def_id, _closure_substs, kind) => {
1546-
p!(write("the closure `"));
1547-
cx = cx.print_value_path(closure_def_id, None)?;
1548-
p!(write("` implements the trait `{}`", kind))
1543+
p!(write("the closure `"),
1544+
print_value_path(closure_def_id, None),
1545+
write("` implements the trait `{}`", kind))
15491546
}
15501547
ty::Predicate::ConstEvaluatable(def_id, substs) => {
1551-
p!(write("the constant `"));
1552-
cx = cx.print_value_path(def_id, Some(substs))?;
1553-
p!(write("` can be evaluated"))
1548+
p!(write("the constant `"),
1549+
print_value_path(def_id, Some(substs)),
1550+
write("` can be evaluated"))
15541551
}
15551552
}
15561553
}

0 commit comments

Comments
 (0)