Skip to content

Commit 50dd8ea

Browse files
committed
Print constants in type_name for const generics
1 parent dee12bb commit 50dd8ea

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

src/librustc/ty/print/pretty.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,27 @@ pub trait PrettyPrinter<'tcx>:
831831
Ok(self)
832832
}
833833

834-
fn pretty_print_const(mut self, ct: &'tcx ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
834+
fn pretty_print_const(
835+
mut self,
836+
ct: &'tcx ty::Const<'tcx>,
837+
print_ty: bool,
838+
) -> Result<Self::Const, Self::Error> {
835839
define_scoped_cx!(self);
836840

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

846+
macro_rules! print_underscore {
847+
() => {{
848+
p!(write("_"));
849+
if print_ty {
850+
p!(write(": "), print(ct.ty));
851+
}
852+
}};
853+
}
854+
842855
match (ct.val, &ct.ty.kind) {
843856
(_, ty::FnDef(did, substs)) => p!(print_value_path(*did, substs)),
844857
(ty::ConstKind::Unevaluated(did, substs, promoted), _) => {
@@ -857,22 +870,27 @@ pub trait PrettyPrinter<'tcx>:
857870
{
858871
p!(write("{}", snip))
859872
} else {
860-
p!(write("_: "), print(ct.ty))
873+
print_underscore!()
861874
}
862875
} else {
863-
p!(write("_: "), print(ct.ty))
876+
print_underscore!()
864877
}
865878
}
866879
}
867880
}
868881
}
869-
(ty::ConstKind::Infer(..), _) => p!(write("_: "), print(ct.ty)),
882+
(ty::ConstKind::Infer(..), _) => print_underscore!(),
870883
(ty::ConstKind::Param(ParamConst { name, .. }), _) => p!(write("{}", name)),
871-
(ty::ConstKind::Value(value), _) => return self.pretty_print_const_value(value, ct.ty),
884+
(ty::ConstKind::Value(value), _) => {
885+
return self.pretty_print_const_value(value, ct.ty, print_ty);
886+
}
872887

873888
_ => {
874889
// fallback
875-
p!(write("{:?} : ", ct.val), print(ct.ty))
890+
p!(write("{:?}", ct.val));
891+
if print_ty {
892+
p!(write(" : "), print(ct.ty));
893+
}
876894
}
877895
};
878896
Ok(self)
@@ -882,6 +900,7 @@ pub trait PrettyPrinter<'tcx>:
882900
mut self,
883901
ct: ConstValue<'tcx>,
884902
ty: Ty<'tcx>,
903+
print_ty: bool,
885904
) -> Result<Self::Const, Self::Error> {
886905
define_scoped_cx!(self);
887906

@@ -988,7 +1007,10 @@ pub trait PrettyPrinter<'tcx>:
9881007
};
9891008
if !printed {
9901009
// fallback
991-
p!(write("{:?} : ", ct), print(ty))
1010+
p!(write("{:?}", ct));
1011+
if print_ty {
1012+
p!(write(" : "), print(ty));
1013+
}
9921014
}
9931015
}
9941016
};
@@ -1162,7 +1184,7 @@ impl<F: fmt::Write> Printer<'tcx> for FmtPrinter<'_, 'tcx, F> {
11621184
}
11631185

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

11681190
fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {

src/librustc_codegen_utils/symbol_names/legacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl Printer<'tcx> for SymbolPrinter<'tcx> {
237237
// only print integers
238238
if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { .. })) = ct.val {
239239
if ct.ty.is_integral() {
240-
return self.pretty_print_const(ct);
240+
return self.pretty_print_const(ct, true);
241241
}
242242
}
243243
self.write_str("_")?;

src/librustc_mir/interpret/intrinsics/type_name.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
6969
}
7070
}
7171

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

7776
fn print_dyn_existential(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// run-pass
2+
3+
#![feature(const_generics)]
4+
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
5+
6+
#[derive(Debug)]
7+
struct S<const N: usize>;
8+
9+
fn main() {
10+
assert_eq!(std::any::type_name::<S<3>>(), "const_generic_type_name::S<3usize>");
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
2+
--> $DIR/const-generic-type_name.rs:3:12
3+
|
4+
LL | #![feature(const_generics)]
5+
| ^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(incomplete_features)]` on by default
8+

0 commit comments

Comments
 (0)