Skip to content

Commit

Permalink
Print constants in type_name for const generics
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Jan 24, 2020
1 parent dee12bb commit 50dd8ea
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
38 changes: 30 additions & 8 deletions src/librustc/ty/print/pretty.rs
Expand Up @@ -831,14 +831,27 @@ pub trait PrettyPrinter<'tcx>:
Ok(self)
}

fn pretty_print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
fn pretty_print_const(
mut self,
ct: &'tcx ty::Const<'tcx>,
print_ty: bool,
) -> Result<Self::Const, Self::Error> {
define_scoped_cx!(self);

if self.tcx().sess.verbose() {
p!(write("Const({:?}: {:?})", ct.val, ct.ty));
return Ok(self);
}

macro_rules! print_underscore {
() => {{
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), _) => {
Expand All @@ -857,22 +870,27 @@ pub trait PrettyPrinter<'tcx>:
{
p!(write("{}", snip))
} else {
p!(write("_: "), print(ct.ty))
print_underscore!()
}
} else {
p!(write("_: "), print(ct.ty))
print_underscore!()
}
}
}
}
}
(ty::ConstKind::Infer(..), _) => 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)
Expand All @@ -882,6 +900,7 @@ pub trait PrettyPrinter<'tcx>:
mut self,
ct: ConstValue<'tcx>,
ty: Ty<'tcx>,
print_ty: bool,
) -> Result<Self::Const, Self::Error> {
define_scoped_cx!(self);

Expand Down Expand Up @@ -988,7 +1007,10 @@ pub trait PrettyPrinter<'tcx>:
};
if !printed {
// fallback
p!(write("{:?} : ", ct), print(ty))
p!(write("{:?}", ct));
if print_ty {
p!(write(" : "), print(ty));
}
}
}
};
Expand Down Expand Up @@ -1162,7 +1184,7 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
}

fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
self.pretty_print_const(ct)
self.pretty_print_const(ct, true)
}

fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_utils/symbol_names/legacy.rs
Expand Up @@ -237,7 +237,7 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
// only print integers
if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { .. })) = ct.val {
if ct.ty.is_integral() {
return self.pretty_print_const(ct);
return self.pretty_print_const(ct, true);
}
}
self.write_str("_")?;
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_mir/interpret/intrinsics/type_name.rs
Expand Up @@ -69,9 +69,8 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
}
}

fn print_const(self, _: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
// don't print constants to the user
Ok(self)
fn print_const(self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
self.pretty_print_const(ct, false)
}

fn print_dyn_existential(
Expand Down
11 changes: 11 additions & 0 deletions 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<const N: usize>;

fn main() {
assert_eq!(std::any::type_name::<S<3>>(), "const_generic_type_name::S<3usize>");
}
8 changes: 8 additions & 0 deletions 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

0 comments on commit 50dd8ea

Please sign in to comment.