X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_middle%2Fsrc%2Fty%2Fprint%2Fpretty.rs;h=ae838a461574b2df1dee5d0a317423c33c28975f;hb=92d20c4aaddea9507f8ad37fe37c551219153bbf;hp=ddcc8680d835269bd4f34257bccb79f01db361c6;hpb=c03bf54dd106cf6afc07c426cbb3522e6839c31b;p=rust.git diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index ddcc8680d83..ae838a46157 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -3,6 +3,7 @@ use crate::ty::{self, ConstInt, DefIdTree, ParamConst, ScalarInt, Term, Ty, TyCtxt, TypeFoldable}; use rustc_apfloat::ieee::{Double, Single}; use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::intern::Interned; use rustc_data_structures::sso::SsoHashSet; use rustc_hir as hir; use rustc_hir::def::{self, CtorKind, DefKind, Namespace}; @@ -130,11 +131,13 @@ pub fn with_no_visible_paths R, R>(f: F) -> R { /// /// Regions not selected by the region highlight mode are presently /// unaffected. -#[derive(Copy, Clone, Default)] -pub struct RegionHighlightMode { +#[derive(Copy, Clone)] +pub struct RegionHighlightMode<'tcx> { + tcx: TyCtxt<'tcx>, + /// If enabled, when we see the selected region, use "`'N`" /// instead of the ordinary behavior. - highlight_regions: [Option<(ty::RegionKind, usize)>; 3], + highlight_regions: [Option<(ty::Region<'tcx>, usize)>; 3], /// If enabled, when printing a "free region" that originated from /// the given `ty::BoundRegionKind`, print it as "`'1`". Free regions that would ordinarily @@ -146,12 +149,20 @@ pub struct RegionHighlightMode { highlight_bound_region: Option<(ty::BoundRegionKind, usize)>, } -impl RegionHighlightMode { +impl<'tcx> RegionHighlightMode<'tcx> { + pub fn new(tcx: TyCtxt<'tcx>) -> Self { + Self { + tcx, + highlight_regions: Default::default(), + highlight_bound_region: Default::default(), + } + } + /// If `region` and `number` are both `Some`, invokes /// `highlighting_region`. pub fn maybe_highlighting_region( &mut self, - region: Option>, + region: Option>, number: Option, ) { if let Some(k) = region { @@ -162,24 +173,24 @@ pub fn maybe_highlighting_region( } /// Highlights the region inference variable `vid` as `'N`. - pub fn highlighting_region(&mut self, region: ty::Region<'_>, number: usize) { + pub fn highlighting_region(&mut self, region: ty::Region<'tcx>, number: usize) { let num_slots = self.highlight_regions.len(); let first_avail_slot = self.highlight_regions.iter_mut().find(|s| s.is_none()).unwrap_or_else(|| { bug!("can only highlight {} placeholders at a time", num_slots,) }); - *first_avail_slot = Some((*region, number)); + *first_avail_slot = Some((region, number)); } /// Convenience wrapper for `highlighting_region`. pub fn highlighting_region_vid(&mut self, vid: ty::RegionVid, number: usize) { - self.highlighting_region(&ty::ReVar(vid), number) + self.highlighting_region(self.tcx.mk_region(ty::ReVar(vid)), number) } /// Returns `Some(n)` with the number to use for the given region, if any. fn region_highlighted(&self, region: ty::Region<'_>) -> Option { self.highlight_regions.iter().find_map(|h| match h { - Some((r, n)) if r == region => Some(*n), + Some((r, n)) if *r == region => Some(*n), _ => None, }) } @@ -743,14 +754,14 @@ fn pretty_print_type(mut self, ty: Ty<'tcx>) -> Result p!("[", print(ty), "; "); if self.tcx().sess.verbose() { p!(write("{:?}", sz)); - } else if let ty::ConstKind::Unevaluated(..) = sz.val { + } else if let ty::ConstKind::Unevaluated(..) = sz.val() { // Do not try to evaluate unevaluated constants. If we are const evaluating an // array length anon const, rustc will (with debug assertions) print the // constant's path. Which will end up here again. p!("_"); - } else if let Some(n) = sz.val.try_to_bits(self.tcx().data_layout.pointer_size) { + } else if let Some(n) = sz.val().try_to_bits(self.tcx().data_layout.pointer_size) { p!(write("{}", n)); - } else if let ty::ConstKind::Param(param) = sz.val { + } else if let ty::ConstKind::Param(param) = sz.val() { p!(write("{}", param)); } else { p!("_"); @@ -1053,7 +1064,7 @@ fn pretty_print_dyn_existential( // Don't print `'_` if there's no unerased regions. let print_regions = args.iter().any(|arg| match arg.unpack() { - GenericArgKind::Lifetime(r) => *r != ty::ReErased, + GenericArgKind::Lifetime(r) => !r.is_erased(), _ => false, }); let mut args = args.iter().cloned().filter(|arg| match arg.unpack() { @@ -1137,13 +1148,13 @@ fn pretty_fn_sig( fn pretty_print_const( mut self, - ct: &'tcx ty::Const<'tcx>, + ct: ty::Const<'tcx>, print_ty: bool, ) -> Result { define_scoped_cx!(self); if self.tcx().sess.verbose() { - p!(write("Const({:?}: {:?})", ct.val, ct.ty)); + p!(write("Const({:?}: {:?})", ct.val(), ct.ty())); return Ok(self); } @@ -1155,7 +1166,7 @@ macro_rules! print_underscore { write!(this, "_")?; Ok(this) }, - |this| this.print_type(ct.ty), + |this| this.print_type(ct.ty()), ": ", )?; } else { @@ -1164,7 +1175,7 @@ macro_rules! print_underscore { }}; } - match ct.val { + match ct.val() { ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, @@ -1195,7 +1206,7 @@ macro_rules! print_underscore { ty::ConstKind::Infer(..) => print_underscore!(), ty::ConstKind::Param(ParamConst { name, .. }) => p!(write("{}", name)), ty::ConstKind::Value(value) => { - return self.pretty_print_const_value(value, ct.ty, print_ty); + return self.pretty_print_const_value(value, ct.ty(), print_ty); } ty::ConstKind::Bound(debruijn, bound_var) => { @@ -1232,16 +1243,23 @@ fn pretty_print_const_scalar_ptr( // Byte strings (&[u8; N]) ty::Ref( _, - ty::TyS { - kind: - ty::Array( - ty::TyS { kind: ty::Uint(ty::UintTy::U8), .. }, - ty::Const { - val: ty::ConstKind::Value(ConstValue::Scalar(int)), .. - }, - ), - .. - }, + Ty(Interned( + ty::TyS { + kind: + ty::Array( + Ty(Interned(ty::TyS { kind: ty::Uint(ty::UintTy::U8), .. }, _)), + ty::Const(Interned( + ty::ConstS { + val: ty::ConstKind::Value(ConstValue::Scalar(int)), + .. + }, + _, + )), + ), + .. + }, + _, + )), _, ) => match self.tcx().get_global_alloc(alloc_id) { Some(GlobalAlloc::Memory(alloc)) => { @@ -1399,7 +1417,7 @@ fn pretty_print_const_value( // Byte/string slices, printed as (byte) string literals. ( ConstValue::Slice { data, start, end }, - ty::Ref(_, ty::TyS { kind: ty::Slice(t), .. }, _), + ty::Ref(_, Ty(Interned(ty::TyS { kind: ty::Slice(t), .. }, _)), _), ) if *t == u8_type => { // The `inspect` here is okay since we checked the bounds, and there are // no relocations (we have an active slice reference here). We don't use @@ -1409,7 +1427,7 @@ fn pretty_print_const_value( } ( ConstValue::Slice { data, start, end }, - ty::Ref(_, ty::TyS { kind: ty::Str, .. }, _), + ty::Ref(_, Ty(Interned(ty::TyS { kind: ty::Str, .. }, _)), _), ) => { // The `inspect` here is okay since we checked the bounds, and there are no // relocations (we have an active `str` reference here). We don't use this @@ -1420,7 +1438,7 @@ fn pretty_print_const_value( Ok(self) } (ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => { - let n = n.val.try_to_bits(self.tcx().data_layout.pointer_size).unwrap(); + let n = n.val().try_to_bits(self.tcx().data_layout.pointer_size).unwrap(); // cast is ok because we already checked for pointer size (32 or 64 bit) above let range = AllocRange { start: offset, size: Size::from_bytes(n) }; @@ -1441,10 +1459,18 @@ fn pretty_print_const_value( // FIXME(eddyb) for `--emit=mir`/`-Z dump-mir`, we should provide the // correct `ty::ParamEnv` to allow printing *all* constant values. (_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) if !ty.has_param_types_or_consts() => { - let contents = self.tcx().destructure_const( + let Some(contents) = self.tcx().try_destructure_const( ty::ParamEnv::reveal_all() - .and(self.tcx().mk_const(ty::Const { val: ty::ConstKind::Value(ct), ty })), - ); + .and(self.tcx().mk_const(ty::ConstS { val: ty::ConstKind::Value(ct), ty })), + ) else { + // Fall back to debug pretty printing for invalid constants. + p!(write("{:?}", ct)); + if print_ty { + p!(": ", print(ty)); + } + return Ok(self); + }; + let fields = contents.fields.iter().copied(); match *ty.kind() { @@ -1531,7 +1557,7 @@ pub struct FmtPrinterData<'a, 'tcx, F> { binder_depth: usize, printed_type_count: usize, - pub region_highlight_mode: RegionHighlightMode, + pub region_highlight_mode: RegionHighlightMode<'tcx>, pub name_resolver: Option Option>>, } @@ -1561,7 +1587,7 @@ pub fn new(tcx: TyCtxt<'tcx>, fmt: F, ns: Namespace) -> Self { region_index: 0, binder_depth: 0, printed_type_count: 0, - region_highlight_mode: RegionHighlightMode::default(), + region_highlight_mode: RegionHighlightMode::new(tcx), name_resolver: None, })) } @@ -1702,7 +1728,7 @@ fn print_dyn_existential( self.pretty_print_dyn_existential(predicates) } - fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result { + fn print_const(self, ct: ty::Const<'tcx>) -> Result { self.pretty_print_const(ct, true) } @@ -1797,7 +1823,7 @@ fn path_generic_args( // Don't print `'_` if there's no unerased regions. let print_regions = self.tcx.sess.verbose() || args.iter().any(|arg| match arg.unpack() { - GenericArgKind::Lifetime(r) => *r != ty::ReErased, + GenericArgKind::Lifetime(r) => !r.is_erased(), _ => false, }); let args = args.iter().cloned().filter(|arg| match arg.unpack() { @@ -2056,7 +2082,7 @@ fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { let name = &mut self.name; let region = match *r { - ty::ReLateBound(_, br) => self.region_map.entry(br).or_insert_with(|| name(br)), + ty::ReLateBound(_, br) => *self.region_map.entry(br).or_insert_with(|| name(br)), ty::RePlaceholder(ty::PlaceholderRegion { name: kind, .. }) => { // If this is an anonymous placeholder, don't rename. Otherwise, in some // async fns, we get a `for<'r> Send` bound @@ -2065,7 +2091,7 @@ fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { _ => { // Index doesn't matter, since this is just for naming and these never get bound let br = ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind }; - self.region_map.entry(br).or_insert_with(|| name(br)) + *self.region_map.entry(br).or_insert_with(|| name(br)) } } } @@ -2267,7 +2293,7 @@ impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector<'_, 'tcx #[instrument(skip(self), level = "trace")] fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow { - trace!("address: {:p}", r); + trace!("address: {:p}", r.0.0); if let ty::ReLateBound(_, ty::BoundRegion { kind: ty::BrNamed(_, name), .. }) = *r { self.used_region_names.insert(name); } else if let ty::RePlaceholder(ty::PlaceholderRegion { @@ -2364,7 +2390,7 @@ fn print(&$self, $cx: P) -> Result { } // HACK(eddyb) this is separate because `ty::RegionKind` doesn't need lifting. -impl fmt::Display for ty::RegionKind { +impl<'tcx> fmt::Display for ty::Region<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ty::tls::with(|tcx| { self.print(FmtPrinter::new(tcx, f, Namespace::TypeNS))?; @@ -2439,7 +2465,7 @@ pub fn print_modifiers_and_trait_path( forward_display_to_print! { Ty<'tcx>, &'tcx ty::List>>, - &'tcx ty::Const<'tcx>, + ty::Const<'tcx>, // HACK(eddyb) these are exhaustive instead of generic, // because `for<'tcx>` isn't possible yet.