Refactor pretty print const to use a big match statement
This commit is contained in:
parent
43badf9b1d
commit
9677cbe82e
3 changed files with 105 additions and 122 deletions
|
@ -863,137 +863,120 @@ pub trait PrettyPrinter<'tcx>:
|
|||
}
|
||||
|
||||
let u8 = self.tcx().types.u8;
|
||||
if let ty::FnDef(did, substs) = ct.ty.kind {
|
||||
p!(print_value_path(did, substs));
|
||||
return Ok(self);
|
||||
}
|
||||
if let ConstValue::Unevaluated(did, substs) = ct.val {
|
||||
match self.tcx().def_kind(did) {
|
||||
| Some(DefKind::Static)
|
||||
| Some(DefKind::Const)
|
||||
| Some(DefKind::AssocConst) => p!(print_value_path(did, substs)),
|
||||
_ => if did.is_local() {
|
||||
let span = self.tcx().def_span(did);
|
||||
if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span) {
|
||||
p!(write("{}", snip))
|
||||
|
||||
match (ct.val, &ct.ty.kind) {
|
||||
(_, ty::FnDef(did, substs)) => p!(print_value_path(*did, substs)),
|
||||
(ConstValue::Unevaluated(did, substs), _) => {
|
||||
match self.tcx().def_kind(did) {
|
||||
| Some(DefKind::Static)
|
||||
| Some(DefKind::Const)
|
||||
| Some(DefKind::AssocConst) => p!(print_value_path(did, substs)),
|
||||
_ => if did.is_local() {
|
||||
let span = self.tcx().def_span(did);
|
||||
if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span) {
|
||||
p!(write("{}", snip))
|
||||
} else {
|
||||
p!(write("_: "), print(ct.ty))
|
||||
}
|
||||
} else {
|
||||
p!(write("_: "), print(ct.ty))
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
(ConstValue::Infer(..), _) => p!(write("_: "), print(ct.ty)),
|
||||
(ConstValue::Param(ParamConst { name, .. }), _) => p!(write("{}", name)),
|
||||
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Bool) =>
|
||||
p!(write("{}", if data == 0 { "false" } else { "true" })),
|
||||
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Float(ast::FloatTy::F32)) =>
|
||||
p!(write("{}f32", Single::from_bits(data))),
|
||||
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Float(ast::FloatTy::F64)) =>
|
||||
p!(write("{}f64", Double::from_bits(data))),
|
||||
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Uint(ui)) => {
|
||||
let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(*ui)).size();
|
||||
let max = truncate(u128::max_value(), bit_size);
|
||||
|
||||
if data == max {
|
||||
p!(write("std::{}::MAX", ui))
|
||||
} else {
|
||||
p!(write("_: "), print(ct.ty))
|
||||
},
|
||||
}
|
||||
return Ok(self);
|
||||
}
|
||||
if let ConstValue::Infer(..) = ct.val {
|
||||
p!(write("_: "), print(ct.ty));
|
||||
return Ok(self);
|
||||
}
|
||||
if let ConstValue::Param(ParamConst { name, .. }) = ct.val {
|
||||
p!(write("{}", name));
|
||||
return Ok(self);
|
||||
}
|
||||
if let ConstValue::Scalar(Scalar::Raw { data, .. }) = ct.val {
|
||||
match ct.ty.kind {
|
||||
ty::Bool => {
|
||||
p!(write("{}", if data == 0 { "false" } else { "true" }));
|
||||
return Ok(self);
|
||||
},
|
||||
ty::Float(ast::FloatTy::F32) => {
|
||||
p!(write("{}f32", Single::from_bits(data)));
|
||||
return Ok(self);
|
||||
},
|
||||
ty::Float(ast::FloatTy::F64) => {
|
||||
p!(write("{}f64", Double::from_bits(data)));
|
||||
return Ok(self);
|
||||
},
|
||||
ty::Uint(ui) => {
|
||||
let bit_size = Integer::from_attr(&self.tcx(), UnsignedInt(ui)).size();
|
||||
let max = truncate(u128::max_value(), bit_size);
|
||||
p!(write("{}{}", data, ui))
|
||||
};
|
||||
},
|
||||
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Int(i)) => {
|
||||
let bit_size = Integer::from_attr(&self.tcx(), SignedInt(*i))
|
||||
.size().bits() as u128;
|
||||
let min = 1u128 << (bit_size - 1);
|
||||
let max = min - 1;
|
||||
|
||||
if data == max {
|
||||
p!(write("std::{}::MAX", ui))
|
||||
} else {
|
||||
p!(write("{}{}", data, ui))
|
||||
};
|
||||
return Ok(self);
|
||||
},
|
||||
ty::Int(i) =>{
|
||||
let bit_size = Integer::from_attr(&self.tcx(), SignedInt(i))
|
||||
.size().bits() as u128;
|
||||
let min = 1u128 << (bit_size - 1);
|
||||
let max = min - 1;
|
||||
|
||||
let ty = self.tcx().lift(&ct.ty).unwrap();
|
||||
let size = self.tcx().layout_of(ty::ParamEnv::empty().and(ty))
|
||||
.unwrap()
|
||||
.size;
|
||||
match data {
|
||||
d if d == min => p!(write("std::{}::MIN", i)),
|
||||
d if d == max => p!(write("std::{}::MAX", i)),
|
||||
_ => p!(write("{}{}", sign_extend(data, size) as i128, i))
|
||||
}
|
||||
return Ok(self);
|
||||
},
|
||||
ty::Char => {
|
||||
p!(write("{:?}", ::std::char::from_u32(data as u32).unwrap()));
|
||||
return Ok(self);
|
||||
let ty = self.tcx().lift(&ct.ty).unwrap();
|
||||
let size = self.tcx().layout_of(ty::ParamEnv::empty().and(ty))
|
||||
.unwrap()
|
||||
.size;
|
||||
match data {
|
||||
d if d == min => p!(write("std::{}::MIN", i)),
|
||||
d if d == max => p!(write("std::{}::MAX", i)),
|
||||
_ => p!(write("{}{}", sign_extend(data, size) as i128, i))
|
||||
}
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
if let ty::Ref(_, ref_ty, _) = ct.ty.kind {
|
||||
let byte_str = match (ct.val, &ref_ty.kind) {
|
||||
(ConstValue::Scalar(Scalar::Ptr(ptr)), ty::Array(t, n)) if *t == u8 => {
|
||||
let n = n.eval_usize(self.tcx(), ty::ParamEnv::empty());
|
||||
Some(self.tcx()
|
||||
.alloc_map.lock()
|
||||
.unwrap_memory(ptr.alloc_id)
|
||||
.get_bytes(&self.tcx(), ptr, Size::from_bytes(n)).unwrap())
|
||||
},
|
||||
(ConstValue::Slice { data, start, end }, ty::Slice(t)) if *t == u8 => {
|
||||
// The `inspect` here is okay since we checked the bounds, and there are no
|
||||
// relocations (we have an active slice reference here). We don't use this
|
||||
// result to affect interpreter execution.
|
||||
Some(data.inspect_with_undef_and_ptr_outside_interpreter(start..end))
|
||||
},
|
||||
(ConstValue::Slice { data, start, end }, ty::Str) => {
|
||||
// The `inspect` here is okay since we checked the bounds, and there are no
|
||||
// relocations (we have an active `str` reference here). We don't use this
|
||||
// result to affect interpreter execution.
|
||||
let slice = data.inspect_with_undef_and_ptr_outside_interpreter(start..end);
|
||||
let s = ::std::str::from_utf8(slice)
|
||||
.expect("non utf8 str from miri");
|
||||
p!(write("{:?}", s));
|
||||
return Ok(self);
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
if let Some(byte_str) = byte_str {
|
||||
p!(write("b\""));
|
||||
for &c in byte_str {
|
||||
for e in std::ascii::escape_default(c) {
|
||||
self.write_char(e as char)?;
|
||||
}
|
||||
}
|
||||
p!(write("\""));
|
||||
return Ok(self);
|
||||
}
|
||||
}
|
||||
|
||||
if let ty::FnPtr(_) = ct.ty.kind {
|
||||
if let ConstValue::Scalar(Scalar::Ptr(ptr)) = ct.val {
|
||||
},
|
||||
(ConstValue::Scalar(Scalar::Raw { data, .. }), ty::Char) =>
|
||||
p!(write("{:?}", ::std::char::from_u32(data as u32).unwrap())),
|
||||
(ConstValue::Scalar(Scalar::Ptr(ptr)), ty::FnPtr(_)) => {
|
||||
let instance = {
|
||||
let alloc_map = self.tcx().alloc_map.lock();
|
||||
alloc_map.unwrap_fn(ptr.alloc_id)
|
||||
};
|
||||
p!(print_value_path(instance.def_id(), instance.substs));
|
||||
return Ok(self);
|
||||
},
|
||||
_ => {
|
||||
let printed = if let ty::Ref(_, ref_ty, _) = ct.ty.kind {
|
||||
let byte_str = match (ct.val, &ref_ty.kind) {
|
||||
(ConstValue::Scalar(Scalar::Ptr(ptr)), ty::Array(t, n)) if *t == u8 => {
|
||||
let n = n.eval_usize(self.tcx(), ty::ParamEnv::empty());
|
||||
Some(self.tcx()
|
||||
.alloc_map.lock()
|
||||
.unwrap_memory(ptr.alloc_id)
|
||||
.get_bytes(&self.tcx(), ptr, Size::from_bytes(n)).unwrap())
|
||||
},
|
||||
(ConstValue::Slice { data, start, end }, ty::Slice(t)) if *t == u8 => {
|
||||
// The `inspect` here is okay since we checked the bounds, and there are
|
||||
// no relocations (we have an active slice reference here). We don't use
|
||||
// this result to affect interpreter execution.
|
||||
Some(data.inspect_with_undef_and_ptr_outside_interpreter(start..end))
|
||||
},
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(byte_str) = byte_str {
|
||||
p!(write("b\""));
|
||||
for &c in byte_str {
|
||||
for e in std::ascii::escape_default(c) {
|
||||
self.write_char(e as char)?;
|
||||
}
|
||||
}
|
||||
p!(write("\""));
|
||||
true
|
||||
} else if let (ConstValue::Slice { data, start, end }, ty::Str) =
|
||||
(ct.val, &ref_ty.kind)
|
||||
{
|
||||
// The `inspect` here is okay since we checked the bounds, and there are no
|
||||
// relocations (we have an active `str` reference here). We don't use this
|
||||
// result to affect interpreter execution.
|
||||
let slice = data.inspect_with_undef_and_ptr_outside_interpreter(start..end);
|
||||
let s = ::std::str::from_utf8(slice)
|
||||
.expect("non utf8 str from miri");
|
||||
p!(write("{:?}", s));
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if !printed {
|
||||
// fallback
|
||||
p!(write("{:?} : ", ct.val), print(ct.ty))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
p!(write("{:?} : ", ct.val), print(ct.ty));
|
||||
|
||||
};
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,4 +16,4 @@ impl<const P: *const u32> Const<{P}> {
|
|||
|
||||
fn main() {
|
||||
assert_eq!(Const::<{&A as *const _}>::get(), 3)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,4 +6,4 @@ struct Const<const P: *const u32>;
|
|||
fn main() {
|
||||
let _: Const<{15 as *const _}> = Const::<{10 as *const _}>; //~ mismatched types
|
||||
let _: Const<{10 as *const _}> = Const::<{10 as *const _}>;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue