Skip to content

Commit 8876ca3

Browse files
committedMar 6, 2022
Auto merge of #94597 - nnethercote:ConstAllocation, r=fee1-dead
Introduce `ConstAllocation`. Currently some `Allocation`s are interned, some are not, and it's very hard to tell at a use point which is which. This commit introduces `ConstAllocation` for the known-interned ones, which makes the division much clearer. `ConstAllocation::inner()` is used to get the underlying `Allocation`. In some places it's natural to use an `Allocation`, in some it's natural to use a `ConstAllocation`, and in some places there's no clear choice. I've tried to make things look as nice as possible, while generally favouring `ConstAllocation`, which is the type that embodies more information. This does require quite a few calls to `inner()`. The commit also tweaks how `PartialOrd` works for `Interned`. The previous code was too clever by half, building on `T: Ord` to make the code shorter. That caused problems with deriving `PartialOrd` and `Ord` for `ConstAllocation`, so I changed it to build on `T: PartialOrd`, which is slightly more verbose but much more standard and avoided the problems. r? `@fee1-dead`
·
1.91.01.61.0
2 parents 38a0b81 + 4852291 commit 8876ca3

File tree

30 files changed

+166
-119
lines changed

30 files changed

+166
-119
lines changed
 

‎compiler/rustc_codegen_cranelift/src/constant.rs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_errors::ErrorGuaranteed;
55
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
66
use rustc_middle::mir::interpret::{
7-
read_target_uint, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
7+
read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
88
};
99
use rustc_middle::ty::ConstKind;
1010
use rustc_span::DUMMY_SP;
@@ -202,7 +202,7 @@ pub(crate) fn codegen_const_value<'tcx>(
202202
&mut fx.constants_cx,
203203
fx.module,
204204
alloc_id,
205-
alloc.mutability,
205+
alloc.inner().mutability,
206206
);
207207
let local_data_id =
208208
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
@@ -257,11 +257,15 @@ pub(crate) fn codegen_const_value<'tcx>(
257257

258258
pub(crate) fn pointer_for_allocation<'tcx>(
259259
fx: &mut FunctionCx<'_, '_, 'tcx>,
260-
alloc: &'tcx Allocation,
260+
alloc: ConstAllocation<'tcx>,
261261
) -> crate::pointer::Pointer {
262262
let alloc_id = fx.tcx.create_memory_alloc(alloc);
263-
let data_id =
264-
data_id_for_alloc_id(&mut fx.constants_cx, &mut *fx.module, alloc_id, alloc.mutability);
263+
let data_id = data_id_for_alloc_id(
264+
&mut fx.constants_cx,
265+
&mut *fx.module,
266+
alloc_id,
267+
alloc.inner().mutability,
268+
);
265269

266270
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
267271
if fx.clif_comments.enabled() {
@@ -361,7 +365,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
361365
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
362366
module
363367
.declare_anonymous_data(
364-
alloc.mutability == rustc_hir::Mutability::Mut,
368+
alloc.inner().mutability == rustc_hir::Mutability::Mut,
365369
false,
366370
)
367371
.unwrap()
@@ -386,6 +390,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
386390
}
387391

388392
let mut data_ctx = DataContext::new();
393+
let alloc = alloc.inner();
389394
data_ctx.set_align(alloc.align.bytes());
390395

391396
if let Some(section_name) = section_name {
@@ -429,7 +434,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
429434
continue;
430435
}
431436
GlobalAlloc::Memory(target_alloc) => {
432-
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.mutability)
437+
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.inner().mutability)
433438
}
434439
GlobalAlloc::Static(def_id) => {
435440
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)

‎compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
159159
let idx_bytes = match idx_const {
160160
ConstValue::ByRef { alloc, offset } => {
161161
let size = Size::from_bytes(4 * ret_lane_count /* size_of([u32; ret_lane_count]) */);
162-
alloc.get_bytes(fx, alloc_range(offset, size)).unwrap()
162+
alloc.inner().get_bytes(fx, alloc_range(offset, size)).unwrap()
163163
}
164164
_ => unreachable!("{:?}", idx_const),
165165
};

‎compiler/rustc_codegen_gcc/src/common.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_codegen_ssa::traits::{
1313
use rustc_middle::mir::Mutability;
1414
use rustc_middle::ty::ScalarInt;
1515
use rustc_middle::ty::layout::{TyAndLayout, LayoutOf};
16-
use rustc_middle::mir::interpret::{Allocation, GlobalAlloc, Scalar};
16+
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
1717
use rustc_span::Symbol;
1818
use rustc_target::abi::{self, HasDataLayout, Pointer, Size};
1919

@@ -230,6 +230,7 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
230230
match self.tcx.global_alloc(alloc_id) {
231231
GlobalAlloc::Memory(alloc) => {
232232
let init = const_alloc_to_gcc(self, alloc);
233+
let alloc = alloc.inner();
233234
let value =
234235
match alloc.mutability {
235236
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
@@ -262,21 +263,21 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
262263
}
263264
}
264265

265-
fn const_data_from_alloc(&self, alloc: &Allocation) -> Self::Value {
266+
fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value {
266267
const_alloc_to_gcc(self, alloc)
267268
}
268269

269-
fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: &Allocation, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> {
270-
assert_eq!(alloc.align, layout.align.abi);
270+
fn from_const_alloc(&self, layout: TyAndLayout<'tcx>, alloc: ConstAllocation<'tcx>, offset: Size) -> PlaceRef<'tcx, RValue<'gcc>> {
271+
assert_eq!(alloc.inner().align, layout.align.abi);
271272
let ty = self.type_ptr_to(layout.gcc_type(self, true));
272273
let value =
273274
if layout.size == Size::ZERO {
274-
let value = self.const_usize(alloc.align.bytes());
275+
let value = self.const_usize(alloc.inner().align.bytes());
275276
self.context.new_cast(None, value, ty)
276277
}
277278
else {
278279
let init = const_alloc_to_gcc(self, alloc);
279-
let base_addr = self.static_addr_of(init, alloc.align, None);
280+
let base_addr = self.static_addr_of(init, alloc.inner().align, None);
280281

281282
let array = self.const_bitcast(base_addr, self.type_i8p());
282283
let value = self.context.new_array_access(None, array, self.const_usize(offset.bytes())).get_address(None);

‎compiler/rustc_codegen_gcc/src/consts.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}
77
use rustc_middle::mir::mono::MonoItem;
88
use rustc_middle::ty::{self, Instance, Ty};
99
use rustc_middle::ty::layout::LayoutOf;
10-
use rustc_middle::mir::interpret::{self, Allocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
10+
use rustc_middle::mir::interpret::{self, ConstAllocation, ErrorHandled, Scalar as InterpScalar, read_target_uint};
1111
use rustc_span::Span;
1212
use rustc_span::def_id::DefId;
1313
use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size, WrappingRange};
@@ -284,7 +284,8 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
284284
}
285285
}
286286

287-
pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: &Allocation) -> RValue<'gcc> {
287+
pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: ConstAllocation<'tcx>) -> RValue<'gcc> {
288+
let alloc = alloc.inner();
288289
let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
289290
let dl = cx.data_layout();
290291
let pointer_size = dl.pointer_size.bytes() as usize;
@@ -338,7 +339,7 @@ pub fn const_alloc_to_gcc<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, alloc: &Alloca
338339
cx.const_struct(&llvals, true)
339340
}
340341

341-
pub fn codegen_static_initializer<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, def_id: DefId) -> Result<(RValue<'gcc>, &'tcx Allocation), ErrorHandled> {
342+
pub fn codegen_static_initializer<'gcc, 'tcx>(cx: &CodegenCx<'gcc, 'tcx>, def_id: DefId) -> Result<(RValue<'gcc>, ConstAllocation<'tcx>), ErrorHandled> {
342343
let alloc = cx.tcx.eval_static_initializer(def_id)?;
343344
Ok((const_alloc_to_gcc(cx, alloc), alloc))
344345
}

‎compiler/rustc_codegen_llvm/src/common.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_ast::Mutability;
1111
use rustc_codegen_ssa::mir::place::PlaceRef;
1212
use rustc_codegen_ssa::traits::*;
1313
use rustc_middle::bug;
14-
use rustc_middle::mir::interpret::{Allocation, GlobalAlloc, Scalar};
14+
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
1515
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1616
use rustc_middle::ty::ScalarInt;
1717
use rustc_span::symbol::Symbol;
@@ -249,6 +249,7 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
249249
let (base_addr, base_addr_space) = match self.tcx.global_alloc(alloc_id) {
250250
GlobalAlloc::Memory(alloc) => {
251251
let init = const_alloc_to_llvm(self, alloc);
252+
let alloc = alloc.inner();
252253
let value = match alloc.mutability {
253254
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
254255
_ => self.static_addr_of(init, alloc.align, None),
@@ -285,24 +286,25 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
285286
}
286287
}
287288

288-
fn const_data_from_alloc(&self, alloc: &Allocation) -> Self::Value {
289+
fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value {
289290
const_alloc_to_llvm(self, alloc)
290291
}
291292

292293
fn from_const_alloc(
293294
&self,
294295
layout: TyAndLayout<'tcx>,
295-
alloc: &Allocation,
296+
alloc: ConstAllocation<'tcx>,
296297
offset: Size,
297298
) -> PlaceRef<'tcx, &'ll Value> {
298-
assert_eq!(alloc.align, layout.align.abi);
299+
let alloc_align = alloc.inner().align;
300+
assert_eq!(alloc_align, layout.align.abi);
299301
let llty = self.type_ptr_to(layout.llvm_type(self));
300302
let llval = if layout.size == Size::ZERO {
301-
let llval = self.const_usize(alloc.align.bytes());
303+
let llval = self.const_usize(alloc_align.bytes());
302304
unsafe { llvm::LLVMConstIntToPtr(llval, llty) }
303305
} else {
304306
let init = const_alloc_to_llvm(self, alloc);
305-
let base_addr = self.static_addr_of(init, alloc.align, None);
307+
let base_addr = self.static_addr_of(init, alloc_align, None);
306308

307309
let llval = unsafe {
308310
llvm::LLVMRustConstInBoundsGEP2(

‎compiler/rustc_codegen_llvm/src/consts.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_codegen_ssa::traits::*;
1212
use rustc_hir::def_id::DefId;
1313
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
1414
use rustc_middle::mir::interpret::{
15-
read_target_uint, Allocation, ErrorHandled, GlobalAlloc, InitChunk, Pointer,
15+
read_target_uint, Allocation, ConstAllocation, ErrorHandled, GlobalAlloc, InitChunk, Pointer,
1616
Scalar as InterpScalar,
1717
};
1818
use rustc_middle::mir::mono::MonoItem;
@@ -25,7 +25,8 @@ use rustc_target::abi::{
2525
use std::ops::Range;
2626
use tracing::debug;
2727

28-
pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
28+
pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: ConstAllocation<'_>) -> &'ll Value {
29+
let alloc = alloc.inner();
2930
let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
3031
let dl = cx.data_layout();
3132
let pointer_size = dl.pointer_size.bytes() as usize;
@@ -127,7 +128,7 @@ pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) ->
127128
pub fn codegen_static_initializer<'ll, 'tcx>(
128129
cx: &CodegenCx<'ll, 'tcx>,
129130
def_id: DefId,
130-
) -> Result<(&'ll Value, &'tcx Allocation), ErrorHandled> {
131+
) -> Result<(&'ll Value, ConstAllocation<'tcx>), ErrorHandled> {
131132
let alloc = cx.tcx.eval_static_initializer(def_id)?;
132133
Ok((const_alloc_to_llvm(cx, alloc), alloc))
133134
}
@@ -370,6 +371,7 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
370371
// Error has already been reported
371372
return;
372373
};
374+
let alloc = alloc.inner();
373375

374376
let g = self.get_static(def_id);
375377

‎compiler/rustc_codegen_ssa/src/traits/consts.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::BackendTypes;
22
use crate::mir::place::PlaceRef;
3-
use rustc_middle::mir::interpret::{Allocation, Scalar};
3+
use rustc_middle::mir::interpret::{ConstAllocation, Scalar};
44
use rustc_middle::ty::layout::TyAndLayout;
55
use rustc_span::Symbol;
66
use rustc_target::abi::{self, Size};
@@ -26,13 +26,13 @@ pub trait ConstMethods<'tcx>: BackendTypes {
2626
fn const_to_opt_uint(&self, v: Self::Value) -> Option<u64>;
2727
fn const_to_opt_u128(&self, v: Self::Value, sign_ext: bool) -> Option<u128>;
2828

29-
fn const_data_from_alloc(&self, alloc: &Allocation) -> Self::Value;
29+
fn const_data_from_alloc(&self, alloc: ConstAllocation<'tcx>) -> Self::Value;
3030

3131
fn scalar_to_backend(&self, cv: Scalar, layout: abi::Scalar, llty: Self::Type) -> Self::Value;
3232
fn from_const_alloc(
3333
&self,
3434
layout: TyAndLayout<'tcx>,
35-
alloc: &Allocation,
35+
alloc: ConstAllocation<'tcx>,
3636
offset: Size,
3737
) -> PlaceRef<'tcx, Self::Value>;
3838

‎compiler/rustc_const_eval/src/const_eval/eval_queries.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
367367
"the raw bytes of the constant ({}",
368368
display_allocation(
369369
*ecx.tcx,
370-
ecx.tcx.global_alloc(alloc_id).unwrap_memory()
370+
ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner()
371371
)
372372
));
373373
},

‎compiler/rustc_const_eval/src/const_eval/machine.rs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use rustc_target::abi::{Align, Size};
1818
use rustc_target::spec::abi::Abi;
1919

2020
use crate::interpret::{
21-
self, compile_time_machine, AllocId, Allocation, Frame, ImmTy, InterpCx, InterpResult, OpTy,
22-
PlaceTy, Scalar, StackPopUnwind,
21+
self, compile_time_machine, AllocId, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
22+
OpTy, PlaceTy, Scalar, StackPopUnwind,
2323
};
2424

2525
use super::error::*;
@@ -475,13 +475,14 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
475475
fn before_access_global(
476476
memory_extra: &MemoryExtra,
477477
alloc_id: AllocId,
478-
allocation: &Allocation,
478+
alloc: ConstAllocation<'tcx>,
479479
static_def_id: Option<DefId>,
480480
is_write: bool,
481481
) -> InterpResult<'tcx> {
482+
let alloc = alloc.inner();
482483
if is_write {
483484
// Write access. These are never allowed, but we give a targeted error message.
484-
if allocation.mutability == Mutability::Not {
485+
if alloc.mutability == Mutability::Not {
485486
Err(err_ub!(WriteToReadOnly(alloc_id)).into())
486487
} else {
487488
Err(ConstEvalErrKind::ModifiedGlobal.into())
@@ -504,7 +505,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
504505
// But make sure we never accept a read from something mutable, that would be
505506
// unsound. The reason is that as the content of this allocation may be different
506507
// now and at run-time, so if we permit reading now we might return the wrong value.
507-
assert_eq!(allocation.mutability, Mutability::Not);
508+
assert_eq!(alloc.mutability, Mutability::Not);
508509
Ok(())
509510
}
510511
}

‎compiler/rustc_const_eval/src/const_eval/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub(crate) fn deref_const<'tcx>(
183183
let mplace = ecx.deref_operand(&op).unwrap();
184184
if let Some(alloc_id) = mplace.ptr.provenance {
185185
assert_eq!(
186-
tcx.get_global_alloc(alloc_id).unwrap().unwrap_memory().mutability,
186+
tcx.get_global_alloc(alloc_id).unwrap().unwrap_memory().inner().mutability,
187187
Mutability::Not,
188188
"deref_const cannot be used with mutable allocations as \
189189
that could allow pattern matching to observe mutable statics",

‎compiler/rustc_const_eval/src/interpret/intern.rs‎

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

2424
use rustc_ast::Mutability;
2525

26-
use super::{AllocId, Allocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy, ValueVisitor};
26+
use super::{
27+
AllocId, Allocation, ConstAllocation, InterpCx, MPlaceTy, Machine, MemoryKind, PlaceTy,
28+
ValueVisitor,
29+
};
2730
use crate::const_eval;
2831

2932
pub trait CompileTimeMachine<'mir, 'tcx, T> = Machine<
@@ -131,8 +134,8 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval:
131134
alloc.mutability = Mutability::Not;
132135
};
133136
// link the alloc id to the actual allocation
134-
let alloc = tcx.intern_const_alloc(alloc);
135137
leftover_allocations.extend(alloc.relocations().iter().map(|&(_, alloc_id)| alloc_id));
138+
let alloc = tcx.intern_const_alloc(alloc);
136139
tcx.set_alloc_id_memory(alloc_id, alloc);
137140
None
138141
}
@@ -393,7 +396,7 @@ pub fn intern_const_alloc_recursive<
393396
}
394397
let alloc = tcx.intern_const_alloc(alloc);
395398
tcx.set_alloc_id_memory(alloc_id, alloc);
396-
for &(_, alloc_id) in alloc.relocations().iter() {
399+
for &(_, alloc_id) in alloc.inner().relocations().iter() {
397400
if leftover_allocations.insert(alloc_id) {
398401
todo.push(alloc_id);
399402
}
@@ -425,7 +428,7 @@ impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>>
425428
&mut InterpCx<'mir, 'tcx, M>,
426429
&PlaceTy<'tcx, M::PointerTag>,
427430
) -> InterpResult<'tcx, ()>,
428-
) -> InterpResult<'tcx, &'tcx Allocation> {
431+
) -> InterpResult<'tcx, ConstAllocation<'tcx>> {
429432
let dest = self.allocate(layout, MemoryKind::Stack)?;
430433
f(self, &dest.into())?;
431434
let mut alloc = self.memory.alloc_map.remove(&dest.ptr.provenance.unwrap()).unwrap().1;

‎compiler/rustc_const_eval/src/interpret/intrinsics.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ crate fn eval_nullary_intrinsic<'tcx>(
5656
sym::type_name => {
5757
ensure_monomorphic_enough(tcx, tp_ty)?;
5858
let alloc = type_name::alloc_type_name(tcx, tp_ty);
59-
ConstValue::Slice { data: alloc, start: 0, end: alloc.len() }
59+
ConstValue::Slice { data: alloc, start: 0, end: alloc.inner().len() }
6060
}
6161
sym::needs_drop => {
6262
ensure_monomorphic_enough(tcx, tp_ty)?;

‎compiler/rustc_const_eval/src/interpret/intrinsics/type_name.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir::def_id::CrateNum;
22
use rustc_hir::definitions::DisambiguatedDefPathData;
3-
use rustc_middle::mir::interpret::Allocation;
3+
use rustc_middle::mir::interpret::{Allocation, ConstAllocation};
44
use rustc_middle::ty::{
55
self,
66
print::{PrettyPrinter, Print, Printer},
@@ -188,7 +188,7 @@ impl Write for AbsolutePathPrinter<'_> {
188188
}
189189

190190
/// Directly returns an `Allocation` containing an absolute path representation of the given type.
191-
crate fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> &'tcx Allocation {
191+
crate fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> {
192192
let path = AbsolutePathPrinter { tcx, path: String::new() }.print_type(ty).unwrap().path;
193193
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
194194
tcx.intern_const_alloc(alloc)

‎compiler/rustc_const_eval/src/interpret/machine.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use rustc_target::abi::Size;
1313
use rustc_target::spec::abi::Abi;
1414

1515
use super::{
16-
AllocId, AllocRange, Allocation, Frame, ImmTy, InterpCx, InterpResult, LocalValue, MemPlace,
17-
Memory, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar, StackPopUnwind,
16+
AllocId, AllocRange, Allocation, ConstAllocation, Frame, ImmTy, InterpCx, InterpResult,
17+
LocalValue, MemPlace, Memory, MemoryKind, OpTy, Operand, PlaceTy, Pointer, Provenance, Scalar,
18+
StackPopUnwind,
1819
};
1920

2021
/// Data returned by Machine::stack_pop,
@@ -252,7 +253,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
252253
fn before_access_global(
253254
_memory_extra: &Self::MemoryExtra,
254255
_alloc_id: AllocId,
255-
_allocation: &Allocation,
256+
_allocation: ConstAllocation<'tcx>,
256257
_static_def_id: Option<DefId>,
257258
_is_write: bool,
258259
) -> InterpResult<'tcx> {

‎compiler/rustc_const_eval/src/interpret/memory.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
525525
}
526526
};
527527
M::before_access_global(&self.extra, id, alloc, def_id, is_write)?;
528-
let alloc = Cow::Borrowed(alloc);
529528
// We got tcx memory. Let the machine initialize its "extra" stuff.
530529
let alloc = M::init_allocation_extra(
531530
self,
532531
id, // always use the ID we got as input, not the "hidden" one.
533-
alloc,
532+
Cow::Borrowed(alloc.inner()),
534533
M::GLOBAL_KIND.map(MemoryKind::Machine),
535534
);
536535
Ok(alloc)
@@ -711,6 +710,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
711710
Some(GlobalAlloc::Memory(alloc)) => {
712711
// Need to duplicate the logic here, because the global allocations have
713712
// different associated types than the interpreter-local ones.
713+
let alloc = alloc.inner();
714714
Ok((alloc.size(), alloc.align))
715715
}
716716
Some(GlobalAlloc::Function(_)) => bug!("We already checked function pointers above"),
@@ -867,7 +867,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> std::fmt::Debug for DumpAllocs<'a,
867867
&mut *fmt,
868868
self.mem.tcx,
869869
&mut allocs_to_print,
870-
alloc,
870+
alloc.inner(),
871871
)?;
872872
}
873873
Some(GlobalAlloc::Function(func)) => {

‎compiler/rustc_data_structures/src/intern.rs‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@ impl<'a, T> PartialEq for Interned<'a, T> {
6262

6363
impl<'a, T> Eq for Interned<'a, T> {}
6464

65-
// In practice you can't intern any `T` that doesn't implement `Eq`, because
66-
// that's needed for hashing. Therefore, we won't be interning any `T` that
67-
// implements `PartialOrd` without also implementing `Ord`. So we can have the
68-
// bound `T: Ord` here and avoid duplication with the `Ord` impl below.
69-
impl<'a, T: Ord> PartialOrd for Interned<'a, T> {
65+
impl<'a, T: PartialOrd> PartialOrd for Interned<'a, T> {
7066
fn partial_cmp(&self, other: &Interned<'a, T>) -> Option<Ordering> {
71-
Some(self.cmp(other))
67+
// Pointer equality implies equality, due to the uniqueness constraint,
68+
// but the contents must be compared otherwise.
69+
if ptr::eq(self.0, other.0) {
70+
Some(Ordering::Equal)
71+
} else {
72+
let res = self.0.partial_cmp(&other.0);
73+
debug_assert_ne!(res, Some(Ordering::Equal));
74+
res
75+
}
7276
}
7377
}
7478

‎compiler/rustc_middle/src/mir/interpret/allocation.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
33
use std::borrow::Cow;
44
use std::convert::{TryFrom, TryInto};
5+
use std::fmt;
56
use std::iter;
67
use std::ops::{Deref, Range};
78
use std::ptr;
89

910
use rustc_ast::Mutability;
11+
use rustc_data_structures::intern::Interned;
1012
use rustc_data_structures::sorted_map::SortedMap;
1113
use rustc_span::DUMMY_SP;
1214
use rustc_target::abi::{Align, HasDataLayout, Size};
@@ -47,6 +49,34 @@ pub struct Allocation<Tag = AllocId, Extra = ()> {
4749
pub extra: Extra,
4850
}
4951

52+
/// Interned types generally have an `Outer` type and an `Inner` type, where
53+
/// `Outer` is a newtype around `Interned<Inner>`, and all the operations are
54+
/// done on `Outer`, because all occurrences are interned. E.g. `Ty` is an
55+
/// outer type and `TyS` is its inner type.
56+
///
57+
/// Here things are different because only const allocations are interned. This
58+
/// means that both the inner type (`Allocation`) and the outer type
59+
/// (`ConstAllocation`) are used quite a bit.
60+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
61+
#[cfg_attr(not(bootstrap), rustc_pass_by_value)]
62+
pub struct ConstAllocation<'tcx, Tag = AllocId, Extra = ()>(
63+
pub Interned<'tcx, Allocation<Tag, Extra>>,
64+
);
65+
66+
impl<'tcx> fmt::Debug for ConstAllocation<'tcx> {
67+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68+
// This matches how `Allocation` is printed. We print it like this to
69+
// avoid having to update expected output in a lot of tests.
70+
write!(f, "{:?}", self.inner())
71+
}
72+
}
73+
74+
impl<'tcx, Tag, Extra> ConstAllocation<'tcx, Tag, Extra> {
75+
pub fn inner(self) -> &'tcx Allocation<Tag, Extra> {
76+
self.0.0
77+
}
78+
}
79+
5080
/// We have our own error type that does not know about the `AllocId`; that information
5181
/// is added when converting to `InterpError`.
5282
#[derive(Debug)]

‎compiler/rustc_middle/src/mir/interpret/mod.rs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ pub use self::error::{
126126
pub use self::value::{get_slice_bytes, ConstAlloc, ConstValue, Scalar, ScalarMaybeUninit};
127127

128128
pub use self::allocation::{
129-
alloc_range, AllocRange, Allocation, InitChunk, InitChunkIter, InitMask, Relocations,
129+
alloc_range, AllocRange, Allocation, ConstAllocation, InitChunk, InitChunkIter, InitMask,
130+
Relocations,
130131
};
131132

132133
pub use self::pointer::{Pointer, PointerArithmetic, Provenance};
@@ -343,7 +344,7 @@ impl<'s> AllocDecodingSession<'s> {
343344
let alloc_id = decoder.with_position(pos, |decoder| {
344345
match alloc_kind {
345346
AllocDiscriminant::Alloc => {
346-
let alloc = <&'tcx Allocation as Decodable<_>>::decode(decoder);
347+
let alloc = <ConstAllocation<'tcx> as Decodable<_>>::decode(decoder);
347348
// We already have a reserved `AllocId`.
348349
let alloc_id = alloc_id.unwrap();
349350
trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc);
@@ -387,14 +388,14 @@ pub enum GlobalAlloc<'tcx> {
387388
/// This is also used to break the cycle in recursive statics.
388389
Static(DefId),
389390
/// The alloc ID points to memory.
390-
Memory(&'tcx Allocation),
391+
Memory(ConstAllocation<'tcx>),
391392
}
392393

393394
impl<'tcx> GlobalAlloc<'tcx> {
394395
/// Panics if the `GlobalAlloc` does not refer to an `GlobalAlloc::Memory`
395396
#[track_caller]
396397
#[inline]
397-
pub fn unwrap_memory(&self) -> &'tcx Allocation {
398+
pub fn unwrap_memory(&self) -> ConstAllocation<'tcx> {
398399
match *self {
399400
GlobalAlloc::Memory(mem) => mem,
400401
_ => bug!("expected memory, got {:?}", self),
@@ -512,7 +513,7 @@ impl<'tcx> TyCtxt<'tcx> {
512513
/// Statics with identical content will still point to the same `Allocation`, i.e.,
513514
/// their data will be deduplicated through `Allocation` interning -- but they
514515
/// are different places in memory and as such need different IDs.
515-
pub fn create_memory_alloc(self, mem: &'tcx Allocation) -> AllocId {
516+
pub fn create_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
516517
let id = self.reserve_alloc_id();
517518
self.set_alloc_id_memory(id, mem);
518519
id
@@ -543,15 +544,15 @@ impl<'tcx> TyCtxt<'tcx> {
543544

544545
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. Trying to
545546
/// call this function twice, even with the same `Allocation` will ICE the compiler.
546-
pub fn set_alloc_id_memory(self, id: AllocId, mem: &'tcx Allocation) {
547+
pub fn set_alloc_id_memory(self, id: AllocId, mem: ConstAllocation<'tcx>) {
547548
if let Some(old) = self.alloc_map.lock().alloc_map.insert(id, GlobalAlloc::Memory(mem)) {
548549
bug!("tried to set allocation ID {}, but it was already existing as {:#?}", id, old);
549550
}
550551
}
551552

552553
/// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called
553554
/// twice for the same `(AllocId, Allocation)` pair.
554-
fn set_alloc_id_same_memory(self, id: AllocId, mem: &'tcx Allocation) {
555+
fn set_alloc_id_same_memory(self, id: AllocId, mem: ConstAllocation<'tcx>) {
555556
self.alloc_map.lock().alloc_map.insert_same(id, GlobalAlloc::Memory(mem));
556557
}
557558
}

‎compiler/rustc_middle/src/mir/interpret/queries.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'tcx> TyCtxt<'tcx> {
7979
pub fn eval_static_initializer(
8080
self,
8181
def_id: DefId,
82-
) -> Result<&'tcx mir::Allocation, ErrorHandled> {
82+
) -> Result<mir::ConstAllocation<'tcx>, ErrorHandled> {
8383
trace!("eval_static_initializer: Need to compute {:?}", def_id);
8484
assert!(self.is_static(def_id));
8585
let instance = ty::Instance::mono(self, def_id);
@@ -92,7 +92,7 @@ impl<'tcx> TyCtxt<'tcx> {
9292
self,
9393
gid: GlobalId<'tcx>,
9494
param_env: ty::ParamEnv<'tcx>,
95-
) -> Result<&'tcx mir::Allocation, ErrorHandled> {
95+
) -> Result<mir::ConstAllocation<'tcx>, ErrorHandled> {
9696
let param_env = param_env.with_const();
9797
trace!("eval_to_allocation: Need to compute {:?}", gid);
9898
let raw_const = self.eval_to_allocation_raw(param_env.and(gid))?;

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_target::abi::{HasDataLayout, Size};
1111
use crate::ty::{Lift, ParamEnv, ScalarInt, Ty, TyCtxt};
1212

1313
use super::{
14-
AllocId, AllocRange, Allocation, InterpResult, Pointer, PointerArithmetic, Provenance,
14+
AllocId, AllocRange, ConstAllocation, InterpResult, Pointer, PointerArithmetic, Provenance,
1515
};
1616

1717
/// Represents the result of const evaluation via the `eval_to_allocation` query.
@@ -34,13 +34,13 @@ pub enum ConstValue<'tcx> {
3434
Scalar(Scalar),
3535

3636
/// Used only for `&[u8]` and `&str`
37-
Slice { data: &'tcx Allocation, start: usize, end: usize },
37+
Slice { data: ConstAllocation<'tcx>, start: usize, end: usize },
3838

3939
/// A value not represented/representable by `Scalar` or `Slice`
4040
ByRef {
4141
/// The backing memory of the value, may contain more memory than needed for just the value
42-
/// in order to share `Allocation`s between values
43-
alloc: &'tcx Allocation,
42+
/// in order to share `ConstAllocation`s between values
43+
alloc: ConstAllocation<'tcx>,
4444
/// Offset into `alloc`
4545
offset: Size,
4646
},
@@ -603,11 +603,12 @@ impl<'tcx, Tag: Provenance> ScalarMaybeUninit<Tag> {
603603
pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
604604
if let ConstValue::Slice { data, start, end } = val {
605605
let len = end - start;
606-
data.get_bytes(
607-
cx,
608-
AllocRange { start: Size::from_bytes(start), size: Size::from_bytes(len) },
609-
)
610-
.unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
606+
data.inner()
607+
.get_bytes(
608+
cx,
609+
AllocRange { start: Size::from_bytes(start), size: Size::from_bytes(len) },
610+
)
611+
.unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
611612
} else {
612613
bug!("expected const slice, but found another const value");
613614
}

‎compiler/rustc_middle/src/mir/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/mir/index.html
44
55
use crate::mir::coverage::{CodeRegion, CoverageKind};
6-
use crate::mir::interpret::{Allocation, ConstValue, GlobalAlloc, Scalar};
6+
use crate::mir::interpret::{ConstAllocation, ConstValue, GlobalAlloc, Scalar};
77
use crate::mir::visit::MirVisitable;
88
use crate::ty::adjustment::PointerCast;
99
use crate::ty::codec::{TyDecoder, TyEncoder};

‎compiler/rustc_middle/src/mir/pretty.rs‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use rustc_data_structures::fx::FxHashMap;
1212
use rustc_hir::def_id::DefId;
1313
use rustc_index::vec::Idx;
1414
use rustc_middle::mir::interpret::{
15-
read_target_uint, AllocId, Allocation, ConstValue, GlobalAlloc, Pointer, Provenance,
15+
read_target_uint, AllocId, Allocation, ConstAllocation, ConstValue, GlobalAlloc, Pointer,
16+
Provenance,
1617
};
1718
use rustc_middle::mir::visit::Visitor;
1819
use rustc_middle::mir::MirSource;
@@ -652,8 +653,10 @@ pub fn write_allocations<'tcx>(
652653
body: &Body<'_>,
653654
w: &mut dyn Write,
654655
) -> io::Result<()> {
655-
fn alloc_ids_from_alloc(alloc: &Allocation) -> impl DoubleEndedIterator<Item = AllocId> + '_ {
656-
alloc.relocations().values().map(|id| *id)
656+
fn alloc_ids_from_alloc(
657+
alloc: ConstAllocation<'_>,
658+
) -> impl DoubleEndedIterator<Item = AllocId> + '_ {
659+
alloc.inner().relocations().values().map(|id| *id)
657660
}
658661
fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> + '_ {
659662
match val {
@@ -686,14 +689,14 @@ pub fn write_allocations<'tcx>(
686689
let mut todo: Vec<_> = seen.iter().copied().collect();
687690
while let Some(id) = todo.pop() {
688691
let mut write_allocation_track_relocs =
689-
|w: &mut dyn Write, alloc: &Allocation| -> io::Result<()> {
692+
|w: &mut dyn Write, alloc: ConstAllocation<'tcx>| -> io::Result<()> {
690693
// `.rev()` because we are popping them from the back of the `todo` vector.
691694
for id in alloc_ids_from_alloc(alloc).rev() {
692695
if seen.insert(id) {
693696
todo.push(id);
694697
}
695698
}
696-
write!(w, "{}", display_allocation(tcx, alloc))
699+
write!(w, "{}", display_allocation(tcx, alloc.inner()))
697700
};
698701
write!(w, "\n{}", id)?;
699702
match tcx.get_global_alloc(id) {

‎compiler/rustc_middle/src/ty/codec.rs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::arena::ArenaAllocatable;
1010
use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
1111
use crate::mir::{
1212
self,
13-
interpret::{AllocId, Allocation},
13+
interpret::{AllocId, ConstAllocation},
1414
};
1515
use crate::thir;
1616
use crate::traits;
@@ -150,6 +150,12 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ty::Const<'tcx> {
150150
}
151151
}
152152

153+
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for ConstAllocation<'tcx> {
154+
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
155+
self.inner().encode(e)
156+
}
157+
}
158+
153159
impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for AllocId {
154160
fn encode(&self, e: &mut E) -> Result<(), E::Error> {
155161
e.encode_alloc_id(self)
@@ -355,8 +361,8 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] {
355361
}
356362
}
357363

358-
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation {
359-
fn decode(decoder: &mut D) -> &'tcx Self {
364+
impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for ConstAllocation<'tcx> {
365+
fn decode(decoder: &mut D) -> Self {
360366
decoder.tcx().intern_const_alloc(Decodable::decode(decoder))
361367
}
362368
}
@@ -399,7 +405,6 @@ impl_decodable_via_ref! {
399405
&'tcx ty::List<Ty<'tcx>>,
400406
&'tcx ty::List<ty::Binder<'tcx, ty::ExistentialPredicate<'tcx>>>,
401407
&'tcx traits::ImplSource<'tcx, ()>,
402-
&'tcx Allocation,
403408
&'tcx mir::Body<'tcx>,
404409
&'tcx mir::UnsafetyCheckResult,
405410
&'tcx mir::BorrowCheckResult<'tcx>,

‎compiler/rustc_middle/src/ty/context.rs‎

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
77
use crate::lint::{struct_lint_level, LintDiagnosticBuilder, LintLevelSource};
88
use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath};
99
use crate::middle::stability;
10-
use crate::mir::interpret::{self, Allocation, ConstValue, Scalar};
10+
use crate::mir::interpret::{self, Allocation, ConstAllocation, ConstValue, Scalar};
1111
use crate::mir::{
1212
Body, BorrowCheckResult, Field, Local, Place, PlaceElem, ProjectionKind, Promoted,
1313
};
@@ -1653,22 +1653,6 @@ pub trait Lift<'tcx>: fmt::Debug {
16531653
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted>;
16541654
}
16551655

1656-
// Deprecated: we are in the process of converting all uses to `nop_lift`.
1657-
macro_rules! nop_lift_old {
1658-
($set:ident; $ty:ty => $lifted:ty) => {
1659-
impl<'a, 'tcx> Lift<'tcx> for $ty {
1660-
type Lifted = $lifted;
1661-
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
1662-
if tcx.interners.$set.contains_pointer_to(&InternedInSet(self)) {
1663-
Some(unsafe { mem::transmute(self) })
1664-
} else {
1665-
None
1666-
}
1667-
}
1668-
}
1669-
};
1670-
}
1671-
16721656
macro_rules! nop_lift {
16731657
($set:ident; $ty:ty => $lifted:ty) => {
16741658
impl<'a, 'tcx> Lift<'tcx> for $ty {
@@ -1726,7 +1710,7 @@ macro_rules! nop_list_lift {
17261710
nop_lift! {type_; Ty<'a> => Ty<'tcx>}
17271711
nop_lift! {region; Region<'a> => Region<'tcx>}
17281712
nop_lift! {const_; Const<'a> => Const<'tcx>}
1729-
nop_lift_old! {const_allocation; &'a Allocation => &'tcx Allocation}
1713+
nop_lift! {const_allocation; ConstAllocation<'a> => ConstAllocation<'tcx>}
17301714
nop_lift! {predicate; Predicate<'a> => Predicate<'tcx>}
17311715

17321716
nop_list_lift! {poly_existential_predicates; ty::Binder<'a, ExistentialPredicate<'a>> => ty::Binder<'tcx, ExistentialPredicate<'tcx>>}
@@ -2161,6 +2145,7 @@ macro_rules! direct_interners {
21612145
direct_interners! {
21622146
region: mk_region(RegionKind): Region -> Region<'tcx>,
21632147
const_: mk_const(ConstS<'tcx>): Const -> Const<'tcx>,
2148+
const_allocation: intern_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>,
21642149
}
21652150

21662151
macro_rules! direct_interners_old {
@@ -2201,7 +2186,6 @@ macro_rules! direct_interners_old {
22012186

22022187
// FIXME: eventually these should all be converted to `direct_interners`.
22032188
direct_interners_old! {
2204-
const_allocation: intern_const_alloc(Allocation),
22052189
layout: intern_layout(Layout),
22062190
adt_def: intern_adt_def(AdtDef),
22072191
}

‎compiler/rustc_middle/src/ty/print/pretty.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ pub trait PrettyPrinter<'tcx>:
12671267
Some(GlobalAlloc::Memory(alloc)) => {
12681268
let len = int.assert_bits(self.tcx().data_layout.pointer_size);
12691269
let range = AllocRange { start: offset, size: Size::from_bytes(len) };
1270-
if let Ok(byte_str) = alloc.get_bytes(&self.tcx(), range) {
1270+
if let Ok(byte_str) = alloc.inner().get_bytes(&self.tcx(), range) {
12711271
p!(pretty_print_byte_str(byte_str))
12721272
} else {
12731273
p!("<too short allocation>")
@@ -1424,7 +1424,8 @@ pub trait PrettyPrinter<'tcx>:
14241424
// The `inspect` here is okay since we checked the bounds, and there are
14251425
// no relocations (we have an active slice reference here). We don't use
14261426
// this result to affect interpreter execution.
1427-
let byte_str = data.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
1427+
let byte_str =
1428+
data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end);
14281429
self.pretty_print_byte_str(byte_str)
14291430
}
14301431
(
@@ -1434,7 +1435,8 @@ pub trait PrettyPrinter<'tcx>:
14341435
// The `inspect` here is okay since we checked the bounds, and there are no
14351436
// relocations (we have an active `str` reference here). We don't use this
14361437
// result to affect interpreter execution.
1437-
let slice = data.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
1438+
let slice =
1439+
data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end);
14381440
p!(write("{:?}", String::from_utf8_lossy(slice)));
14391441
Ok(self)
14401442
}
@@ -1443,7 +1445,7 @@ pub trait PrettyPrinter<'tcx>:
14431445
// cast is ok because we already checked for pointer size (32 or 64 bit) above
14441446
let range = AllocRange { start: offset, size: Size::from_bytes(n) };
14451447

1446-
let byte_str = alloc.get_bytes(&self.tcx(), range).unwrap();
1448+
let byte_str = alloc.inner().get_bytes(&self.tcx(), range).unwrap();
14471449
p!("*");
14481450
p!(pretty_print_byte_str(byte_str));
14491451
Ok(self)

‎compiler/rustc_mir_transform/src/const_prop.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ use rustc_trait_selection::traits;
3131
use crate::MirPass;
3232
use rustc_const_eval::const_eval::ConstEvalErr;
3333
use rustc_const_eval::interpret::{
34-
self, compile_time_machine, AllocId, Allocation, ConstValue, CtfeValidationMode, Frame, ImmTy,
35-
Immediate, InterpCx, InterpResult, LocalState, LocalValue, MemPlace, MemoryKind, OpTy,
34+
self, compile_time_machine, AllocId, ConstAllocation, ConstValue, CtfeValidationMode, Frame,
35+
ImmTy, Immediate, InterpCx, InterpResult, LocalState, LocalValue, MemPlace, MemoryKind, OpTy,
3636
Operand as InterpOperand, PlaceTy, Scalar, ScalarMaybeUninit, StackPopCleanup, StackPopUnwind,
3737
};
3838

@@ -274,7 +274,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
274274
fn before_access_global(
275275
_memory_extra: &(),
276276
_alloc_id: AllocId,
277-
allocation: &Allocation<Self::PointerTag, Self::AllocExtra>,
277+
alloc: ConstAllocation<'tcx, Self::PointerTag, Self::AllocExtra>,
278278
_static_def_id: Option<DefId>,
279279
is_write: bool,
280280
) -> InterpResult<'tcx> {
@@ -283,7 +283,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
283283
}
284284
// If the static allocation is mutable, then we can't const prop it as its content
285285
// might be different at runtime.
286-
if allocation.mutability == Mutability::Mut {
286+
if alloc.inner().mutability == Mutability::Mut {
287287
throw_machine_stop_str!("can't access mutable globals in ConstProp");
288288
}
289289

‎compiler/rustc_monomorphize/src/collector.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ fn collect_items_rec<'tcx>(
401401
recursion_depth_reset = None;
402402

403403
if let Ok(alloc) = tcx.eval_static_initializer(def_id) {
404-
for &id in alloc.relocations().values() {
404+
for &id in alloc.inner().relocations().values() {
405405
collect_miri(tcx, id, &mut neighbors);
406406
}
407407
}
@@ -1370,7 +1370,7 @@ fn collect_miri<'tcx>(
13701370
}
13711371
GlobalAlloc::Memory(alloc) => {
13721372
trace!("collecting {:?} with {:#?}", alloc_id, alloc);
1373-
for &inner in alloc.relocations().values() {
1373+
for &inner in alloc.inner().relocations().values() {
13741374
rustc_data_structures::stack::ensure_sufficient_stack(|| {
13751375
collect_miri(tcx, inner, output);
13761376
});
@@ -1405,7 +1405,7 @@ fn collect_const_value<'tcx>(
14051405
match value {
14061406
ConstValue::Scalar(Scalar::Ptr(ptr, _size)) => collect_miri(tcx, ptr.provenance, output),
14071407
ConstValue::Slice { data: alloc, start: _, end: _ } | ConstValue::ByRef { alloc, .. } => {
1408-
for &id in alloc.relocations().values() {
1408+
for &id in alloc.inner().relocations().values() {
14091409
collect_miri(tcx, id, output);
14101410
}
14111411
}

‎compiler/rustc_symbol_mangling/src/v0.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,9 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
633633
// The `inspect` here is okay since we checked the bounds, and there are no
634634
// relocations (we have an active `str` reference here). We don't use this
635635
// result to affect interpreter execution.
636-
let slice =
637-
data.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
636+
let slice = data
637+
.inner()
638+
.inspect_with_uninit_and_ptr_outside_interpreter(start..end);
638639
let s = std::str::from_utf8(slice).expect("non utf8 str from miri");
639640

640641
self.push("e");

‎compiler/rustc_typeck/src/check/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId, span: S
553553
// the consumer's responsibility to ensure all bytes that have been read
554554
// have defined values.
555555
if let Ok(alloc) = tcx.eval_static_initializer(id.to_def_id()) {
556-
if alloc.relocations().len() != 0 {
556+
if alloc.inner().relocations().len() != 0 {
557557
let msg = "statics with a custom `#[link_section]` must be a \
558558
simple list of bytes on the wasm target with no \
559559
extra levels of indirection such as references";

‎src/tools/clippy/clippy_utils/src/consts.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ pub fn miri_to_const(result: ty::Const<'_>) -> Option<Constant> {
593593
ty::ConstKind::Value(ConstValue::Slice { data, start, end }) => match result.ty().kind() {
594594
ty::Ref(_, tam, _) => match tam.kind() {
595595
ty::Str => String::from_utf8(
596-
data.inspect_with_uninit_and_ptr_outside_interpreter(start..end)
596+
data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end)
597597
.to_owned(),
598598
)
599599
.ok()
@@ -605,7 +605,7 @@ pub fn miri_to_const(result: ty::Const<'_>) -> Option<Constant> {
605605
ty::ConstKind::Value(ConstValue::ByRef { alloc, offset: _ }) => match result.ty().kind() {
606606
ty::Array(sub_type, len) => match sub_type.kind() {
607607
ty::Float(FloatTy::F32) => match miri_to_const(*len) {
608-
Some(Constant::Int(len)) => alloc
608+
Some(Constant::Int(len)) => alloc.inner()
609609
.inspect_with_uninit_and_ptr_outside_interpreter(0..(4 * len as usize))
610610
.to_owned()
611611
.chunks(4)
@@ -619,7 +619,7 @@ pub fn miri_to_const(result: ty::Const<'_>) -> Option<Constant> {
619619
_ => None,
620620
},
621621
ty::Float(FloatTy::F64) => match miri_to_const(*len) {
622-
Some(Constant::Int(len)) => alloc
622+
Some(Constant::Int(len)) => alloc.inner()
623623
.inspect_with_uninit_and_ptr_outside_interpreter(0..(8 * len as usize))
624624
.to_owned()
625625
.chunks(8)

0 commit comments

Comments
 (0)
Please sign in to comment.