From e16d71b706e54b3f21305b2042635b94a69b879e Mon Sep 17 00:00:00 2001 From: Boxy Date: Tue, 16 May 2023 04:25:25 +0100 Subject: [PATCH 1/2] format `Const`'s less verbosely --- compiler/rustc_middle/src/ty/consts.rs | 10 ---- compiler/rustc_middle/src/ty/consts/kind.rs | 4 +- compiler/rustc_middle/src/ty/print/pretty.rs | 60 ++++++++++--------- .../rustc_middle/src/ty/structural_impls.rs | 38 ++++++++++++ 4 files changed, 71 insertions(+), 41 deletions(-) diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 0a5792a9c4713..1a4bd14815f93 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -6,7 +6,6 @@ use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::LocalDefId; use rustc_macros::HashStable; -use std::fmt; mod int; mod kind; @@ -21,15 +20,6 @@ pub use valtree::*; #[rustc_pass_by_value] pub struct Const<'tcx>(pub(super) Interned<'tcx, ConstData<'tcx>>); -impl<'tcx> fmt::Debug for Const<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - // This reflects what `Const` looked liked before `Interned` was - // introduced. We print it like this to avoid having to update expected - // output in a lot of tests. - write!(f, "Const {{ ty: {:?}, kind: {:?} }}", self.ty(), self.kind()) - } -} - /// Typed constant value. #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, HashStable, TyEncodable, TyDecodable)] pub struct ConstData<'tcx> { diff --git a/compiler/rustc_middle/src/ty/consts/kind.rs b/compiler/rustc_middle/src/ty/consts/kind.rs index a108f6c8947a3..1dd4f8a243741 100644 --- a/compiler/rustc_middle/src/ty/consts/kind.rs +++ b/compiler/rustc_middle/src/ty/consts/kind.rs @@ -42,7 +42,7 @@ impl<'tcx> UnevaluatedConst<'tcx> { } /// Represents a constant in Rust. -#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)] +#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)] #[derive(Hash, HashStable, TypeFoldable, TypeVisitable)] #[derive(derive_more::From)] pub enum ConstKind<'tcx> { @@ -128,7 +128,7 @@ impl<'tcx> ConstKind<'tcx> { } /// An inference variable for a const, for use in const generics. -#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)] +#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)] pub enum InferConst<'tcx> { /// Infer the value of the const. Var(ty::ConstVid<'tcx>), diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 385156262c76a..a8193c3e5591f 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -703,7 +703,7 @@ pub trait PrettyPrinter<'tcx>: ty::Error(_) => p!("[type error]"), ty::Param(ref param_ty) => p!(print(param_ty)), ty::Bound(debruijn, bound_ty) => match bound_ty.kind { - ty::BoundTyKind::Anon => self.pretty_print_bound_var(debruijn, bound_ty.var)?, + ty::BoundTyKind::Anon => debug_bound_var(&mut self, debruijn, bound_ty.var)?, ty::BoundTyKind::Param(_, s) => match self.should_print_verbose() { true if debruijn == ty::INNERMOST => p!(write("^{}", s)), true => p!(write("^{}_{}", debruijn.index(), s)), @@ -741,7 +741,7 @@ pub trait PrettyPrinter<'tcx>: } ty::Placeholder(placeholder) => match placeholder.bound.kind { ty::BoundTyKind::Anon => { - self.pretty_print_placeholder_var(placeholder.universe, placeholder.bound.var)? + debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound.var)?; } ty::BoundTyKind::Param(_, name) => p!(write("{}", name)), }, @@ -1164,30 +1164,6 @@ pub trait PrettyPrinter<'tcx>: traits.entry(trait_ref).or_default().extend(proj_ty); } - fn pretty_print_bound_var( - &mut self, - debruijn: ty::DebruijnIndex, - var: ty::BoundVar, - ) -> Result<(), Self::Error> { - if debruijn == ty::INNERMOST { - write!(self, "^{}", var.index()) - } else { - write!(self, "^{}_{}", debruijn.index(), var.index()) - } - } - - fn pretty_print_placeholder_var( - &mut self, - ui: ty::UniverseIndex, - var: ty::BoundVar, - ) -> Result<(), Self::Error> { - if ui == ty::UniverseIndex::ROOT { - write!(self, "!{}", var.index()) - } else { - write!(self, "!{}_{}", ui.index(), var.index()) - } - } - fn ty_infer_name(&self, _: ty::TyVid) -> Option { None } @@ -1321,7 +1297,7 @@ pub trait PrettyPrinter<'tcx>: define_scoped_cx!(self); if self.should_print_verbose() { - p!(write("Const({:?}: {:?})", ct.kind(), ct.ty())); + p!(write("{:?}", ct)); return Ok(self); } @@ -1380,9 +1356,11 @@ pub trait PrettyPrinter<'tcx>: } ty::ConstKind::Bound(debruijn, bound_var) => { - self.pretty_print_bound_var(debruijn, bound_var)? + debug_bound_var(&mut self, debruijn, bound_var)? } - ty::ConstKind::Placeholder(placeholder) => p!(write("Placeholder({:?})", placeholder)), + ty::ConstKind::Placeholder(placeholder) => { + debug_placeholder_var(&mut self, placeholder.universe, placeholder.bound)?; + }, // FIXME(generic_const_exprs): // write out some legible representation of an abstract const? ty::ConstKind::Expr(_) => p!("[const expr]"), @@ -3067,3 +3045,27 @@ pub struct OpaqueFnEntry<'tcx> { fn_trait_ref: Option>, return_ty: Option>>, } + +pub fn debug_bound_var( + fmt: &mut T, + debruijn: ty::DebruijnIndex, + var: ty::BoundVar, +) -> Result<(), std::fmt::Error> { + if debruijn == ty::INNERMOST { + write!(fmt, "^{}", var.index()) + } else { + write!(fmt, "^{}_{}", debruijn.index(), var.index()) + } +} + +pub fn debug_placeholder_var( + fmt: &mut T, + universe: ty::UniverseIndex, + bound: ty::BoundVar, +) -> Result<(), std::fmt::Error> { + if universe == ty::UniverseIndex::ROOT { + write!(fmt, "!{}", bound.index()) + } else { + write!(fmt, "!{}_{}", universe.index(), bound.index()) + } +} diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index e73208b877f10..16cb6c910463a 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -192,6 +192,44 @@ impl<'tcx> fmt::Debug for AliasTy<'tcx> { } } +impl<'tcx> fmt::Debug for ty::InferConst<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + InferConst::Var(var) => write!(f, "{var:?}"), + InferConst::Fresh(var) => write!(f, "Fresh({var:?})"), + } + } +} + +impl<'tcx> fmt::Debug for ty::Const<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // This reflects what `Const` looked liked before `Interned` was + // introduced. We print it like this to avoid having to update expected + // output in a lot of tests. + write!(f, "Const {{ ty: {:?}, kind: {:?} }}", self.ty(), self.kind()) + } +} + +impl<'tcx> fmt::Debug for ty::ConstKind<'tcx> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use ty::ConstKind::*; + match self { + Param(param) => write!(f, "{param:?}"), + Infer(var) => write!(f, "{var:?}"), + Bound(debruijn, var) => ty::print::debug_bound_var(f, *debruijn, *var), + Placeholder(placeholder) => { + ty::print::debug_placeholder_var(f, placeholder.universe, placeholder.bound) + } + Unevaluated(uv) => { + f.debug_tuple("Unevaluated").field(&uv.substs).field(&uv.def).finish() + } + Value(valtree) => write!(f, "{valtree:?}"), + Error(_) => write!(f, "[const error]"), + Expr(expr) => write!(f, "{expr:?}"), + } + } +} + /////////////////////////////////////////////////////////////////////////// // Atomic structs // From 2a554eb40677a659092c5b95a52821275924c4bf Mon Sep 17 00:00:00 2001 From: Boxy Date: Tue, 16 May 2023 17:16:01 +0100 Subject: [PATCH 2/2] bless --- tests/mir-opt/issue_99325.main.built.after.mir | 4 ++-- tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir | 2 +- tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/mir-opt/issue_99325.main.built.after.mir b/tests/mir-opt/issue_99325.main.built.after.mir index f0c9ef419bd89..0424ce3abeb2e 100644 --- a/tests/mir-opt/issue_99325.main.built.after.mir +++ b/tests/mir-opt/issue_99325.main.built.after.mir @@ -1,8 +1,8 @@ // MIR for `main` after built | User Type Annotations -| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Value(Branch([Leaf(0x41), Leaf(0x41), Leaf(0x41), Leaf(0x41)])) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:10:16: 10:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">} -| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Unevaluated(UnevaluatedConst { def: DefId(0:8 ~ issue_99325[22bb]::main::{constant#1}), substs: [] }) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:11:16: 11:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">} +| 0: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Branch([Leaf(0x41), Leaf(0x41), Leaf(0x41), Leaf(0x41)]) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:10:16: 10:46, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">} +| 1: user_ty: Canonical { value: TypeOf(DefId(0:3 ~ issue_99325[22bb]::function_with_bytes), UserSubsts { substs: [Const { ty: &'static [u8; 4], kind: Unevaluated([], DefId(0:8 ~ issue_99325[22bb]::main::{constant#1})) }], user_self_ty: None }), max_universe: U0, variables: [] }, span: $DIR/issue_99325.rs:11:16: 11:68, inferred_ty: fn() -> &'static [u8] {function_with_bytes::<&*b"AAAA">} | fn main() -> () { let mut _0: (); // return place in scope 0 at $DIR/issue_99325.rs:+0:15: +0:15 diff --git a/tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir b/tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir index 71bdfcc5c4991..c425f3cd5069b 100644 --- a/tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir +++ b/tests/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir @@ -22,7 +22,7 @@ | fn main() -> () { let mut _0: (); // return place in scope 0 at $DIR/region_subtyping_basic.rs:+0:11: +0:11 - let mut _1: [usize; Const(Value(Leaf(0x00000003)): usize)]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14 + let mut _1: [usize; Const { ty: usize, kind: Leaf(0x00000003) }]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14 let _3: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:16: +2:17 let mut _4: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18 let mut _5: bool; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18 diff --git a/tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir b/tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir index 9fa8609b751a2..22ad24f8d7724 100644 --- a/tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir +++ b/tests/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir @@ -22,7 +22,7 @@ | fn main() -> () { let mut _0: (); // return place in scope 0 at $DIR/region_subtyping_basic.rs:+0:11: +0:11 - let mut _1: [usize; Const(Value(Leaf(0x0000000000000003)): usize)]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14 + let mut _1: [usize; Const { ty: usize, kind: Leaf(0x0000000000000003) }]; // in scope 0 at $DIR/region_subtyping_basic.rs:+1:9: +1:14 let _3: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:16: +2:17 let mut _4: usize; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18 let mut _5: bool; // in scope 0 at $DIR/region_subtyping_basic.rs:+2:14: +2:18