Skip to content

Commit a7b312f

Browse files
committed
erase the tag on casts involving (raw) pointers
1 parent e4d03f8 commit a7b312f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/librustc_mir/interpret/cast.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,34 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
4444
}
4545

4646
Misc => {
47+
let src_layout = src.layout;
4748
let src = self.read_immediate(src)?;
4849

49-
if self.type_is_fat_ptr(src.layout.ty) {
50-
match (*src, self.type_is_fat_ptr(dest.layout.ty)) {
50+
// There are no casts to references
51+
assert!(!dest.layout.ty.is_region_ptr());
52+
// Hence we make all casts erase the tag
53+
let src = src.erase_tag().with_default_tag();
54+
55+
if self.type_is_fat_ptr(src_layout.ty) {
56+
match (src, self.type_is_fat_ptr(dest.layout.ty)) {
5157
// pointers to extern types
5258
(Immediate::Scalar(_),_) |
5359
// slices and trait objects to other slices/trait objects
5460
(Immediate::ScalarPair(..), true) => {
5561
// No change to immediate
56-
self.write_immediate(*src, dest)?;
62+
self.write_immediate(src, dest)?;
5763
}
5864
// slices and trait objects to thin pointers (dropping the metadata)
5965
(Immediate::ScalarPair(data, _), false) => {
6066
self.write_scalar(data, dest)?;
6167
}
6268
}
6369
} else {
64-
match src.layout.variants {
70+
match src_layout.variants {
6571
layout::Variants::Single { index } => {
66-
if let Some(def) = src.layout.ty.ty_adt_def() {
72+
if let Some(def) = src_layout.ty.ty_adt_def() {
6773
// Cast from a univariant enum
68-
assert!(src.layout.is_zst());
74+
assert!(src_layout.is_zst());
6975
let discr_val = def
7076
.discriminant_for_variant(*self.tcx, index)
7177
.val;
@@ -78,7 +84,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
7884
layout::Variants::NicheFilling { .. } => {},
7985
}
8086

81-
let dest_val = self.cast_scalar(src.to_scalar()?, src.layout, dest.layout)?;
87+
let dest_val = self.cast_scalar(src.to_scalar()?, src_layout, dest.layout)?;
8288
self.write_scalar(dest_val, dest)?;
8389
}
8490
}

0 commit comments

Comments
 (0)