From 50dd8eaeb9ebbccb8b79ff30d3068d0ee337cd2f Mon Sep 17 00:00:00 2001 From: varkor Date: Fri, 24 Jan 2020 16:22:24 +0000 Subject: [PATCH] Print constants in `type_name` for const generics --- src/librustc/ty/print/pretty.rs | 38 +++++++++++++++---- .../symbol_names/legacy.rs | 2 +- .../interpret/intrinsics/type_name.rs | 5 +-- .../const-generics/const-generic-type_name.rs | 11 ++++++ .../const-generic-type_name.stderr | 8 ++++ 5 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 src/test/ui/const-generics/const-generic-type_name.rs create mode 100644 src/test/ui/const-generics/const-generic-type_name.stderr diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 9091de55b7d..7dd3c8f4a72 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -831,7 +831,11 @@ fn pretty_fn_sig( Ok(self) } - fn pretty_print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result { + fn pretty_print_const( + mut self, + ct: &'tcx ty::Const<'tcx>, + print_ty: bool, + ) -> Result { define_scoped_cx!(self); if self.tcx().sess.verbose() { @@ -839,6 +843,15 @@ fn pretty_print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result {{ + p!(write("_")); + if print_ty { + p!(write(": "), print(ct.ty)); + } + }}; + } + match (ct.val, &ct.ty.kind) { (_, ty::FnDef(did, substs)) => p!(print_value_path(*did, substs)), (ty::ConstKind::Unevaluated(did, substs, promoted), _) => { @@ -857,22 +870,27 @@ fn pretty_print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result p!(write("_: "), print(ct.ty)), + (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), + (ty::ConstKind::Value(value), _) => { + return self.pretty_print_const_value(value, ct.ty, print_ty); + } _ => { // fallback - p!(write("{:?} : ", ct.val), print(ct.ty)) + p!(write("{:?}", ct.val)); + if print_ty { + p!(write(" : "), print(ct.ty)); + } } }; Ok(self) @@ -882,6 +900,7 @@ fn pretty_print_const_value( mut self, ct: ConstValue<'tcx>, ty: Ty<'tcx>, + print_ty: bool, ) -> Result { define_scoped_cx!(self); @@ -988,7 +1007,10 @@ fn pretty_print_const_value( }; if !printed { // fallback - p!(write("{:?} : ", ct), print(ty)) + p!(write("{:?}", ct)); + if print_ty { + p!(write(" : "), print(ty)); + } } } }; @@ -1162,7 +1184,7 @@ fn print_dyn_existential( } fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result { - self.pretty_print_const(ct) + self.pretty_print_const(ct, true) } fn path_crate(mut self, cnum: CrateNum) -> Result { diff --git a/src/librustc_codegen_utils/symbol_names/legacy.rs b/src/librustc_codegen_utils/symbol_names/legacy.rs index 4f5b9ce03fc..0dedda9bb6b 100644 --- a/src/librustc_codegen_utils/symbol_names/legacy.rs +++ b/src/librustc_codegen_utils/symbol_names/legacy.rs @@ -237,7 +237,7 @@ fn print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result) -> Result { } } - fn print_const(self, _: &'tcx ty::Const<'tcx>) -> Result { - // don't print constants to the user - Ok(self) + fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result { + self.pretty_print_const(ct, false) } fn print_dyn_existential( diff --git a/src/test/ui/const-generics/const-generic-type_name.rs b/src/test/ui/const-generics/const-generic-type_name.rs new file mode 100644 index 00000000000..28586426b44 --- /dev/null +++ b/src/test/ui/const-generics/const-generic-type_name.rs @@ -0,0 +1,11 @@ +// run-pass + +#![feature(const_generics)] +//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash + +#[derive(Debug)] +struct S; + +fn main() { + assert_eq!(std::any::type_name::>(), "const_generic_type_name::S<3usize>"); +} diff --git a/src/test/ui/const-generics/const-generic-type_name.stderr b/src/test/ui/const-generics/const-generic-type_name.stderr new file mode 100644 index 00000000000..6b60a77effe --- /dev/null +++ b/src/test/ui/const-generics/const-generic-type_name.stderr @@ -0,0 +1,8 @@ +warning: the feature `const_generics` is incomplete and may cause the compiler to crash + --> $DIR/const-generic-type_name.rs:3:12 + | +LL | #![feature(const_generics)] + | ^^^^^^^^^^^^^^ + | + = note: `#[warn(incomplete_features)]` on by default + -- 2.44.0