Skip to content

Commit 030cdc9

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

File tree

4 files changed

+142
-104
lines changed

4 files changed

+142
-104
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 crate::hir::map::DefPathData;
22
use crate::hir::def_id::{CrateNum, DefId};
3-
use crate::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable};
3+
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
44
use crate::ty::subst::{Subst, SubstsRef};
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
@@ -205,6 +205,15 @@ pub trait PrettyPrinter:
205205
self.print_def_path(def_id, substs, iter::empty())
206206
}
207207

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

796+
fn in_binder<T>(
797+
self: PrintCx<'_, '_, 'tcx, Self>,
798+
value: &ty::Binder<T>,
799+
) -> Result<Self, Self::Error>
800+
where T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx>
801+
{
802+
self.pretty_in_binder(value)
803+
}
804+
787805
fn generic_delimiters<'gcx, 'tcx>(
788806
mut self: PrintCx<'_, 'gcx, 'tcx, Self>,
789807
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, Self::Error>,
@@ -1125,7 +1143,7 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
11251143
p!(write(" "), print(witness), write("]"))
11261144
},
11271145
ty::GeneratorWitness(types) => {
1128-
nest!(|cx| cx.pretty_in_binder(&types))
1146+
nest!(|cx| cx.in_binder(&types))
11291147
}
11301148
ty::Closure(did, substs) => {
11311149
let upvar_tys = substs.upvar_tys(did, self.tcx);
@@ -1257,9 +1275,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
12571275
})
12581276
};
12591277

1260-
// NOTE(eddyb) this must be below `start_or_continue`'s definition
1261-
// as that also has a `define_scoped_cx` and that kind of shadowing
1262-
// is disallowed (name resolution thinks `scoped_cx!` is ambiguous).
12631278
define_scoped_cx!(self);
12641279

12651280
let old_region_index = self.config.region_index;
@@ -1302,10 +1317,43 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
13021317
result
13031318
}
13041319

1320+
fn prepare_late_bound_region_info<T>(&mut self, value: &ty::Binder<T>)
1321+
where T: TypeFoldable<'tcx>
1322+
{
1323+
1324+
struct LateBoundRegionNameCollector(FxHashSet<InternedString>);
1325+
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector {
1326+
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
1327+
match *r {
1328+
ty::ReLateBound(_, ty::BrNamed(_, name)) => {
1329+
self.0.insert(name);
1330+
},
1331+
_ => {},
1332+
}
1333+
r.super_visit_with(self)
1334+
}
1335+
}
1336+
1337+
let mut collector = LateBoundRegionNameCollector(Default::default());
1338+
value.visit_with(&mut collector);
1339+
self.config.used_region_names = Some(collector.0);
1340+
self.config.region_index = 0;
1341+
}
1342+
13051343
fn is_name_used(&self, name: &InternedString) -> bool {
13061344
match self.config.used_region_names {
13071345
Some(ref names) => names.contains(name),
13081346
None => false,
13091347
}
13101348
}
13111349
}
1350+
1351+
impl<T, P: PrettyPrinter> Print<'tcx, P> for ty::Binder<T>
1352+
where T: Print<'tcx, P, Output = P, Error = P::Error> + TypeFoldable<'tcx>
1353+
{
1354+
type Output = P;
1355+
type Error = P::Error;
1356+
fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result<Self::Output, Self::Error> {
1357+
cx.in_binder(self)
1358+
}
1359+
}

src/librustc/ty/structural_impls.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -775,28 +775,6 @@ BraceStructLiftImpl! {
775775
}
776776
}
777777

778-
// FIXME(eddyb) this is like what some of the macros above generate,
779-
// except that macros *also* generate a foldable impl, which we don't
780-
// want (with it we'd risk bypassing `fold_region` / `fold_const`).
781-
impl<'tcx> Lift<'tcx> for ty::RegionKind {
782-
type Lifted = ty::RegionKind;
783-
fn lift_to_tcx<'b, 'gcx>(&self, _: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
784-
Some(self.clone())
785-
}
786-
}
787-
788-
impl<'a, 'tcx> Lift<'tcx> for ty::LazyConst<'a> {
789-
type Lifted = ty::LazyConst<'tcx>;
790-
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> {
791-
match self {
792-
ty::LazyConst::Evaluated(v) => Some(ty::LazyConst::Evaluated(tcx.lift(v)?)),
793-
ty::LazyConst::Unevaluated(def_id, substs) => {
794-
Some(ty::LazyConst::Unevaluated(*def_id, tcx.lift(substs)?))
795-
}
796-
}
797-
}
798-
}
799-
800778
BraceStructLiftImpl! {
801779
impl<'a, 'tcx> Lift<'tcx> for ty::Const<'a> {
802780
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,7 +1,7 @@
11
use crate::hir;
22
use crate::hir::def::Namespace;
33
use crate::ty::subst::{Kind, UnpackedKind};
4-
use crate::ty::{self, ParamConst, Ty};
4+
use crate::ty::{self, ParamConst, Ty, TyCtxt};
55
use crate::ty::print::{FmtPrinter, PrettyPrinter, PrintCx, Print};
66
use crate::mir::interpret::ConstValue;
77

@@ -10,13 +10,60 @@ use std::iter;
1010

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

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

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

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

@@ -172,11 +220,7 @@ define_print! {
172220
}
173221

174222
define_print! {
175-
ty::RegionKind, (self, cx) {
176-
display {
177-
return cx.print_region(self);
178-
}
179-
}
223+
ty::RegionKind
180224
}
181225

182226
define_print! {
@@ -215,34 +259,8 @@ define_print! {
215259
}
216260
}
217261

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

248266
define_print! {
@@ -254,11 +272,7 @@ define_print! {
254272
}
255273

256274
define_print! {
257-
Ty<'tcx>, (self, cx) {
258-
display {
259-
return cx.print_type(self);
260-
}
261-
}
275+
Ty<'tcx>
262276
}
263277

264278
define_print! {
@@ -309,13 +323,8 @@ define_print! {
309323
}
310324
}
311325

312-
// Similar problem to `Binder<T>`, can't define a generic impl.
313326
define_print! {
314-
[
315-
ty::OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>>,
316-
ty::OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>>
317-
]
318-
(self, cx) {
327+
<T, U> ty::OutlivesPredicate<T, U>, (self, cx) {
319328
display {
320329
p!(print(self.0), write(" : "), print(self.1))
321330
}

0 commit comments

Comments
 (0)