From: Eduard-Mihai Burtescu Date: Sun, 20 Jan 2019 02:56:48 +0000 (+0200) Subject: rustc: remove obsolete hacks from ppaux, relating to normalization under HRTB. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=030cdc972930aa864fe89dd4c5125ba26ebd05ac;p=rust.git rustc: remove obsolete hacks from ppaux, relating to normalization under HRTB. --- diff --git a/src/librustc/ty/print/mod.rs b/src/librustc/ty/print/mod.rs index 8c590bd7833..d1632e1e9bb 100644 --- a/src/librustc/ty/print/mod.rs +++ b/src/librustc/ty/print/mod.rs @@ -1,6 +1,6 @@ use crate::hir::map::DefPathData; use crate::hir::def_id::{CrateNum, DefId}; -use crate::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable}; +use crate::ty::{self, DefIdTree, Ty, TyCtxt}; use crate::ty::subst::{Subst, SubstsRef}; use rustc_data_structures::fx::FxHashSet; @@ -16,19 +16,6 @@ // FIXME(eddyb) this module uses `pub(crate)` for things used only // from `ppaux` - when that is removed, they can be re-privatized. -struct LateBoundRegionNameCollector(FxHashSet); -impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector { - fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool { - match *r { - ty::ReLateBound(_, ty::BrNamed(_, name)) => { - self.0.insert(name); - }, - _ => {}, - } - r.super_visit_with(self) - } -} - #[derive(Default)] pub(crate) struct PrintConfig { used_region_names: Option>, @@ -67,14 +54,6 @@ pub fn with( pub(crate) fn with_tls_tcx(printer: P, f: impl FnOnce(PrintCx<'_, '_, '_, P>) -> R) -> R { ty::tls::with(|tcx| PrintCx::with(tcx, printer, f)) } - fn prepare_late_bound_region_info(&mut self, value: &ty::Binder) - where T: TypeFoldable<'tcx> - { - let mut collector = LateBoundRegionNameCollector(Default::default()); - value.visit_with(&mut collector); - self.config.used_region_names = Some(collector.0); - self.config.region_index = 0; - } } pub trait Print<'tcx, P> { @@ -322,3 +301,27 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option { ty::Float(_) => None, } } + +impl Print<'tcx, P> for ty::RegionKind { + type Output = P::Region; + type Error = P::Error; + fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result { + cx.print_region(self) + } +} + +impl Print<'tcx, P> for ty::Region<'_> { + type Output = P::Region; + type Error = P::Error; + fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result { + cx.print_region(self) + } +} + +impl Print<'tcx, P> for Ty<'tcx> { + type Output = P::Type; + type Error = P::Error; + fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result { + cx.print_type(self) + } +} diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 6a7c48ee879..7488b074471 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -205,6 +205,15 @@ fn print_value_path( self.print_def_path(def_id, substs, iter::empty()) } + fn in_binder( + self: PrintCx<'_, '_, 'tcx, Self>, + value: &ty::Binder, + ) -> Result + where T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx> + { + value.skip_binder().print(self) + } + /// Print `<...>` around what `f` prints. fn generic_delimiters<'gcx, 'tcx>( self: PrintCx<'_, 'gcx, 'tcx, Self>, @@ -784,6 +793,15 @@ fn print_value_path( Ok(printer) } + fn in_binder( + self: PrintCx<'_, '_, 'tcx, Self>, + value: &ty::Binder, + ) -> Result + where T: Print<'tcx, Self, Output = Self, Error = Self::Error> + TypeFoldable<'tcx> + { + self.pretty_in_binder(value) + } + fn generic_delimiters<'gcx, 'tcx>( mut self: PrintCx<'_, 'gcx, 'tcx, Self>, f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result, @@ -1125,7 +1143,7 @@ pub fn pretty_print_type( p!(write(" "), print(witness), write("]")) }, ty::GeneratorWitness(types) => { - nest!(|cx| cx.pretty_in_binder(&types)) + nest!(|cx| cx.in_binder(&types)) } ty::Closure(did, substs) => { let upvar_tys = substs.upvar_tys(did, self.tcx); @@ -1257,9 +1275,6 @@ fn name_by_region_index(index: usize) -> InternedString { }) }; - // NOTE(eddyb) this must be below `start_or_continue`'s definition - // as that also has a `define_scoped_cx` and that kind of shadowing - // is disallowed (name resolution thinks `scoped_cx!` is ambiguous). define_scoped_cx!(self); let old_region_index = self.config.region_index; @@ -1302,6 +1317,29 @@ fn name_by_region_index(index: usize) -> InternedString { result } + fn prepare_late_bound_region_info(&mut self, value: &ty::Binder) + where T: TypeFoldable<'tcx> + { + + struct LateBoundRegionNameCollector(FxHashSet); + impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector { + fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool { + match *r { + ty::ReLateBound(_, ty::BrNamed(_, name)) => { + self.0.insert(name); + }, + _ => {}, + } + r.super_visit_with(self) + } + } + + let mut collector = LateBoundRegionNameCollector(Default::default()); + value.visit_with(&mut collector); + self.config.used_region_names = Some(collector.0); + self.config.region_index = 0; + } + fn is_name_used(&self, name: &InternedString) -> bool { match self.config.used_region_names { Some(ref names) => names.contains(name), @@ -1309,3 +1347,13 @@ fn is_name_used(&self, name: &InternedString) -> bool { } } } + +impl Print<'tcx, P> for ty::Binder + where T: Print<'tcx, P, Output = P, Error = P::Error> + TypeFoldable<'tcx> +{ + type Output = P; + type Error = P::Error; + fn print(&self, cx: PrintCx<'_, '_, 'tcx, P>) -> Result { + cx.in_binder(self) + } +} diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index 45db95e2b0f..dfb7e64d98b 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -775,28 +775,6 @@ impl<'a, 'tcx> Lift<'tcx> for interpret::GlobalId<'a> { } } -// FIXME(eddyb) this is like what some of the macros above generate, -// except that macros *also* generate a foldable impl, which we don't -// want (with it we'd risk bypassing `fold_region` / `fold_const`). -impl<'tcx> Lift<'tcx> for ty::RegionKind { - type Lifted = ty::RegionKind; - fn lift_to_tcx<'b, 'gcx>(&self, _: TyCtxt<'b, 'gcx, 'tcx>) -> Option { - Some(self.clone()) - } -} - -impl<'a, 'tcx> Lift<'tcx> for ty::LazyConst<'a> { - type Lifted = ty::LazyConst<'tcx>; - fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option { - match self { - ty::LazyConst::Evaluated(v) => Some(ty::LazyConst::Evaluated(tcx.lift(v)?)), - ty::LazyConst::Unevaluated(def_id, substs) => { - Some(ty::LazyConst::Unevaluated(*def_id, tcx.lift(substs)?)) - } - } - } -} - BraceStructLiftImpl! { impl<'a, 'tcx> Lift<'tcx> for ty::Const<'a> { type Lifted = ty::Const<'tcx>; diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 2e11a374136..f8d0c8f661c 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -1,7 +1,7 @@ use crate::hir; use crate::hir::def::Namespace; use crate::ty::subst::{Kind, UnpackedKind}; -use crate::ty::{self, ParamConst, Ty}; +use crate::ty::{self, ParamConst, Ty, TyCtxt}; use crate::ty::print::{FmtPrinter, PrettyPrinter, PrintCx, Print}; use crate::mir::interpret::ConstValue; @@ -10,13 +10,60 @@ use rustc_target::spec::abi::Abi; +pub trait LiftAndPrintToFmt<'tcx> { + fn lift_and_print_to_fmt( + &self, + tcx: TyCtxt<'_, '_, 'tcx>, + f: &mut fmt::Formatter<'_>, + ) -> fmt::Result; +} + +impl LiftAndPrintToFmt<'tcx> for T + where T: ty::Lift<'tcx>, + for<'a, 'b> >::Lifted: + Print<'tcx, FmtPrinter<&'a mut fmt::Formatter<'b>>, Error = fmt::Error> +{ + fn lift_and_print_to_fmt( + &self, + tcx: TyCtxt<'_, '_, 'tcx>, + f: &mut fmt::Formatter<'_>, + ) -> fmt::Result { + PrintCx::with(tcx, FmtPrinter::new(f, Namespace::TypeNS), |cx| { + cx.tcx.lift(self).expect("could not lift for printing").print(cx)?; + Ok(()) + }) + } +} + +// HACK(eddyb) this is separate because `ty::RegionKind` doesn't need lifting. +impl LiftAndPrintToFmt<'tcx> for ty::RegionKind { + fn lift_and_print_to_fmt( + &self, + tcx: TyCtxt<'_, '_, 'tcx>, + f: &mut fmt::Formatter<'_>, + ) -> fmt::Result { + PrintCx::with(tcx, FmtPrinter::new(f, Namespace::TypeNS), |cx| { + self.print(cx)?; + Ok(()) + }) + } +} + macro_rules! define_print { - ([$($target:ty),+] $vars:tt $def:tt) => { - $(define_print!($target, $vars $def);)+ + (<$($T:ident),*> $target:ty) => { + impl<$($T),*> fmt::Display for $target + where Self: for<'a> LiftAndPrintToFmt<'a> + { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + ty::tls::with(|tcx| self.lift_and_print_to_fmt(tcx, f)) + } + } }; - ($target:ty, ($self:ident, $cx:ident) { display $disp:block }) => { - impl Print<'tcx, P> for $target { + (<$($T:ident),*> $target:ty, ($self:ident, $cx:ident) { display $disp:block }) => { + impl<$($T,)* P: PrettyPrinter> Print<'tcx, P> for $target + where $($T: Print<'tcx, P, Output = P, Error = P::Error>),* + { type Output = P; type Error = fmt::Error; fn print(&$self, $cx: PrintCx<'_, '_, 'tcx, P>) -> Result { @@ -29,14 +76,15 @@ fn print(&$self, $cx: PrintCx<'_, '_, 'tcx, P>) -> Result) -> fmt::Result { - PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| { - cx.tcx.lift(self).expect("could not lift for printing").print(cx)?; - Ok(()) - }) - } - } + define_print!(<$($T),*> $target); + }; + + ($target:ty) => { + define_print!(<> $target); + }; + + ($target:ty, ($self:ident, $cx:ident) { display $disp:block }) => { + define_print!(<> $target, ($self, $cx) { display $disp }); }; } @@ -172,11 +220,7 @@ macro_rules! scoped_cx { } define_print! { - ty::RegionKind, (self, cx) { - display { - return cx.print_region(self); - } - } + ty::RegionKind } define_print! { @@ -215,34 +259,8 @@ macro_rules! scoped_cx { } } -// The generic impl doesn't work yet because projections are not -// normalized under HRTB. -/*impl fmt::Display for ty::Binder - where T: fmt::Display + for<'a> ty::Lift<'a>, - for<'a> >::Lifted: fmt::Display + TypeFoldable<'a> -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with_tls_tcx(|cx| cx.pretty_in_binder(cx.tcx.lift(self) - .expect("could not lift for printing"))) - } -}*/ - define_print! { - [ - ty::Binder<&'tcx ty::List>>, - ty::Binder>, - ty::Binder>, - ty::Binder>, - ty::Binder>, - ty::Binder>, - ty::Binder, ty::Region<'tcx>>>, - ty::Binder, ty::Region<'tcx>>> - ] - (self, cx) { - display { - nest!(|cx| cx.pretty_in_binder(self)) - } - } + ty::Binder } define_print! { @@ -254,11 +272,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } define_print! { - Ty<'tcx>, (self, cx) { - display { - return cx.print_type(self); - } - } + Ty<'tcx> } define_print! { @@ -309,13 +323,8 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } -// Similar problem to `Binder`, can't define a generic impl. define_print! { - [ - ty::OutlivesPredicate, ty::Region<'tcx>>, - ty::OutlivesPredicate, ty::Region<'tcx>> - ] - (self, cx) { + ty::OutlivesPredicate, (self, cx) { display { p!(print(self.0), write(" : "), print(self.1)) }