Skip to content

Commit ee33e02

Browse files
committed
Refactor pretty_print_const.
1 parent e5fb784 commit ee33e02

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/librustc/ty/print/pretty.rs

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,6 @@ pub trait PrettyPrinter<'tcx>:
861861
return Ok(self);
862862
}
863863

864-
let u8 = self.tcx().types.u8;
865-
866864
match (ct.val, &ct.ty.kind) {
867865
(_, ty::FnDef(did, substs)) => p!(print_value_path(*did, substs)),
868866
(ty::ConstKind::Unevaluated(did, substs), _) => {
@@ -884,13 +882,38 @@ pub trait PrettyPrinter<'tcx>:
884882
},
885883
(ty::ConstKind::Infer(..), _) => p!(write("_: "), print(ct.ty)),
886884
(ty::ConstKind::Param(ParamConst { name, .. }), _) => p!(write("{}", name)),
887-
(ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Bool) =>
885+
(ty::ConstKind::Value(value), _) => return self.pretty_print_const_value(value, ct.ty),
886+
887+
_ => {
888+
// fallback
889+
p!(write("{:?} : ", ct.val), print(ct.ty))
890+
}
891+
};
892+
Ok(self)
893+
}
894+
895+
fn pretty_print_const_value(
896+
mut self,
897+
ct: ConstValue<'tcx>,
898+
ty: Ty<'tcx>,
899+
) -> Result<Self::Const, Self::Error> {
900+
define_scoped_cx!(self);
901+
902+
if self.tcx().sess.verbose() {
903+
p!(write("ConstValue({:?}: {:?})", ct, ty));
904+
return Ok(self);
905+
}
906+
907+
let u8 = self.tcx().types.u8;
908+
909+
match (ct, &ty.kind) {
910+
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Bool) =>
888911
p!(write("{}", if data == 0 { "false" } else { "true" })),
889-
(ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Float(ast::FloatTy::F32)) =>
912+
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Float(ast::FloatTy::F32)) =>
890913
p!(write("{}f32", Single::from_bits(data))),
891-
(ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Float(ast::FloatTy::F64)) =>
914+
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Float(ast::FloatTy::F64)) =>
892915
p!(write("{}f64", Double::from_bits(data))),
893-
(ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Uint(ui)) => {
916+
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Uint(ui)) => {
894917
let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(*ui)).size();
895918
let max = truncate(u128::max_value(), bit_size);
896919

@@ -901,13 +924,13 @@ pub trait PrettyPrinter<'tcx>:
901924
p!(write("{}{}", data, ui_str))
902925
};
903926
},
904-
(ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Int(i)) => {
927+
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Int(i)) => {
905928
let bit_size = Integer::from_attr(&self.tcx(), SignedInt(*i))
906929
.size().bits() as u128;
907930
let min = 1u128 << (bit_size - 1);
908931
let max = min - 1;
909932

910-
let ty = self.tcx().lift(&ct.ty).unwrap();
933+
let ty = self.tcx().lift(&ty).unwrap();
911934
let size = self.tcx().layout_of(ty::ParamEnv::empty().and(ty))
912935
.unwrap()
913936
.size;
@@ -918,27 +941,27 @@ pub trait PrettyPrinter<'tcx>:
918941
_ => p!(write("{}{}", sign_extend(data, size) as i128, i_str))
919942
}
920943
},
921-
(ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })), ty::Char) =>
944+
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Char) =>
922945
p!(write("{:?}", ::std::char::from_u32(data as u32).unwrap())),
923-
(ty::ConstKind::Value(ConstValue::Scalar(_)), ty::RawPtr(_)) => p!(write("{{pointer}}")),
924-
(ty::ConstKind::Value(ConstValue::Scalar(Scalar::Ptr(ptr))), ty::FnPtr(_)) => {
946+
(ConstValue::Scalar(_), ty::RawPtr(_)) => p!(write("{{pointer}}")),
947+
(ConstValue::Scalar(Scalar::Ptr(ptr)), ty::FnPtr(_)) => {
925948
let instance = {
926949
let alloc_map = self.tcx().alloc_map.lock();
927950
alloc_map.unwrap_fn(ptr.alloc_id)
928951
};
929952
p!(print_value_path(instance.def_id(), instance.substs));
930953
},
931954
_ => {
932-
let printed = if let ty::Ref(_, ref_ty, _) = ct.ty.kind {
933-
let byte_str = match (ct.val, &ref_ty.kind) {
934-
(ty::ConstKind::Value(ConstValue::Scalar(Scalar::Ptr(ptr))), ty::Array(t, n)) if *t == u8 => {
955+
let printed = if let ty::Ref(_, ref_ty, _) = ty.kind {
956+
let byte_str = match (ct, &ref_ty.kind) {
957+
(ConstValue::Scalar(Scalar::Ptr(ptr)), ty::Array(t, n)) if *t == u8 => {
935958
let n = n.eval_usize(self.tcx(), ty::ParamEnv::empty());
936959
Some(self.tcx()
937960
.alloc_map.lock()
938961
.unwrap_memory(ptr.alloc_id)
939962
.get_bytes(&self.tcx(), ptr, Size::from_bytes(n)).unwrap())
940963
},
941-
(ty::ConstKind::Value(ConstValue::Slice { data, start, end }), ty::Slice(t)) if *t == u8 => {
964+
(ConstValue::Slice { data, start, end }, ty::Slice(t)) if *t == u8 => {
942965
// The `inspect` here is okay since we checked the bounds, and there are
943966
// no relocations (we have an active slice reference here). We don't use
944967
// this result to affect interpreter execution.
@@ -956,8 +979,8 @@ pub trait PrettyPrinter<'tcx>:
956979
}
957980
p!(write("\""));
958981
true
959-
} else if let (ty::ConstKind::Value(ConstValue::Slice { data, start, end }), ty::Str) =
960-
(ct.val, &ref_ty.kind)
982+
} else if let (ConstValue::Slice { data, start, end }, ty::Str) =
983+
(ct, &ref_ty.kind)
961984
{
962985
// The `inspect` here is okay since we checked the bounds, and there are no
963986
// relocations (we have an active `str` reference here). We don't use this
@@ -975,7 +998,7 @@ pub trait PrettyPrinter<'tcx>:
975998
};
976999
if !printed {
9771000
// fallback
978-
p!(write("{:?} : ", ct.val), print(ct.ty))
1001+
p!(write("{:?} : ", ct), print(ty))
9791002
}
9801003
}
9811004
};

0 commit comments

Comments
 (0)