Skip to content

Commit 8713a7b

Browse files
committed
fix casts from fat pointers
1 parent 8debe64 commit 8713a7b

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/librustc_mir/interpret/cast.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,13 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
111111
)
112112
}
113113

114-
// Handle cast the metadata away from a fat pointer.
115-
if dest_layout.size != src.layout.size {
114+
// Handle casting the metadata away from a fat pointer.
115+
if src.layout.ty.is_unsafe_ptr() && dest_layout.ty.is_unsafe_ptr() &&
116+
dest_layout.size != src.layout.size
117+
{
118+
assert_eq!(src.layout.size, 2*self.memory.pointer_size());
116119
assert_eq!(dest_layout.size, self.memory.pointer_size());
120+
assert!(dest_layout.ty.is_unsafe_ptr());
117121
return match *src {
118122
Immediate::ScalarPair(data, _) => Ok(data.into()),
119123
Immediate::Scalar(..) =>
@@ -124,6 +128,15 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
124128
};
125129
}
126130

131+
// Handle casting reference to raw ptr or raw to other raw (might be a fat ptr).
132+
if (src.layout.ty.is_region_ptr() || src.layout.ty.is_unsafe_ptr()) &&
133+
dest_layout.ty.is_unsafe_ptr()
134+
{
135+
// The only possible size-unequal case was handled above.
136+
assert_eq!(src.layout.size, dest_layout.size);
137+
return Ok(*src);
138+
}
139+
127140
// Handle cast from a univariant (ZST) enum
128141
match src.layout.variants {
129142
layout::Variants::Single { index } => {

0 commit comments

Comments
 (0)