From fb53bb9e2b67887916a9815428ff5b8bac4c1ebf Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Sat, 19 Jan 2019 06:33:44 +0200 Subject: [PATCH] rustc: move Debug impls from ppaux to ty::structural_impls. --- src/librustc/ty/structural_impls.rs | 270 +++++++++++++++++++++++++ src/librustc/ty/subst.rs | 11 ++ src/librustc/util/ppaux.rs | 294 +--------------------------- 3 files changed, 288 insertions(+), 287 deletions(-) diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index 43ab32237dea..45db95e2b0f0 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -3,17 +3,287 @@ //! hand, though we've recently added some macros (e.g., //! `BraceStructLiftImpl!`) to help with the tedium. +use crate::hir::def::Namespace; use crate::mir::ProjectionKind; use crate::mir::interpret::ConstValue; use crate::ty::{self, Lift, Ty, TyCtxt, ConstVid, InferConst}; use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor}; +use crate::ty::print::{FmtPrinter, PrintCx, Printer}; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; use smallvec::SmallVec; use crate::mir::interpret; +use std::fmt; +use std::iter; use std::marker::PhantomData; use std::rc::Rc; +impl fmt::Debug for ty::GenericParamDef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let type_name = match self.kind { + ty::GenericParamDefKind::Lifetime => "Lifetime", + ty::GenericParamDefKind::Type {..} => "Type", + ty::GenericParamDefKind::Const => "Const", + }; + write!(f, "{}({}, {:?}, {})", + type_name, + self.name, + self.def_id, + self.index) + } +} + +impl fmt::Debug for ty::TraitDef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| { + cx.print_def_path(self.def_id, None, iter::empty())?; + Ok(()) + }) + } +} + +impl fmt::Debug for ty::AdtDef { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| { + cx.print_def_path(self.did, None, iter::empty())?; + Ok(()) + }) + } +} + +impl fmt::Debug for ty::ClosureUpvar<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "ClosureUpvar({:?},{:?})", + self.def, + self.ty) + } +} + +impl fmt::Debug for ty::UpvarId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let name = ty::tls::with(|tcx| { + tcx.hir().name_by_hir_id(self.var_path.hir_id) + }); + write!(f, "UpvarId({:?};`{}`;{:?})", + self.var_path.hir_id, + name, + self.closure_expr_id) + } +} + +impl fmt::Debug for ty::UpvarBorrow<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "UpvarBorrow({:?}, {:?})", + self.kind, self.region) + } +} + +impl fmt::Debug for ty::ExistentialTraitRef<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Debug for ty::adjustment::Adjustment<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?} -> {}", self.kind, self.target) + } +} + +impl fmt::Debug for ty::BoundRegion { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ty::BrAnon(n) => write!(f, "BrAnon({:?})", n), + ty::BrFresh(n) => write!(f, "BrFresh({:?})", n), + ty::BrNamed(did, name) => { + write!(f, "BrNamed({:?}:{:?}, {})", + did.krate, did.index, name) + } + ty::BrEnv => write!(f, "BrEnv"), + } + } +} + +impl fmt::Debug for ty::RegionKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ty::ReEarlyBound(ref data) => { + write!(f, "ReEarlyBound({}, {})", + data.index, + data.name) + } + + ty::ReClosureBound(ref vid) => { + write!(f, "ReClosureBound({:?})", vid) + } + + ty::ReLateBound(binder_id, ref bound_region) => { + write!(f, "ReLateBound({:?}, {:?})", binder_id, bound_region) + } + + ty::ReFree(ref fr) => fr.fmt(f), + + ty::ReScope(id) => write!(f, "ReScope({:?})", id), + + ty::ReStatic => write!(f, "ReStatic"), + + ty::ReVar(ref vid) => vid.fmt(f), + + ty::RePlaceholder(placeholder) => { + write!(f, "RePlaceholder({:?})", placeholder) + } + + ty::ReEmpty => write!(f, "ReEmpty"), + + ty::ReErased => write!(f, "ReErased"), + } + } +} + +impl fmt::Debug for ty::FreeRegion { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region) + } +} + +impl fmt::Debug for ty::Variance { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match *self { + ty::Covariant => "+", + ty::Contravariant => "-", + ty::Invariant => "o", + ty::Bivariant => "*", + }) + } +} + +impl fmt::Debug for ty::FnSig<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "({:?}; c_variadic: {})->{:?}", + self.inputs(), self.c_variadic, self.output()) + } +} + +impl fmt::Debug for ty::TyVid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "_#{}t", self.index) + } +} + +impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "_#{}c", self.index) + } +} + +impl fmt::Debug for ty::IntVid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "_#{}i", self.index) + } +} + +impl fmt::Debug for ty::FloatVid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "_#{}f", self.index) + } +} + +impl fmt::Debug for ty::RegionVid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "'_#{}r", self.index()) + } +} + +impl fmt::Debug for ty::InferTy { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ty::TyVar(ref v) => v.fmt(f), + ty::IntVar(ref v) => v.fmt(f), + ty::FloatVar(ref v) => v.fmt(f), + ty::FreshTy(v) => write!(f, "FreshTy({:?})", v), + ty::FreshIntTy(v) => write!(f, "FreshIntTy({:?})", v), + ty::FreshFloatTy(v) => write!(f, "FreshFloatTy({:?})", v), + } + } +} + +impl fmt::Debug for ty::IntVarValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ty::IntType(ref v) => v.fmt(f), + ty::UintType(ref v) => v.fmt(f), + } + } +} + +impl fmt::Debug for ty::FloatVarValue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl fmt::Debug for ty::TraitRef<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // HACK(eddyb) this is used across the compiler to print + // a `TraitRef` qualified (with the Self type explicit), + // instead of having a different way to make that choice. + write!(f, "<{} as {}>", self.self_ty(), self) + } +} + +impl fmt::Debug for Ty<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Debug for ty::ParamTy { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}/#{}", self.name, self.idx) + } +} + +impl fmt::Debug for ty::ParamConst { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}/#{}", self.name, self.index) + } +} + +impl fmt::Debug for ty::TraitPredicate<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "TraitPredicate({:?})", self.trait_ref) + } +} + +impl fmt::Debug for ty::ProjectionPredicate<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty) + } +} + +impl fmt::Debug for ty::Predicate<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ty::Predicate::Trait(ref a) => a.fmt(f), + ty::Predicate::Subtype(ref pair) => pair.fmt(f), + ty::Predicate::RegionOutlives(ref pair) => pair.fmt(f), + ty::Predicate::TypeOutlives(ref pair) => pair.fmt(f), + ty::Predicate::Projection(ref pair) => pair.fmt(f), + ty::Predicate::WellFormed(ty) => write!(f, "WellFormed({:?})", ty), + ty::Predicate::ObjectSafe(trait_def_id) => { + write!(f, "ObjectSafe({:?})", trait_def_id) + } + ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => { + write!(f, "ClosureKind({:?}, {:?}, {:?})", + closure_def_id, closure_substs, kind) + } + ty::Predicate::ConstEvaluatable(def_id, substs) => { + write!(f, "ConstEvaluatable({:?}, {:?})", def_id, substs) + } + } + } +} + /////////////////////////////////////////////////////////////////////////// // Atomic structs // diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index 8c67cdf62b62..846428656146 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -12,6 +12,7 @@ use smallvec::SmallVec; use rustc_macros::HashStable; use core::intrinsics; +use std::fmt; use std::cmp::Ordering; use std::marker::PhantomData; use std::mem; @@ -69,6 +70,16 @@ impl<'tcx> UnpackedKind<'tcx> { } } +impl fmt::Debug for Kind<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.unpack() { + UnpackedKind::Lifetime(lt) => lt.fmt(f), + UnpackedKind::Type(ty) => ty.fmt(f), + UnpackedKind::Const(ct) => ct.fmt(f), + } + } +} + impl<'tcx> Ord for Kind<'tcx> { fn cmp(&self, other: &Kind<'_>) -> Ordering { self.unpack().cmp(&other.unpack()) diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 4c9d26c84816..2e11a3741369 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -2,16 +2,20 @@ use crate::hir; use crate::hir::def::Namespace; use crate::ty::subst::{Kind, UnpackedKind}; use crate::ty::{self, ParamConst, Ty}; -use crate::ty::print::{FmtPrinter, PrettyPrinter, PrintCx, Print, Printer}; +use crate::ty::print::{FmtPrinter, PrettyPrinter, PrintCx, Print}; use crate::mir::interpret::ConstValue; -use std::fmt::{self, Write as _}; +use std::fmt; use std::iter; use rustc_target::spec::abi::Abi; macro_rules! define_print { - (@display $target:ty, ($self:ident, $cx:ident) $disp:block) => { + ([$($target:ty),+] $vars:tt $def:tt) => { + $(define_print!($target, $vars $def);)+ + }; + + ($target:ty, ($self:ident, $cx:ident) { display $disp:block }) => { impl Print<'tcx, P> for $target { type Output = P; type Error = fmt::Error; @@ -34,38 +38,6 @@ macro_rules! define_print { } } }; - - (@debug $target:ty, ($self:ident, $cx:ident) $dbg:block) => { - impl fmt::Debug for $target { - fn fmt(&$self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |$cx| { - #[allow(unused_mut)] - let mut $cx = $cx; - define_scoped_cx!($cx); - let _: () = $dbg; - let _ = $cx; - Ok(()) - }) - } - } - }; - - ([$($target:ty),+] $vars:tt $def:tt) => { - $(define_print!($target, $vars $def);)+ - }; - - ($target:ty, $vars:tt { - display $disp:block - debug $dbg:block - }) => { - define_print!(@display $target, $vars $disp); - define_print!(@debug $target, $vars $dbg); - }; - ($target:ty, $vars:tt { - display $disp:block - }) => { - define_print!(@display $target, $vars $disp); - }; } macro_rules! nest { @@ -160,67 +132,6 @@ define_print! { } } -impl fmt::Debug for ty::GenericParamDef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let type_name = match self.kind { - ty::GenericParamDefKind::Lifetime => "Lifetime", - ty::GenericParamDefKind::Type { .. } => "Type", - ty::GenericParamDefKind::Const => "Const", - }; - write!(f, "{}({}, {:?}, {})", - type_name, - self.name, - self.def_id, - self.index) - } -} - -impl fmt::Debug for ty::TraitDef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| { - cx.print_def_path(self.def_id, None, iter::empty())?; - Ok(()) - }) - } -} - -impl fmt::Debug for ty::AdtDef { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::TypeNS), |cx| { - cx.print_def_path(self.did, None, iter::empty())?; - Ok(()) - }) - } -} - -impl<'tcx> fmt::Debug for ty::ClosureUpvar<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "ClosureUpvar({:?},{:?})", - self.def, - self.ty) - } -} - -impl fmt::Debug for ty::UpvarId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - PrintCx::with_tls_tcx(FmtPrinter::new(f, Namespace::ValueNS), |mut cx| { - define_scoped_cx!(cx); - p!(write("UpvarId({:?};`{}`;{:?})", - self.var_path.hir_id, - cx.tcx.hir().name_by_hir_id(self.var_path.hir_id), - self.closure_expr_id)); - Ok(()) - }) - } -} - -impl<'tcx> fmt::Debug for ty::UpvarBorrow<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "UpvarBorrow({:?}, {:?})", - self.kind, self.region) - } -} - define_print! { &'tcx ty::List>, (self, cx) { display { @@ -260,91 +171,11 @@ define_print! { } } -impl fmt::Debug for ty::ExistentialTraitRef<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(self, f) - } -} - -impl fmt::Debug for ty::adjustment::Adjustment<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?} -> {}", self.kind, self.target) - } -} - -impl fmt::Debug for ty::BoundRegion { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { - ty::BrAnon(n) => write!(f, "BrAnon({:?})", n), - ty::BrFresh(n) => write!(f, "BrFresh({:?})", n), - ty::BrNamed(did, name) => { - write!(f, "BrNamed({:?}:{:?}, {})", - did.krate, did.index, name) - } - ty::BrEnv => write!(f, "BrEnv"), - } - } -} - define_print! { ty::RegionKind, (self, cx) { display { return cx.print_region(self); } - debug { - match *self { - ty::ReEarlyBound(ref data) => { - p!(write("ReEarlyBound({}, {})", - data.index, - data.name)) - } - - ty::ReClosureBound(ref vid) => { - p!(write("ReClosureBound({:?})", vid)) - } - - ty::ReLateBound(binder_id, ref bound_region) => { - p!(write("ReLateBound({:?}, {:?})", binder_id, bound_region)) - } - - ty::ReFree(ref fr) => p!(write("{:?}", fr)), - - ty::ReScope(id) => { - p!(write("ReScope({:?})", id)) - } - - ty::ReStatic => p!(write("ReStatic")), - - ty::ReVar(ref vid) => { - p!(write("{:?}", vid)); - } - - ty::RePlaceholder(placeholder) => { - p!(write("RePlaceholder({:?})", placeholder)) - } - - ty::ReEmpty => p!(write("ReEmpty")), - - ty::ReErased => p!(write("ReErased")) - } - } - } -} - -impl fmt::Debug for ty::FreeRegion { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "ReFree({:?}, {:?})", self.scope, self.bound_region) - } -} - -impl fmt::Debug for ty::Variance { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(match *self { - ty::Covariant => "+", - ty::Contravariant => "-", - ty::Invariant => "o", - ty::Bivariant => "*", - }) } } @@ -362,40 +193,6 @@ define_print! { p!(write("fn")); nest!(|cx| cx.pretty_fn_sig(self.inputs(), self.c_variadic, self.output())); } - debug { - p!(write("({:?}; c_variadic: {})->{:?}", - self.inputs(), self.c_variadic, self.output())) - } - } -} - -impl fmt::Debug for ty::TyVid { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "_#{}t", self.index) - } -} - -impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "_#{}f", self.index) - } -} - -impl fmt::Debug for ty::IntVid { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "_#{}i", self.index) - } -} - -impl fmt::Debug for ty::FloatVid { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "_#{}f", self.index) - } -} - -impl fmt::Debug for ty::RegionVid { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "'_#{}r", self.index()) } } @@ -415,31 +212,6 @@ define_print! { ty::FreshFloatTy(v) => p!(write("FreshFloatTy({})", v)) } } - debug { - match *self { - ty::TyVar(ref v) => p!(write("{:?}", v)), - ty::IntVar(ref v) => p!(write("{:?}", v)), - ty::FloatVar(ref v) => p!(write("{:?}", v)), - ty::FreshTy(v) => p!(write("FreshTy({:?})", v)), - ty::FreshIntTy(v) => p!(write("FreshIntTy({:?})", v)), - ty::FreshFloatTy(v) => p!(write("FreshFloatTy({:?})", v)) - } - } - } -} - -impl fmt::Debug for ty::IntVarValue { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { - ty::IntType(ref v) => v.fmt(f), - ty::UintType(ref v) => v.fmt(f), - } - } -} - -impl fmt::Debug for ty::FloatVarValue { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.0.fmt(f) } } @@ -478,12 +250,6 @@ define_print! { display { nest!(|cx| cx.print_def_path(self.def_id, Some(self.substs), iter::empty())); } - debug { - // HACK(eddyb) this is used across the compiler to print - // a `TraitRef` qualified (with the Self type explicit), - // instead of having a different way to make that choice. - p!(write("<{} as {}>", self.self_ty(), self)) - } } } @@ -495,12 +261,6 @@ define_print! { } } -impl fmt::Debug for Ty<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(self, f) - } -} - define_print! { ConstValue<'tcx>, (self, cx) { display { @@ -538,9 +298,6 @@ define_print! { display { p!(write("{}", self.name)) } - debug { - p!(write("{}/#{}", self.name, self.idx)) - } } } @@ -549,9 +306,6 @@ define_print! { display { p!(write("{}", self.name)) } - debug { - p!(write("{}/#{}", self.name, self.index)) - } } } @@ -581,10 +335,6 @@ define_print! { display { p!(print(self.trait_ref.self_ty()), write(": "), print(self.trait_ref)) } - debug { - p!(write("TraitPredicate({:?})", - self.trait_ref)) - } } } @@ -593,9 +343,6 @@ define_print! { display { p!(print(self.projection_ty), write(" == "), print(self.ty)) } - debug { - p!(write("ProjectionPredicate({:?}, {:?})", self.projection_ty, self.ty)) - } } } @@ -646,26 +393,6 @@ define_print! { } } } - debug { - match *self { - ty::Predicate::Trait(ref a) => p!(write("{:?}", a)), - ty::Predicate::Subtype(ref pair) => p!(write("{:?}", pair)), - ty::Predicate::RegionOutlives(ref pair) => p!(write("{:?}", pair)), - ty::Predicate::TypeOutlives(ref pair) => p!(write("{:?}", pair)), - ty::Predicate::Projection(ref pair) => p!(write("{:?}", pair)), - ty::Predicate::WellFormed(ty) => p!(write("WellFormed({:?})", ty)), - ty::Predicate::ObjectSafe(trait_def_id) => { - p!(write("ObjectSafe({:?})", trait_def_id)) - } - ty::Predicate::ClosureKind(closure_def_id, closure_substs, kind) => { - p!(write("ClosureKind({:?}, {:?}, {:?})", - closure_def_id, closure_substs, kind)) - } - ty::Predicate::ConstEvaluatable(def_id, substs) => { - p!(write("ConstEvaluatable({:?}, {:?})", def_id, substs)) - } - } - } } } @@ -678,12 +405,5 @@ define_print! { UnpackedKind::Const(ct) => p!(print(ct)), } } - debug { - match self.unpack() { - UnpackedKind::Lifetime(lt) => p!(write("{:?}", lt)), - UnpackedKind::Type(ty) => p!(write("{:?}", ty)), - UnpackedKind::Const(ct) => p!(write("{:?}", ct)), - } - } } }