From c83241a7f9ca77acfe0264b4d42a0f915b0a5a34 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 18 Apr 2022 11:48:09 -0400 Subject: [PATCH] avoid an unnecessary call to Pointer::into_parts, and caution against into_pointer_or_addr --- compiler/rustc_const_eval/src/interpret/memory.rs | 5 ++--- compiler/rustc_middle/src/mir/interpret/pointer.rs | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index d19a9d7056012..f08bf801633f9 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -158,8 +158,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { &self, ptr: Pointer, ) -> InterpResult<'tcx, Pointer> { - // 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) => { @@ -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( diff --git a/compiler/rustc_middle/src/mir/interpret/pointer.rs b/compiler/rustc_middle/src/mir/interpret/pointer.rs index 4461f4e056873..c71aea417eca0 100644 --- a/compiler/rustc_middle/src/mir/interpret/pointer.rs +++ b/compiler/rustc_middle/src/mir/interpret/pointer.rs @@ -201,6 +201,10 @@ impl From> for Pointer> { } impl Pointer> { + /// 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, Size> { match self.provenance { Some(tag) => Ok(Pointer::new(tag, self.offset)),