Skip to content

Commit c83241a

Browse files
committed
avoid an unnecessary call to Pointer::into_parts, and caution against into_pointer_or_addr
1 parent 3236092 commit c83241a

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
158158
&self,
159159
ptr: Pointer<AllocId>,
160160
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
161-
// We know `offset` is relative to the allocation, so we can use `into_parts`.
162-
let (alloc_id, offset) = ptr.into_parts();
161+
let alloc_id = ptr.provenance;
163162
// We need to handle `extern static`.
164163
match self.tcx.get_global_alloc(alloc_id) {
165164
Some(GlobalAlloc::Static(def_id)) if self.tcx.is_thread_local_static(def_id) => {
@@ -171,7 +170,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
171170
_ => {}
172171
}
173172
// And we need to get the tag.
174-
Ok(M::tag_alloc_base_pointer(self, Pointer::new(alloc_id, offset)))
173+
Ok(M::tag_alloc_base_pointer(self, ptr))
175174
}
176175

177176
pub fn create_fn_alloc_ptr(

compiler/rustc_middle/src/mir/interpret/pointer.rs

+4
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ impl<Tag> From<Pointer<Tag>> for Pointer<Option<Tag>> {
201201
}
202202

203203
impl<Tag> Pointer<Option<Tag>> {
204+
/// Convert this pointer that *might* have a tag into a pointer that *definitely* has a tag, or
205+
/// an absolute address.
206+
///
207+
/// This is rarely what you want; call `ptr_try_get_alloc_id` instead.
204208
pub fn into_pointer_or_addr(self) -> Result<Pointer<Tag>, Size> {
205209
match self.provenance {
206210
Some(tag) => Ok(Pointer::new(tag, self.offset)),

0 commit comments

Comments
 (0)