提交 a2911534 编写于 作者: R Ralf Jung

Permit ptr->int->ptr roundtrip

上级 2a231d66
......@@ -662,7 +662,6 @@ pub(super) fn eval_rvalue_into_lvalue(
let src = self.eval_operand(operand)?;
let src_ty = self.operand_ty(operand);
if self.type_is_fat_ptr(src_ty) {
trace!("misc cast: {:?}", src);
match (src, self.type_is_fat_ptr(dest_ty)) {
(Value::ByRef(_), _) |
(Value::ByValPair(..), true) => {
......@@ -674,9 +673,19 @@ pub(super) fn eval_rvalue_into_lvalue(
(Value::ByVal(_), _) => bug!("expected fat ptr"),
}
} else {
let src_val = self.value_to_primval(src, src_ty)?;
let dest_val = self.cast_primval(src_val, src_ty, dest_ty)?;
self.write_value(Value::ByVal(dest_val), dest, dest_ty)?;
// First, try casting
let dest_val = self.value_to_primval(src, src_ty).and_then(
|src_val| { self.cast_primval(src_val, src_ty, dest_ty) })
// Alternatively, if the sizes are equal, try just reading at the target type
.or_else(|err| {
let size = self.type_size(src_ty)?;
if size.is_some() && size == self.type_size(dest_ty)? {
self.value_to_primval(src, dest_ty)
} else {
Err(err)
}
});
self.write_value(Value::ByVal(dest_val?), dest, dest_ty)?;
}
}
......
// fn eq_ref<T>(x: &T, y: &T) -> bool {
// x as *const _ == y as *const _
// }
fn eq_ref<T>(x: &T, y: &T) -> bool {
x as *const _ == y as *const _
}
fn main() {
// int-ptr-int
assert_eq!(1 as *const i32 as usize, 1);
// TODO
// { // ptr-int-ptr
// let x = 13;
// let y = &x as *const _ as usize;
// let y = y as *const _;
// assert!(eq_ref(&x, unsafe { &*y }));
// }
{ // ptr-int-ptr
let x = 13;
let y = &x as *const _ as usize;
let y = y as *const _;
assert!(eq_ref(&x, unsafe { &*y }));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册