Skip to content

Commit 0937561

Browse files
committed
rustc: remove obsolete hacks from ppaux, relating to normalization under HRTB.
1 parent ff3ae32 commit 0937561

File tree

4 files changed

+142
-92
lines changed

4 files changed

+142
-92
lines changed

src/librustc/ty/print/mod.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use hir::map::DefPathData;
22
use hir::def_id::{CrateNum, DefId};
3-
use ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable};
3+
use ty::{self, DefIdTree, Ty, TyCtxt};
44
use ty::subst::{Subst, Substs};
55

66
use rustc_data_structures::fx::FxHashSet;
@@ -16,19 +16,6 @@ pub use self::pretty::*;
1616
// FIXME(eddyb) this module uses `pub(crate)` for things used only
1717
// from `ppaux` - when that is removed, they can be re-privatized.
1818

19-
struct LateBoundRegionNameCollector(FxHashSet<InternedString>);
20-
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector {
21-
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
22-
match *r {
23-
ty::ReLateBound(_, ty::BrNamed(_, name)) => {
24-
self.0.insert(name);
25-
},
26-
_ => {},
27-
}
28-
r.super_visit_with(self)
29-
}
30-
}
31-
3219
#[derive(Default)]
3320
pub(crate) struct PrintConfig {
3421
used_region_names: Option<FxHashSet<InternedString>>,
@@ -67,14 +54,6 @@ impl<'a, 'gcx, 'tcx, P> PrintCx<'a, 'gcx, 'tcx, P> {
6754
pub(crate) fn with_tls_tcx<R>(printer: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R {
6855
ty::tls::with(|tcx| PrintCx::with(tcx, printer, f))
6956
}
70-
fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
71-
where T: TypeFoldable<'tcx>
72-
{
73-
let mut collector = LateBoundRegionNameCollector(Default::default());
74-
value.visit_with(&mut collector);
75-
self.config.used_region_names = Some(collector.0);
76-
self.config.region_index = 0;
77-
}
7857
}
7958

8059
pub trait Print<'tcx, P> {
@@ -322,3 +301,27 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
322301
ty::Float(_) => None,
323302
}
324303
}
304+
305+
impl<P: Printer> Print<'tcx, P> for ty::RegionKind {
306+
type Output = P::Region;
307+
type Error = P::Error;
308+
fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
309+
cx.print_region(self)
310+
}
311+
}
312+
313+
impl<P: Printer> Print<'tcx, P> for ty::Region<'_> {
314+
type Output = P::Region;
315+
type Error = P::Error;
316+
fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
317+
cx.print_region(self)
318+
}
319+
}
320+
321+
impl<P: Printer> Print<'tcx, P> for Ty<'tcx> {
322+
type Output = P::Type;
323+
type Error = P::Error;
324+
fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
325+
cx.print_type(self)
326+
}
327+
}

src/librustc/ty/print/pretty.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ pub trait PrettyPrinter:
206206
self.print_def_path(def_id, substs, iter::empty())
207207
}
208208

209+
fn in_binder<T>(
210+
self: PrintCx<'_, '_, 'tcx, Self>,
211+
value: &ty::Binder<T>,
212+
) -> Result<Self, Self::Error>
213+
where T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>
214+
{
215+
value.skip_binder().print(self)
216+
}
217+
209218
/// Print `<...>` around what `f` prints.
210219
fn generic_delimiters<'gcx, 'tcx>(
211220
self: PrintCx<'_, 'gcx, 'tcx, Self>,
@@ -786,6 +795,15 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
786795
Ok(printer)
787796
}
788797

798+
fn in_binder<T>(
799+
self: PrintCx<'_, '_, 'tcx, Self>,
800+
value: &ty::Binder<T>,
801+
) -> Result<Self, Self::Error>
802+
where T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>
803+
{
804+
self.pretty_in_binder(value)
805+
}
806+
789807
fn generic_delimiters<'gcx, 'tcx>(
790808
mut self: PrintCx<'_, 'gcx, 'tcx, Self>,
791809
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, Self::Error>,
@@ -1127,7 +1145,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
11271145
p!(write(" "), print(witness), write("]"))
11281146
},
11291147
ty::GeneratorWitness(types) => {
1130-
nest!(|cx| cx.pretty_in_binder(&types))
1148+
nest!(|cx| cx.in_binder(&types))
11311149
}
11321150
ty::Closure(did, substs) => {
11331151
let upvar_tys = substs.upvar_tys(did, self.tcx);
@@ -1246,9 +1264,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
12461264
})
12471265
};
12481266

1249-
// NOTE(eddyb) this must be below `start_or_continue`'s definition
1250-
// as that also has a `define_scoped_cx` and that kind of shadowing
1251-
// is disallowed (name resolution thinks `scoped_cx!` is ambiguous).
12521267
define_scoped_cx!(self);
12531268

12541269
let old_region_index = self.config.region_index;
@@ -1291,10 +1306,43 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
12911306
result
12921307
}
12931308

1309+
fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
1310+
where T: TypeFoldable<'tcx>
1311+
{
1312+
1313+
struct LateBoundRegionNameCollector(FxHashSet<InternedString>);
1314+
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector {
1315+
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
1316+
match *r {
1317+
ty::ReLateBound(_, ty::BrNamed(_, name)) => {
1318+
self.0.insert(name);
1319+
},
1320+
_ => {},
1321+
}
1322+
r.super_visit_with(self)
1323+
}
1324+
}
1325+
1326+
let mut collector = LateBoundRegionNameCollector(Default::default());
1327+
value.visit_with(&mut collector);
1328+
self.config.used_region_names = Some(collector.0);
1329+
self.config.region_index = 0;
1330+
}
1331+
12941332
fn is_name_used(&self, name: &InternedString) -> bool {
12951333
match self.config.used_region_names {
12961334
Some(ref names) => names.contains(name),
12971335
None => false,
12981336
}
12991337
}
13001338
}
1339+
1340+
impl<T, P: PrettyPrinter> Print<'tcx, P> for ty::Binder<T>
1341+
where T: Print<'tcx, P, Output = P, Error = P::Error> + TypeFoldable<'tcx>
1342+
{
1343+
type Output = P;
1344+
type Error = P::Error;
1345+
fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
1346+
cx.in_binder(self)
1347+
}
1348+
}

src/librustc/ty/structural_impls.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -754,16 +754,6 @@ BraceStructLiftImpl! {
754754
}
755755
}
756756

757-
// FIXME(eddyb) this is like what `CloneTypeFoldableAndLiftImpls!`
758-
// generates, except that macro *also* generates a foldable impl,
759-
// which we don't want (with it we'd risk bypassing `fold_region`).
760-
impl<'tcx> Lift<'tcx> for ty::RegionKind {
761-
type Lifted = ty::RegionKind;
762-
fn lift_to_tcx<'b, 'gcx>(&self, _: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
763-
Some(self.clone())
764-
}
765-
}
766-
767757
BraceStructLiftImpl! {
768758
impl<'a, 'tcx> Lift<'tcx> for ty::Const<'a> {
769759
type Lifted = ty::Const<'tcx>;

src/librustc/util/ppaux.rs

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
11
use hir;
22
use hir::def::Namespace;
33
use ty::subst::{Kind, UnpackedKind};
4-
use ty::{self, Ty};
4+
use ty::{self, Ty, TyCtxt};
55
use ty::print::{FmtPrinter, PrettyPrinter, PrintCx, Print};
66

77
use std::fmt;
88
use std::iter;
99

1010
use rustc_target::spec::abi::Abi;
1111

12+
pub trait LiftAndPrintToFmt<'tcx> {
13+
fn lift_and_print_to_fmt(
14+
&self,
15+
tcx: TyCtxt<'_, '_, 'tcx>,
16+
f: &mut fmt::Formatter<'_>,
17+
) -> fmt::Result;
18+
}
19+
20+
impl<T> LiftAndPrintToFmt<'tcx> for T
21+
where T: ty::Lift<'tcx>,
22+
for<'a, 'b> <T as ty::Lift<'tcx>>::Lifted:
23+
Print<'tcx, FmtPrinter<&'a mut fmt::Formatter<'b>>, Error = fmt::Error>
24+
{
25+
fn lift_and_print_to_fmt(
26+
&self,
27+
tcx: TyCtxt<'_, '_, 'tcx>,
28+
f: &mut fmt::Formatter<'_>,
29+
) -> fmt::Result {
30+
PrintCx::with(tcx, FmtPrinter::new(f, Namespace::TypeNS), |cx| {
31+
cx.tcx.lift(self).expect("could not lift for printing").print(cx)?;
32+
Ok(())
33+
})
34+
}
35+
}
36+
37+
// HACK(eddyb) this is separate because `ty::RegionKind` doesn't need lifting.
38+
impl LiftAndPrintToFmt<'tcx> for ty::RegionKind {
39+
fn lift_and_print_to_fmt(
40+
&self,
41+
tcx: TyCtxt<'_, '_, 'tcx>,
42+
f: &mut fmt::Formatter<'_>,
43+
) -> fmt::Result {
44+
PrintCx::with(tcx, FmtPrinter::new(f, Namespace::TypeNS), |cx| {
45+
self.print(cx)?;
46+
Ok(())
47+
})
48+
}
49+
}
50+
1251
macro_rules! define_print {
13-
([$($target:ty),+] $vars:tt $def:tt) => {
14-
$(define_print!($target, $vars $def);)+
52+
(<$($T:ident),*> $target:ty) => {
53+
impl<$($T),*> fmt::Display for $target
54+
where Self: for<'a> LiftAndPrintToFmt<'a>
55+
{
56+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57+
ty::tls::with(|tcx| self.lift_and_print_to_fmt(tcx, f))
58+
}
59+
}
1560
};
1661

17-
($target:ty, ($self:ident, $cx:ident) { display $disp:block }) => {
18-
impl<P: PrettyPrinter> Print<'tcx, P> for $target {
62+
(<$($T:ident),*> $target:ty, ($self:ident, $cx:ident) { display $disp:block }) => {
63+
impl<$($T,)* P: PrettyPrinter> Print<'tcx, P> for $target
64+
where $($T: Print<'tcx, P, Output = P, Error = P::Error>),*
65+
{
1966
type Output = P;
2067
type Error = fmt::Error;
2168
fn print(&$self, $cx: PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
@@ -28,14 +75,15 @@ macro_rules! define_print {
2875
}
2976
}
3077

31-
impl fmt::Display for $target {
32-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33-
PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| {
34-
cx.tcx.lift(self).expect("could not lift for printing").print(cx)?;
35-
Ok(())
36-
})
37-
}
38-
}
78+
define_print!(<$($T),*> $target);
79+
};
80+
81+
($target:ty) => {
82+
define_print!(<> $target);
83+
};
84+
85+
($target:ty, ($self:ident, $cx:ident) { display $disp:block }) => {
86+
define_print!(<> $target, ($self, $cx) { display $disp });
3987
};
4088
}
4189

@@ -171,11 +219,7 @@ define_print! {
171219
}
172220

173221
define_print! {
174-
ty::RegionKind, (self, cx) {
175-
display {
176-
return cx.print_region(self);
177-
}
178-
}
222+
ty::RegionKind
179223
}
180224

181225
define_print! {
@@ -214,34 +258,8 @@ define_print! {
214258
}
215259
}
216260

217-
// The generic impl doesn't work yet because projections are not
218-
// normalized under HRTB.
219-
/*impl<T> fmt::Display for ty::Binder<T>
220-
where T: fmt::Display + for<'a> ty::Lift<'a>,
221-
for<'a> <T as ty::Lift<'a>>::Lifted: fmt::Display + TypeFoldable<'a>
222-
{
223-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
224-
PrintCx::with_tls_tcx(|cx| cx.pretty_in_binder(cx.tcx.lift(self)
225-
.expect("could not lift for printing")))
226-
}
227-
}*/
228-
229261
define_print! {
230-
[
231-
ty::Binder<&'tcx ty::List<ty::ExistentialPredicate<'tcx>>>,
232-
ty::Binder<ty::TraitRef<'tcx>>,
233-
ty::Binder<ty::FnSig<'tcx>>,
234-
ty::Binder<ty::TraitPredicate<'tcx>>,
235-
ty::Binder<ty::SubtypePredicate<'tcx>>,
236-
ty::Binder<ty::ProjectionPredicate<'tcx>>,
237-
ty::Binder<ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>>,
238-
ty::Binder<ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>>
239-
]
240-
(self, cx) {
241-
display {
242-
nest!(|cx| cx.pretty_in_binder(self))
243-
}
244-
}
262+
<T> ty::Binder<T>
245263
}
246264

247265
define_print! {
@@ -253,11 +271,7 @@ define_print! {
253271
}
254272

255273
define_print! {
256-
Ty<'tcx>, (self, cx) {
257-
display {
258-
return cx.print_type(self);
259-
}
260-
}
274+
Ty<'tcx>
261275
}
262276

263277
define_print! {
@@ -268,13 +282,8 @@ define_print! {
268282
}
269283
}
270284

271-
// Similar problem to `Binder<T>`, can't define a generic impl.
272285
define_print! {
273-
[
274-
ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>,
275-
ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>
276-
]
277-
(self, cx) {
286+
<T, U> ty::OutlivesPredicate<T, U>, (self, cx) {
278287
display {
279288
p!(print(self.0), write(" : "), print(self.1))
280289
}

0 commit comments

Comments
 (0)