Skip to content

Commit d61db93

Browse files
committed
also hook dereferencing
1 parent 5f20b16 commit d61db93

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

src/librustc_mir/const_eval.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,19 @@ use std::collections::hash_map::Entry;
1919
use rustc::hir::{self, def_id::DefId};
2020
use rustc::mir::interpret::ConstEvalErr;
2121
use rustc::mir;
22-
use rustc::ty::{self, TyCtxt, Instance, query::TyCtxtAt};
23-
use rustc::ty::layout::{self, LayoutOf, TyLayout};
22+
use rustc::ty::{self, Ty, TyCtxt, Instance, query::TyCtxtAt};
23+
use rustc::ty::layout::{self, Size, LayoutOf, TyLayout};
2424
use rustc::ty::subst::Subst;
2525
use rustc_data_structures::indexed_vec::IndexVec;
2626
use rustc_data_structures::fx::FxHashMap;
2727

2828
use syntax::ast::Mutability;
2929
use syntax::source_map::{Span, DUMMY_SP};
3030

31-
use rustc::mir::interpret::{
32-
EvalResult, EvalError, EvalErrorKind, GlobalId,
33-
Scalar, Allocation, AllocId, ConstValue,
34-
};
3531
use interpret::{self,
36-
PlaceTy, MPlaceTy, MemPlace, OpTy, Operand, Value,
37-
EvalContext, StackPopCleanup, MemoryKind,
32+
PlaceTy, MemPlace, OpTy, Operand, Value, Pointer, Scalar, ConstValue,
33+
EvalResult, EvalError, EvalErrorKind, GlobalId, EvalContext, StackPopCleanup,
34+
Allocation, AllocId, MemoryKind,
3835
snapshot,
3936
};
4037

@@ -468,11 +465,22 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
468465
#[inline(always)]
469466
fn tag_reference(
470467
_ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
471-
_place: MPlaceTy<'tcx, Self::PointerTag>,
468+
_ptr: Pointer<Self::PointerTag>,
469+
_pointee_ty: Ty<'tcx>,
470+
_pointee_size: Size,
472471
_borrow_kind: mir::BorrowKind,
473472
) -> EvalResult<'tcx, Self::PointerTag> {
474473
Ok(())
475474
}
475+
476+
#[inline(always)]
477+
fn tag_dereference(
478+
_ecx: &EvalContext<'a, 'mir, 'tcx, Self>,
479+
_ptr: Pointer<Self::PointerTag>,
480+
_ptr_ty: Ty<'tcx>,
481+
) -> EvalResult<'tcx, Self::PointerTag> {
482+
Ok(())
483+
}
476484
}
477485

478486
/// Project to a field of a (variant of a) const

src/librustc_mir/interpret/machine.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use std::hash::Hash;
1717

1818
use rustc::hir::def_id::DefId;
1919
use rustc::mir;
20-
use rustc::ty::{self, layout::{Size, TyLayout}, query::TyCtxtAt};
20+
use rustc::ty::{self, Ty, layout::{Size, TyLayout}, query::TyCtxtAt};
2121

2222
use super::{
2323
Allocation, AllocId, EvalResult, Scalar,
24-
EvalContext, PlaceTy, OpTy, MPlaceTy, Pointer, MemoryKind,
24+
EvalContext, PlaceTy, OpTy, Pointer, MemoryKind,
2525
};
2626

2727
/// Classifying memory accesses
@@ -199,14 +199,23 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
199199
}
200200

201201
/// Executed when evaluating the `&` operator: Creating a new reference.
202-
/// This has the chance to adjust the tag. It is only ever called if the
203-
/// pointer in `place` is really a pointer, not another scalar.
202+
/// This has the chance to adjust the tag.
204203
fn tag_reference(
205204
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
206-
place: MPlaceTy<'tcx, Self::PointerTag>,
205+
ptr: Pointer<Self::PointerTag>,
206+
pointee_ty: Ty<'tcx>,
207+
pointee_size: Size,
207208
borrow_kind: mir::BorrowKind,
208209
) -> EvalResult<'tcx, Self::PointerTag>;
209210

211+
/// Executed when evaluating the `*` operator: Following a reference.
212+
/// This has the change to adjust the tag.
213+
fn tag_dereference(
214+
ecx: &EvalContext<'a, 'mir, 'tcx, Self>,
215+
ptr: Pointer<Self::PointerTag>,
216+
ptr_ty: Ty<'tcx>,
217+
) -> EvalResult<'tcx, Self::PointerTag>;
218+
210219
/// Execute a validation operation
211220
#[inline]
212221
fn validation_op(

src/librustc_mir/interpret/place.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,24 @@ where
264264
&self,
265265
val: ValTy<'tcx, M::PointerTag>,
266266
) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
267+
let ptr = match val.to_scalar_ptr()? {
268+
Scalar::Ptr(ptr) => {
269+
// Machine might want to track the `*` operator
270+
let tag = M::tag_dereference(self, ptr, val.layout.ty)?;
271+
Scalar::Ptr(Pointer::new_with_tag(ptr.alloc_id, ptr.offset, tag))
272+
}
273+
scalar @ Scalar::Bits { .. } => scalar,
274+
};
275+
267276
let pointee_type = val.layout.ty.builtin_deref(true).unwrap().ty;
268277
let layout = self.layout_of(pointee_type)?;
269278
let align = layout.align;
279+
270280
let mplace = match *val {
271-
Value::Scalar(ptr) =>
272-
MemPlace { ptr: ptr.not_undef()?, align, meta: None },
273-
Value::ScalarPair(ptr, meta) =>
274-
MemPlace { ptr: ptr.not_undef()?, align, meta: Some(meta.not_undef()?) },
281+
Value::Scalar(_) =>
282+
MemPlace { ptr, align, meta: None },
283+
Value::ScalarPair(_, meta) =>
284+
MemPlace { ptr, align, meta: Some(meta.not_undef()?) },
275285
};
276286
Ok(MPlaceTy { mplace, layout })
277287
}
@@ -285,7 +295,10 @@ where
285295
) -> EvalResult<'tcx, Value<M::PointerTag>> {
286296
let ptr = match place.ptr {
287297
Scalar::Ptr(ptr) => {
288-
let tag = M::tag_reference(self, place, borrow_kind)?;
298+
// Machine might want to track the `&` operator
299+
let (size, _) = self.size_and_align_of_mplace(place)?
300+
.expect("create_ref cannot determine size");
301+
let tag = M::tag_reference(self, ptr, place.layout.ty, size, borrow_kind)?;
289302
Scalar::Ptr(Pointer::new_with_tag(ptr.alloc_id, ptr.offset, tag))
290303
},
291304
scalar @ Scalar::Bits { .. } => scalar,

0 commit comments

Comments
 (0)