Skip to content

Commit a22cf2a

Browse files
committed
Auto merge of rust-lang#95662 - Dylan-DPC:rollup-fo7jsr6, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - rust-lang#91873 (Mention implementers of unsatisfied trait) - rust-lang#95588 (explicitly distinguish pointer::addr and pointer::expose_addr) - rust-lang#95603 (Fix late-bound ICE in `dyn` return type suggestion) - rust-lang#95620 (interpret: remove MemoryExtra in favor of giving access to the Machine) - rust-lang#95630 (Update `NonNull` pointer provenance methods' documentation) - rust-lang#95631 (Refactor: remove unnecessary nested blocks) - rust-lang#95642 (`CandidateSource::XCandidate` -> `CandidateSource::X`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 60e50fc + b3c3eda commit a22cf2a

File tree

168 files changed

+1951
-695
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+1951
-695
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr, MemoryExtra};
1+
use super::{CompileTimeEvalContext, CompileTimeInterpreter, ConstEvalErr};
22
use crate::interpret::eval_nullary_intrinsic;
33
use crate::interpret::{
44
intern_const_alloc_recursive, Allocation, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId,
@@ -100,8 +100,7 @@ pub(super) fn mk_eval_cx<'mir, 'tcx>(
100100
tcx,
101101
root_span,
102102
param_env,
103-
CompileTimeInterpreter::new(tcx.const_eval_limit()),
104-
MemoryExtra { can_access_statics },
103+
CompileTimeInterpreter::new(tcx.const_eval_limit(), can_access_statics),
105104
)
106105
}
107106

@@ -285,10 +284,9 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
285284
tcx,
286285
tcx.def_span(def.did),
287286
key.param_env,
288-
CompileTimeInterpreter::new(tcx.const_eval_limit()),
289287
// Statics (and promoteds inside statics) may access other statics, because unlike consts
290288
// they do not have to behave "as if" they were evaluated at runtime.
291-
MemoryExtra { can_access_statics: is_static },
289+
CompileTimeInterpreter::new(tcx.const_eval_limit(), /*can_access_statics:*/ is_static),
292290
);
293291

294292
let res = ecx.load_mir(cid.instance.def, cid.promoted);

compiler/rustc_const_eval/src/const_eval/machine.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,7 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
9393

9494
/// The virtual call stack.
9595
pub(crate) stack: Vec<Frame<'mir, 'tcx, AllocId, ()>>,
96-
}
9796

98-
#[derive(Copy, Clone, Debug)]
99-
pub struct MemoryExtra {
10097
/// We need to make sure consts never point to anything mutable, even recursively. That is
10198
/// relied on for pattern matching on consts with references.
10299
/// To achieve this, two pieces have to work together:
@@ -107,8 +104,12 @@ pub struct MemoryExtra {
107104
}
108105

109106
impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {
110-
pub(super) fn new(const_eval_limit: Limit) -> Self {
111-
CompileTimeInterpreter { steps_remaining: const_eval_limit.0, stack: Vec::new() }
107+
pub(super) fn new(const_eval_limit: Limit, can_access_statics: bool) -> Self {
108+
CompileTimeInterpreter {
109+
steps_remaining: const_eval_limit.0,
110+
stack: Vec::new(),
111+
can_access_statics,
112+
}
112113
}
113114
}
114115

@@ -233,8 +234,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
233234

234235
type MemoryKind = MemoryKind;
235236

236-
type MemoryExtra = MemoryExtra;
237-
238237
const PANIC_ON_ALLOC_FAIL: bool = false; // will be raised as a proper error
239238

240239
fn load_mir(
@@ -345,7 +344,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
345344
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
346345
};
347346

348-
let ptr = ecx.memory.allocate(
347+
let ptr = ecx.allocate_ptr(
349348
Size::from_bytes(size as u64),
350349
align,
351350
interpret::MemoryKind::Machine(MemoryKind::Heap),
@@ -365,14 +364,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
365364

366365
// If an allocation is created in an another const,
367366
// we don't deallocate it.
368-
let (alloc_id, _, _) = ecx.memory.ptr_get_alloc(ptr)?;
367+
let (alloc_id, _, _) = ecx.ptr_get_alloc_id(ptr)?;
369368
let is_allocated_in_another_const = matches!(
370369
ecx.tcx.get_global_alloc(alloc_id),
371370
Some(interpret::GlobalAlloc::Memory(_))
372371
);
373372

374373
if !is_allocated_in_another_const {
375-
ecx.memory.deallocate(
374+
ecx.deallocate_ptr(
376375
ptr,
377376
Some((size, align)),
378377
interpret::MemoryKind::Machine(MemoryKind::Heap),
@@ -472,7 +471,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
472471
}
473472

474473
fn before_access_global(
475-
memory_extra: &MemoryExtra,
474+
machine: &Self,
476475
alloc_id: AllocId,
477476
alloc: ConstAllocation<'tcx>,
478477
static_def_id: Option<DefId>,
@@ -488,7 +487,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
488487
}
489488
} else {
490489
// Read access. These are usually allowed, with some exceptions.
491-
if memory_extra.can_access_statics {
490+
if machine.can_access_statics {
492491
// Machine configuration allows us read from anything (e.g., `static` initializer).
493492
Ok(())
494493
} else if static_def_id.is_some() {

compiler/rustc_const_eval/src/interpret/cast.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5656
)
5757
.ok_or_else(|| err_inval!(TooGeneric))?;
5858

59-
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
59+
let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
6060
self.write_pointer(fn_ptr, dest)?;
6161
}
6262
_ => span_bug!(self.cur_span(), "reify fn pointer on {:?}", src.layout.ty),
@@ -87,7 +87,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8787
substs,
8888
ty::ClosureKind::FnOnce,
8989
);
90-
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
90+
let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
9191
self.write_pointer(fn_ptr, dest)?;
9292
}
9393
_ => span_bug!(self.cur_span(), "closure fn pointer on {:?}", src.layout.ty),
@@ -153,8 +153,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
153153
return Ok(**src);
154154
} else {
155155
// Casting the metadata away from a fat ptr.
156-
assert_eq!(src.layout.size, 2 * self.memory.pointer_size());
157-
assert_eq!(dest_layout.size, self.memory.pointer_size());
156+
assert_eq!(src.layout.size, 2 * self.pointer_size());
157+
assert_eq!(dest_layout.size, self.pointer_size());
158158
assert!(src.layout.ty.is_unsafe_ptr());
159159
return match **src {
160160
Immediate::ScalarPair(data, _) => Ok(data.into()),

compiler/rustc_const_eval/src/interpret/eval_context.rs

+8-52
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use rustc_span::{Pos, Span};
2222
use rustc_target::abi::{call::FnAbi, Align, HasDataLayout, Size, TargetDataLayout};
2323

2424
use super::{
25-
AllocCheck, AllocId, GlobalId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy, Machine,
26-
MemPlace, MemPlaceMeta, Memory, MemoryKind, Operand, Place, PlaceTy, Pointer,
27-
PointerArithmetic, Provenance, Scalar, ScalarMaybeUninit, StackPopJump,
25+
AllocId, GlobalId, Immediate, InterpErrorInfo, InterpResult, MPlaceTy, Machine, MemPlace,
26+
MemPlaceMeta, Memory, MemoryKind, Operand, Place, PlaceTy, PointerArithmetic, Provenance,
27+
Scalar, ScalarMaybeUninit, StackPopJump,
2828
};
2929
use crate::transform::validate::equal_up_to_regions;
3030

@@ -413,13 +413,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
413413
root_span: Span,
414414
param_env: ty::ParamEnv<'tcx>,
415415
machine: M,
416-
memory_extra: M::MemoryExtra,
417416
) -> Self {
418417
InterpCx {
419418
machine,
420419
tcx: tcx.at(root_span),
421420
param_env,
422-
memory: Memory::new(tcx, memory_extra),
421+
memory: Memory::new(),
423422
recursion_limit: tcx.recursion_limit(),
424423
}
425424
}
@@ -433,49 +432,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
433432
.map_or(self.tcx.span, |f| f.current_span())
434433
}
435434

436-
#[inline(always)]
437-
pub fn scalar_to_ptr(&self, scalar: Scalar<M::PointerTag>) -> Pointer<Option<M::PointerTag>> {
438-
self.memory.scalar_to_ptr(scalar)
439-
}
440-
441-
/// Test if this value might be null.
442-
/// If the machine does not support ptr-to-int casts, this is conservative.
443-
pub fn scalar_may_be_null(&self, scalar: Scalar<M::PointerTag>) -> bool {
444-
match scalar.try_to_int() {
445-
Ok(int) => int.is_null(),
446-
Err(_) => {
447-
// Can only happen during CTFE.
448-
let ptr = self.scalar_to_ptr(scalar);
449-
match self.memory.ptr_try_get_alloc(ptr) {
450-
Ok((alloc_id, offset, _)) => {
451-
let (size, _align) = self
452-
.memory
453-
.get_size_and_align(alloc_id, AllocCheck::MaybeDead)
454-
.expect("alloc info with MaybeDead cannot fail");
455-
// If the pointer is out-of-bounds, it may be null.
456-
// Note that one-past-the-end (offset == size) is still inbounds, and never null.
457-
offset > size
458-
}
459-
Err(_offset) => bug!("a non-int scalar is always a pointer"),
460-
}
461-
}
462-
}
463-
}
464-
465-
/// Call this to turn untagged "global" pointers (obtained via `tcx`) into
466-
/// the machine pointer to the allocation. Must never be used
467-
/// for any other pointers, nor for TLS statics.
468-
///
469-
/// Using the resulting pointer represents a *direct* access to that memory
470-
/// (e.g. by directly using a `static`),
471-
/// as opposed to access through a pointer that was created by the program.
472-
///
473-
/// This function can fail only if `ptr` points to an `extern static`.
474-
#[inline(always)]
475-
pub fn global_base_pointer(&self, ptr: Pointer) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
476-
self.memory.global_base_pointer(ptr)
477-
}
478-
479435
#[inline(always)]
480436
pub(crate) fn stack(&self) -> &[Frame<'mir, 'tcx, M::PointerTag, M::FrameExtra>] {
481437
M::stack(self)
@@ -949,9 +905,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
949905
trace!(
950906
"deallocating local {:?}: {:?}",
951907
local,
952-
self.memory.dump_alloc(ptr.provenance.unwrap().get_alloc_id())
908+
self.dump_alloc(ptr.provenance.unwrap().get_alloc_id())
953909
);
954-
self.memory.deallocate(ptr, None, MemoryKind::Stack)?;
910+
self.deallocate_ptr(ptr, None, MemoryKind::Stack)?;
955911
};
956912
Ok(())
957913
}
@@ -1057,15 +1013,15 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> std::fmt::Debug
10571013
}
10581014
}
10591015

1060-
write!(fmt, ": {:?}", self.ecx.memory.dump_allocs(allocs))
1016+
write!(fmt, ": {:?}", self.ecx.dump_allocs(allocs))
10611017
}
10621018
Place::Ptr(mplace) => match mplace.ptr.provenance.map(Provenance::get_alloc_id) {
10631019
Some(alloc_id) => write!(
10641020
fmt,
10651021
"by align({}) ref {:?}: {:?}",
10661022
mplace.align.bytes(),
10671023
mplace.ptr,
1068-
self.ecx.memory.dump_alloc(alloc_id)
1024+
self.ecx.dump_alloc(alloc_id)
10691025
),
10701026
ptr => write!(fmt, " integral by ref: {:?}", ptr),
10711027
},

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
318318
// exception from the exception.)
319319
// This is the dual to the special exception for offset-by-0
320320
// in the inbounds pointer offset operation (see `ptr_offset_inbounds` below).
321-
match (self.memory.ptr_try_get_alloc(a), self.memory.ptr_try_get_alloc(b)) {
321+
match (self.ptr_try_get_alloc_id(a), self.ptr_try_get_alloc_id(b)) {
322322
(Err(a), Err(b)) if a == b && a != 0 => {
323323
// Both are the same non-null integer.
324324
self.write_scalar(Scalar::from_machine_isize(0, self), dest)?;
@@ -335,13 +335,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
335335
);
336336
}
337337
// And they must both be valid for zero-sized accesses ("in-bounds or one past the end").
338-
self.memory.check_ptr_access_align(
338+
self.check_ptr_access_align(
339339
a,
340340
Size::ZERO,
341341
Align::ONE,
342342
CheckInAllocMsg::OffsetFromTest,
343343
)?;
344-
self.memory.check_ptr_access_align(
344+
self.check_ptr_access_align(
345345
b,
346346
Size::ZERO,
347347
Align::ONE,
@@ -545,7 +545,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
545545
let min_ptr = if offset_bytes >= 0 { ptr } else { offset_ptr };
546546
let size = offset_bytes.unsigned_abs();
547547
// This call handles checking for integer/null pointers.
548-
self.memory.check_ptr_access_align(
548+
self.check_ptr_access_align(
549549
min_ptr,
550550
Size::from_bytes(size),
551551
Align::ONE,
@@ -577,7 +577,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
577577
let src = self.read_pointer(&src)?;
578578
let dst = self.read_pointer(&dst)?;
579579

580-
self.memory.copy(src, align, dst, align, size, nonoverlapping)
580+
self.mem_copy(src, align, dst, align, size, nonoverlapping)
581581
}
582582

583583
pub(crate) fn write_bytes_intrinsic(
@@ -600,7 +600,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
600600
.ok_or_else(|| err_ub_format!("overflow computing total size of `write_bytes`"))?;
601601

602602
let bytes = std::iter::repeat(byte).take(len.bytes_usize());
603-
self.memory.write_bytes(dst, bytes)
603+
self.write_bytes_ptr(dst, bytes)
604604
}
605605

606606
pub(crate) fn raw_eq_intrinsic(
@@ -613,8 +613,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
613613

614614
let lhs = self.read_pointer(lhs)?;
615615
let rhs = self.read_pointer(rhs)?;
616-
let lhs_bytes = self.memory.read_bytes(lhs, layout.size)?;
617-
let rhs_bytes = self.memory.read_bytes(rhs, layout.size)?;
616+
let lhs_bytes = self.read_bytes_ptr(lhs, layout.size)?;
617+
let rhs_bytes = self.read_bytes_ptr(rhs, layout.size)?;
618618
Ok(Scalar::from_bool(lhs_bytes == rhs_bytes))
619619
}
620620
}

0 commit comments

Comments
 (0)