Skip to content

Commit

Permalink
avoid an unnecessary call to Pointer::into_parts, and caution against…
Browse files Browse the repository at this point in the history
… into_pointer_or_addr
  • Loading branch information
RalfJung committed Apr 18, 2022
1 parent 3236092 commit c83241a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 2 additions & 3 deletions compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
&self,
ptr: Pointer<AllocId>,
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
// We know `offset` is relative to the allocation, so we can use `into_parts`.
let (alloc_id, offset) = ptr.into_parts();
let alloc_id = ptr.provenance;
// We need to handle `extern static`.
match self.tcx.get_global_alloc(alloc_id) {
Some(GlobalAlloc::Static(def_id)) if self.tcx.is_thread_local_static(def_id) => {
Expand All @@ -171,7 +170,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
_ => {}
}
// And we need to get the tag.
Ok(M::tag_alloc_base_pointer(self, Pointer::new(alloc_id, offset)))
Ok(M::tag_alloc_base_pointer(self, ptr))
}

pub fn create_fn_alloc_ptr(
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ impl<Tag> From<Pointer<Tag>> for Pointer<Option<Tag>> {
}

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

0 comments on commit c83241a

Please sign in to comment.