Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f4b61ba

Browse files
committed
adjustions and cleanup to make Miri build again
1 parent 8932aeb commit f4b61ba

File tree

13 files changed

+134
-201
lines changed

13 files changed

+134
-201
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<Tag> From<Pointer<Tag>> for Pointer<Option<Tag>> {
171171
}
172172

173173
impl<Tag> Pointer<Option<Tag>> {
174-
pub fn into_pointer_or_offset(self) -> Result<Pointer<Tag>, Size> {
174+
pub fn into_pointer_or_addr(self) -> Result<Pointer<Tag>, Size> {
175175
match self.provenance {
176176
Some(tag) => Ok(Pointer::new(tag, self.offset)),
177177
None => Err(self.offset),
@@ -187,6 +187,13 @@ impl<Tag> Pointer<Option<Tag>> {
187187
}
188188
}
189189

190+
impl<Tag> Pointer<Option<Tag>> {
191+
#[inline(always)]
192+
pub fn null() -> Self {
193+
Pointer { provenance: None, offset: Size::ZERO }
194+
}
195+
}
196+
190197
impl<'tcx, Tag> Pointer<Tag> {
191198
#[inline(always)]
192199
pub fn new(provenance: Tag, offset: Size) -> Self {
@@ -206,9 +213,14 @@ impl<'tcx, Tag> Pointer<Tag> {
206213
where
207214
Tag: Provenance,
208215
{
216+
// FIXME: This is wrong! `self.offset` might be an absolute address.
209217
Pointer { offset: self.offset, provenance: self.provenance.erase_for_fmt() }
210218
}
211219

220+
pub fn map_provenance(self, f: impl FnOnce(Tag) -> Tag) -> Self {
221+
Pointer { provenance: f(self.provenance), ..self }
222+
}
223+
212224
#[inline]
213225
pub fn offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
214226
Ok(Pointer {

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

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_apfloat::{
66
Float,
77
};
88
use rustc_macros::HashStable;
9-
use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
9+
use rustc_target::abi::{HasDataLayout, Size};
1010

1111
use crate::ty::{Lift, ParamEnv, ScalarInt, Ty, TyCtxt};
1212

@@ -179,7 +179,7 @@ impl<Tag> From<ScalarInt> for Scalar<Tag> {
179179
}
180180
}
181181

182-
impl<'tcx, Tag> Scalar<Tag> {
182+
impl<Tag> Scalar<Tag> {
183183
pub const ZST: Self = Scalar::Int(ScalarInt::ZST);
184184

185185
#[inline(always)]
@@ -202,56 +202,6 @@ impl<'tcx, Tag> Scalar<Tag> {
202202
Scalar::Int(ScalarInt::null(cx.pointer_size()))
203203
}
204204

205-
#[inline(always)]
206-
fn ptr_op(
207-
self,
208-
dl: &TargetDataLayout,
209-
f_int: impl FnOnce(u64) -> InterpResult<'tcx, u64>,
210-
f_ptr: impl FnOnce(Pointer<Tag>) -> InterpResult<'tcx, Pointer<Tag>>,
211-
) -> InterpResult<'tcx, Self> {
212-
match self {
213-
Scalar::Int(int) => Ok(Scalar::Int(int.ptr_sized_op(dl, f_int)?)),
214-
Scalar::Ptr(ptr, sz) => {
215-
debug_assert_eq!(u64::from(sz), dl.pointer_size().bytes());
216-
Ok(Scalar::Ptr(f_ptr(ptr)?, sz))
217-
}
218-
}
219-
}
220-
221-
#[inline]
222-
pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
223-
let dl = cx.data_layout();
224-
self.ptr_op(dl, |int| dl.offset(int, i.bytes()), |ptr| ptr.offset(i, dl))
225-
}
226-
227-
#[inline]
228-
pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
229-
let dl = cx.data_layout();
230-
self.ptr_op(
231-
dl,
232-
|int| Ok(dl.overflowing_offset(int, i.bytes()).0),
233-
|ptr| Ok(ptr.wrapping_offset(i, dl)),
234-
)
235-
.unwrap()
236-
}
237-
238-
#[inline]
239-
pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
240-
let dl = cx.data_layout();
241-
self.ptr_op(dl, |int| dl.signed_offset(int, i), |ptr| ptr.signed_offset(i, dl))
242-
}
243-
244-
#[inline]
245-
pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
246-
let dl = cx.data_layout();
247-
self.ptr_op(
248-
dl,
249-
|int| Ok(dl.overflowing_signed_offset(int, i).0),
250-
|ptr| Ok(ptr.wrapping_signed_offset(i, dl)),
251-
)
252-
.unwrap()
253-
}
254-
255205
#[inline]
256206
pub fn from_bool(b: bool) -> Self {
257207
Scalar::Int(b.into())

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_apfloat::ieee::{Double, Single};
22
use rustc_apfloat::Float;
33
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
4-
use rustc_target::abi::{Size, TargetDataLayout};
4+
use rustc_target::abi::Size;
55
use std::convert::{TryFrom, TryInto};
66
use std::fmt;
77

@@ -193,15 +193,6 @@ impl ScalarInt {
193193
self.data == 0
194194
}
195195

196-
pub(crate) fn ptr_sized_op<E>(
197-
self,
198-
dl: &TargetDataLayout,
199-
f_int: impl FnOnce(u64) -> Result<u64, E>,
200-
) -> Result<Self, E> {
201-
assert_eq!(u64::from(self.size), dl.pointer_size.bytes());
202-
Ok(Self::try_from_uint(f_int(u64::try_from(self.data).unwrap())?, self.size()).unwrap())
203-
}
204-
205196
#[inline]
206197
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
207198
let data = i.into();

compiler/rustc_mir/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
312312
align,
313313
interpret::MemoryKind::Machine(MemoryKind::Heap),
314314
)?;
315-
ecx.write_scalar(Scalar::from_pointer(ptr, &*ecx.tcx), dest)?;
315+
ecx.write_pointer(ptr, dest)?;
316316
}
317317
_ => {
318318
return Err(ConstEvalErrKind::NeedsRfc(format!(

compiler/rustc_mir/src/const_eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn const_caller_location(
3535
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
3636
bug!("intern_const_alloc_recursive should not error in this case")
3737
}
38-
ConstValue::Scalar(Scalar::from_pointer(loc_place.ptr.into_pointer_or_offset().unwrap(), &tcx))
38+
ConstValue::Scalar(Scalar::from_pointer(loc_place.ptr.into_pointer_or_addr().unwrap(), &tcx))
3939
}
4040

4141
/// Convert an evaluated constant to a type level constant

compiler/rustc_mir/src/interpret/cast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5757
.ok_or_else(|| err_inval!(TooGeneric))?;
5858

5959
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
60-
self.write_scalar(Scalar::from_pointer(fn_ptr, &*self.tcx), dest)?;
60+
self.write_pointer(fn_ptr, dest)?;
6161
}
6262
_ => span_bug!(self.cur_span(), "reify fn pointer on {:?}", src.layout.ty),
6363
}
@@ -88,7 +88,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8888
ty::ClosureKind::FnOnce,
8989
);
9090
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
91-
self.write_scalar(Scalar::from_pointer(fn_ptr, &*self.tcx), dest)?;
91+
self.write_pointer(fn_ptr, dest)?;
9292
}
9393
_ => span_bug!(self.cur_span(), "closure fn pointer on {:?}", src.layout.ty),
9494
}

compiler/rustc_mir/src/interpret/intern.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::ty::{self, layout::TyAndLayout, Ty};
2323

2424
use rustc_ast::Mutability;
2525

26-
use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, ValueVisitor};
26+
use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy, ValueVisitor};
2727
use crate::const_eval;
2828

2929
pub trait CompileTimeMachine<'mir, 'tcx, T> = Machine<
@@ -425,11 +425,11 @@ impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>>
425425
layout: TyAndLayout<'tcx>,
426426
f: impl FnOnce(
427427
&mut InterpCx<'mir, 'tcx, M>,
428-
&MPlaceTy<'tcx, M::PointerTag>,
428+
&PlaceTy<'tcx, M::PointerTag>,
429429
) -> InterpResult<'tcx, ()>,
430430
) -> InterpResult<'tcx, &'tcx Allocation> {
431431
let dest = self.allocate(layout, MemoryKind::Stack)?;
432-
f(self, &dest)?;
432+
f(self, &dest.into())?;
433433
let mut alloc = self.memory.alloc_map.remove(&dest.ptr.provenance.unwrap()).unwrap().1;
434434
alloc.mutability = Mutability::Not;
435435
Ok(self.tcx.intern_const_alloc(alloc))

compiler/rustc_mir/src/interpret/intrinsics.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,17 +337,17 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
337337
let pointee_ty = substs.type_at(0);
338338

339339
let offset_ptr = self.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?;
340-
self.write_scalar(Scalar::from_maybe_pointer(offset_ptr, self), dest)?;
340+
self.write_pointer(offset_ptr, dest)?;
341341
}
342342
sym::arith_offset => {
343-
let ptr = self.read_scalar(&args[0])?.check_init()?;
343+
let ptr = self.read_pointer(&args[0])?;
344344
let offset_count = self.read_scalar(&args[1])?.to_machine_isize(self)?;
345345
let pointee_ty = substs.type_at(0);
346346

347347
let pointee_size = i64::try_from(self.layout_of(pointee_ty)?.size.bytes()).unwrap();
348348
let offset_bytes = offset_count.wrapping_mul(pointee_size);
349-
let offset_ptr = ptr.ptr_wrapping_signed_offset(offset_bytes, self);
350-
self.write_scalar(offset_ptr, dest)?;
349+
let offset_ptr = ptr.wrapping_signed_offset(offset_bytes, self);
350+
self.write_pointer(offset_ptr, dest)?;
351351
}
352352
sym::ptr_offset_from => {
353353
let a = self.read_immediate(&args[0])?.to_scalar()?;
@@ -379,8 +379,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
379379
// General case: we need two pointers.
380380
let a = self.scalar_to_ptr(a);
381381
let b = self.scalar_to_ptr(b);
382-
let (a_alloc_id, a_offset, _) = self.memory.ptr_force_alloc(a)?;
383-
let (b_alloc_id, b_offset, _) = self.memory.ptr_force_alloc(b)?;
382+
let (a_alloc_id, a_offset, _) = self.memory.ptr_get_alloc(a)?;
383+
let (b_alloc_id, b_offset, _) = self.memory.ptr_get_alloc(b)?;
384384
if a_alloc_id != b_alloc_id {
385385
throw_ub_format!(
386386
"ptr_offset_from cannot compute offset of pointers into different \

0 commit comments

Comments
 (0)