Skip to content

Commit 0bb367e

Browse files
committed
Split pretty printer logic for scalar int and scalar ptr
Value trees won't have scalar ptr at all, so we need a scalar int printing method anyway. This way we'll be able to share that method between all const representations.
1 parent b729cc9 commit 0bb367e

File tree

1 file changed

+67
-46
lines changed

1 file changed

+67
-46
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 67 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -956,32 +956,40 @@ pub trait PrettyPrinter<'tcx>:
956956
}
957957

958958
fn pretty_print_const_scalar(
959-
mut self,
959+
self,
960960
scalar: Scalar,
961961
ty: Ty<'tcx>,
962962
print_ty: bool,
963+
) -> Result<Self::Const, Self::Error> {
964+
match scalar {
965+
Scalar::Ptr(ptr) => self.pretty_print_const_scalar_ptr(ptr, ty, print_ty),
966+
Scalar::Int(int) => self.pretty_print_const_scalar_int(int, ty, print_ty),
967+
}
968+
}
969+
970+
fn pretty_print_const_scalar_ptr(
971+
mut self,
972+
ptr: Pointer,
973+
ty: Ty<'tcx>,
974+
print_ty: bool,
963975
) -> Result<Self::Const, Self::Error> {
964976
define_scoped_cx!(self);
965977

966-
match (scalar, &ty.kind()) {
978+
match ty.kind() {
967979
// Byte strings (&[u8; N])
968-
(
969-
Scalar::Ptr(ptr),
970-
ty::Ref(
971-
_,
972-
ty::TyS {
973-
kind:
974-
ty::Array(
975-
ty::TyS { kind: ty::Uint(ty::UintTy::U8), .. },
976-
ty::Const {
977-
val: ty::ConstKind::Value(ConstValue::Scalar(int)),
978-
..
979-
},
980-
),
981-
..
982-
},
983-
_,
984-
),
980+
ty::Ref(
981+
_,
982+
ty::TyS {
983+
kind:
984+
ty::Array(
985+
ty::TyS { kind: ty::Uint(ty::UintTy::U8), .. },
986+
ty::Const {
987+
val: ty::ConstKind::Value(ConstValue::Scalar(int)), ..
988+
},
989+
),
990+
..
991+
},
992+
_,
985993
) => match self.tcx().get_global_alloc(ptr.alloc_id) {
986994
Some(GlobalAlloc::Memory(alloc)) => {
987995
let bytes = int.assert_bits(self.tcx().data_layout.pointer_size);
@@ -997,28 +1005,59 @@ pub trait PrettyPrinter<'tcx>:
9971005
Some(GlobalAlloc::Function(_)) => p!("<function>"),
9981006
None => p!("<dangling pointer>"),
9991007
},
1008+
ty::FnPtr(_) => {
1009+
// FIXME: We should probably have a helper method to share code with the "Byte strings"
1010+
// printing above (which also has to handle pointers to all sorts of things).
1011+
match self.tcx().get_global_alloc(ptr.alloc_id) {
1012+
Some(GlobalAlloc::Function(instance)) => {
1013+
self = self.typed_value(
1014+
|this| this.print_value_path(instance.def_id(), instance.substs),
1015+
|this| this.print_type(ty),
1016+
" as ",
1017+
)?;
1018+
}
1019+
_ => self = self.pretty_print_const_pointer(ptr, ty, print_ty)?,
1020+
}
1021+
}
1022+
// Any pointer values not covered by a branch above
1023+
_ => {
1024+
self = self.pretty_print_const_pointer(ptr, ty, print_ty)?;
1025+
}
1026+
}
1027+
Ok(self)
1028+
}
1029+
1030+
fn pretty_print_const_scalar_int(
1031+
mut self,
1032+
int: ScalarInt,
1033+
ty: Ty<'tcx>,
1034+
print_ty: bool,
1035+
) -> Result<Self::Const, Self::Error> {
1036+
define_scoped_cx!(self);
1037+
1038+
match ty.kind() {
10001039
// Bool
1001-
(Scalar::Int(int), ty::Bool) if int == ScalarInt::FALSE => p!("false"),
1002-
(Scalar::Int(int), ty::Bool) if int == ScalarInt::TRUE => p!("true"),
1040+
ty::Bool if int == ScalarInt::FALSE => p!("false"),
1041+
ty::Bool if int == ScalarInt::TRUE => p!("true"),
10031042
// Float
1004-
(Scalar::Int(int), ty::Float(ty::FloatTy::F32)) => {
1043+
ty::Float(ty::FloatTy::F32) => {
10051044
p!(write("{}f32", Single::try_from(int).unwrap()))
10061045
}
1007-
(Scalar::Int(int), ty::Float(ty::FloatTy::F64)) => {
1046+
ty::Float(ty::FloatTy::F64) => {
10081047
p!(write("{}f64", Double::try_from(int).unwrap()))
10091048
}
10101049
// Int
1011-
(Scalar::Int(int), ty::Uint(_) | ty::Int(_)) => {
1050+
ty::Uint(_) | ty::Int(_) => {
10121051
let int =
10131052
ConstInt::new(int, matches!(ty.kind(), ty::Int(_)), ty.is_ptr_sized_integral());
10141053
if print_ty { p!(write("{:#?}", int)) } else { p!(write("{:?}", int)) }
10151054
}
10161055
// Char
1017-
(Scalar::Int(int), ty::Char) if char::try_from(int).is_ok() => {
1056+
ty::Char if char::try_from(int).is_ok() => {
10181057
p!(write("{:?}", char::try_from(int).unwrap()))
10191058
}
10201059
// Raw pointers
1021-
(Scalar::Int(int), ty::RawPtr(_) | ty::FnPtr(_)) => {
1060+
ty::RawPtr(_) | ty::FnPtr(_) => {
10221061
let data = int.assert_bits(self.tcx().data_layout.pointer_size);
10231062
self = self.typed_value(
10241063
|mut this| {
@@ -1029,26 +1068,12 @@ pub trait PrettyPrinter<'tcx>:
10291068
" as ",
10301069
)?;
10311070
}
1032-
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
1033-
// FIXME: We should probably have a helper method to share code with the "Byte strings"
1034-
// printing above (which also has to handle pointers to all sorts of things).
1035-
match self.tcx().get_global_alloc(ptr.alloc_id) {
1036-
Some(GlobalAlloc::Function(instance)) => {
1037-
self = self.typed_value(
1038-
|this| this.print_value_path(instance.def_id(), instance.substs),
1039-
|this| this.print_type(ty),
1040-
" as ",
1041-
)?;
1042-
}
1043-
_ => self = self.pretty_print_const_pointer(ptr, ty, print_ty)?,
1044-
}
1045-
}
10461071
// For function type zsts just printing the path is enough
1047-
(Scalar::Int(int), ty::FnDef(d, s)) if int == ScalarInt::ZST => {
1072+
ty::FnDef(d, s) if int == ScalarInt::ZST => {
10481073
p!(print_value_path(*d, s))
10491074
}
10501075
// Nontrivial types with scalar bit representation
1051-
(Scalar::Int(int), _) => {
1076+
_ => {
10521077
let print = |mut this: Self| {
10531078
if int.size() == Size::ZERO {
10541079
write!(this, "transmute(())")?;
@@ -1063,10 +1088,6 @@ pub trait PrettyPrinter<'tcx>:
10631088
print(self)?
10641089
};
10651090
}
1066-
// Any pointer values not covered by a branch above
1067-
(Scalar::Ptr(p), _) => {
1068-
self = self.pretty_print_const_pointer(p, ty, print_ty)?;
1069-
}
10701091
}
10711092
Ok(self)
10721093
}

0 commit comments

Comments
 (0)