Skip to content

Commit bc647e9

Browse files
committed
add 'raw reference' to the machine hook, and use that in ptr-to-raw casts
1 parent 24724ef commit bc647e9

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

src/librustc_mir/const_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
468468
_ptr: Pointer<Self::PointerTag>,
469469
_pointee_ty: Ty<'tcx>,
470470
_pointee_size: Size,
471-
_borrow_kind: mir::BorrowKind,
471+
_borrow_kind: Option<mir::BorrowKind>,
472472
) -> EvalResult<'tcx, Self::PointerTag> {
473473
Ok(())
474474
}

src/librustc_mir/interpret/cast.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
3838
dest: PlaceTy<'tcx, M::PointerTag>,
3939
) -> EvalResult<'tcx> {
4040
let src_layout = src.layout;
41-
let dst_layout = dest.layout;
4241
use rustc::mir::CastKind::*;
4342
match kind {
4443
Unsize => {
@@ -47,7 +46,19 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
4746

4847
Misc => {
4948
let src = self.read_value(src)?;
50-
if self.type_is_fat_ptr(src_layout.ty) {
49+
50+
if src.layout.ty.is_region_ptr() && dest.layout.ty.is_unsafe_ptr() {
51+
// For the purpose of the "ptr tag hooks", treat this as creating
52+
// a new, raw reference.
53+
let place = self.ref_to_mplace(src)?;
54+
let _val = self.create_ref(place, None)?;
55+
// FIXME: The blog post said we should now also erase the tag.
56+
// That would amount to using `_val` instead of `src` from here on.
57+
// However, do we really want to do that? `transmute` doesn't
58+
// do it either and we have to support that, somehow.
59+
}
60+
61+
if self.type_is_fat_ptr(src.layout.ty) {
5162
match (*src, self.type_is_fat_ptr(dest.layout.ty)) {
5263
// pointers to extern types
5364
(Value::Scalar(_),_) |
@@ -65,11 +76,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
6576
match src_layout.variants {
6677
layout::Variants::Single { index } => {
6778
if let Some(def) = src_layout.ty.ty_adt_def() {
79+
// Cast from a univariant enum
80+
assert!(src.layout.is_zst());
6881
let discr_val = def
6982
.discriminant_for_variant(*self.tcx, index)
7083
.val;
7184
return self.write_scalar(
72-
Scalar::from_uint(discr_val, dst_layout.size),
85+
Scalar::from_uint(discr_val, dest.layout.size),
7386
dest);
7487
}
7588
}
@@ -85,7 +98,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
8598

8699
ReifyFnPointer => {
87100
// The src operand does not matter, just its type
88-
match src_layout.ty.sty {
101+
match src.layout.ty.sty {
89102
ty::FnDef(def_id, substs) => {
90103
if self.tcx.has_attr(def_id, "rustc_args_required_const") {
91104
bug!("reifying a fn ptr that requires \
@@ -117,7 +130,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
117130

118131
ClosureFnPointer => {
119132
// The src operand does not matter, just its type
120-
match src_layout.ty.sty {
133+
match src.layout.ty.sty {
121134
ty::Closure(def_id, substs) => {
122135
let substs = self.tcx.subst_and_normalize_erasing_regions(
123136
self.substs(),

src/librustc_mir/interpret/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,13 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
201201

202202
/// Executed when evaluating the `&` operator: Creating a new reference.
203203
/// This has the chance to adjust the tag.
204+
/// `borrow_kind` can be `None` in case a raw ptr is being created.
204205
fn tag_reference(
205206
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
206207
ptr: Pointer<Self::PointerTag>,
207208
pointee_ty: Ty<'tcx>,
208209
pointee_size: Size,
209-
borrow_kind: mir::BorrowKind,
210+
borrow_kind: Option<mir::BorrowKind>,
210211
) -> EvalResult<'tcx, Self::PointerTag>;
211212

212213
/// Executed when evaluating the `*` operator: Following a reference.

src/librustc_mir/interpret/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ where
291291
pub fn create_ref(
292292
&mut self,
293293
place: MPlaceTy<'tcx, M::PointerTag>,
294-
borrow_kind: mir::BorrowKind,
294+
borrow_kind: Option<mir::BorrowKind>,
295295
) -> EvalResult<'tcx, Value<M::PointerTag>> {
296296
let ptr = match place.ptr {
297297
Scalar::Ptr(ptr) => {

src/librustc_mir/interpret/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
251251
Ref(_, borrow_kind, ref place) => {
252252
let src = self.eval_place(place)?;
253253
let val = self.force_allocation(src)?;
254-
let val = self.create_ref(val, borrow_kind)?;
254+
let val = self.create_ref(val, Some(borrow_kind))?;
255255
self.write_value(val, dest)?;
256256
}
257257

src/librustc_mir/interpret/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
448448
let arg = OpTy {
449449
op: Operand::Immediate(self.create_ref(
450450
place,
451-
mir::BorrowKind::Mut { allow_two_phase_borrow: false }
451+
None // this is a "raw reference"
452452
)?),
453453
layout: self.layout_of(self.tcx.mk_mut_ptr(place.layout.ty))?,
454454
};

0 commit comments

Comments
 (0)