Skip to content

Commit 152001f

Browse files
committed
make it more clear which functions create fresh AllocId
1 parent dd45abe commit 152001f

File tree

17 files changed

+31
-33
lines changed

17 files changed

+31
-33
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ fn pointer_for_allocation<'tcx>(
259259
fx: &mut FunctionCx<'_, '_, 'tcx>,
260260
alloc: ConstAllocation<'tcx>,
261261
) -> crate::pointer::Pointer {
262-
let alloc_id = fx.tcx.create_memory_alloc(alloc);
262+
let alloc_id = fx.tcx.reserve_and_set_memory_alloc(alloc);
263263
let data_id = data_id_for_alloc_id(
264264
&mut fx.constants_cx,
265265
&mut *fx.module,

compiler/rustc_const_eval/src/interpret/cast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8484
)
8585
.ok_or_else(|| err_inval!(TooGeneric))?;
8686

87-
let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
87+
let fn_ptr = self.fn_ptr(FnVal::Instance(instance));
8888
self.write_pointer(fn_ptr, dest)?;
8989
}
9090
_ => span_bug!(self.cur_span(), "reify fn pointer on {:?}", src.layout.ty),
@@ -116,7 +116,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
116116
ty::ClosureKind::FnOnce,
117117
)
118118
.ok_or_else(|| err_inval!(TooGeneric))?;
119-
let fn_ptr = self.create_fn_alloc_ptr(FnVal::Instance(instance));
119+
let fn_ptr = self.fn_ptr(FnVal::Instance(instance));
120120
self.write_pointer(fn_ptr, dest)?;
121121
}
122122
_ => span_bug!(self.cur_span(), "closure fn pointer on {:?}", src.layout.ty),

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
5757
let path = crate::util::type_name(tcx, tp_ty);
5858
let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes());
5959
let alloc = tcx.mk_const_alloc(alloc);
60-
let alloc_id = tcx.create_memory_alloc(alloc);
60+
let alloc_id = tcx.reserve_and_set_memory_alloc(alloc);
6161
ConstValue::Slice { alloc_id: Some(alloc_id), start: 0, end: alloc.inner().len() }
6262
}
6363
sym::needs_drop => {

compiler/rustc_const_eval/src/interpret/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
552552
def_id: DefId,
553553
) -> InterpResult<$tcx, Pointer> {
554554
// Use the `AllocId` associated with the `DefId`. Any actual *access* will fail.
555-
Ok(Pointer::new(ecx.tcx.create_static_alloc(def_id), Size::ZERO))
555+
Ok(Pointer::new(ecx.tcx.reserve_and_set_static_alloc(def_id), Size::ZERO))
556556
}
557557

558558
#[inline(always)]

compiler/rustc_const_eval/src/interpret/memory.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
176176
M::adjust_alloc_base_pointer(self, ptr)
177177
}
178178

179-
pub fn create_fn_alloc_ptr(
180-
&mut self,
181-
fn_val: FnVal<'tcx, M::ExtraFnVal>,
182-
) -> Pointer<M::Provenance> {
179+
pub fn fn_ptr(&mut self, fn_val: FnVal<'tcx, M::ExtraFnVal>) -> Pointer<M::Provenance> {
183180
let id = match fn_val {
184-
FnVal::Instance(instance) => self.tcx.create_fn_alloc(instance),
181+
FnVal::Instance(instance) => self.tcx.reserve_and_set_fn_alloc(instance),
185182
FnVal::Other(extra) => {
186183
// FIXME(RalfJung): Should we have a cache here?
187184
let id = self.tcx.reserve_alloc_id();

compiler/rustc_const_eval/src/interpret/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2727
ensure_monomorphic_enough(*self.tcx, ty)?;
2828
ensure_monomorphic_enough(*self.tcx, poly_trait_ref)?;
2929

30-
let vtable_symbolic_allocation = self.tcx.create_vtable_alloc(ty, poly_trait_ref);
30+
let vtable_symbolic_allocation = self.tcx.reserve_and_set_vtable_alloc(ty, poly_trait_ref);
3131
let vtable_ptr = self.global_base_pointer(Pointer::from(vtable_symbolic_allocation))?;
3232
Ok(vtable_ptr.into())
3333
}

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ impl<'s> AllocDecodingSession<'s> {
389389
trace!("creating fn alloc ID");
390390
let instance = ty::Instance::decode(decoder);
391391
trace!("decoded fn alloc instance: {:?}", instance);
392-
let alloc_id = decoder.interner().create_fn_alloc(instance);
392+
let alloc_id = decoder.interner().reserve_and_set_fn_alloc(instance);
393393
alloc_id
394394
}
395395
AllocDiscriminant::VTable => {
@@ -399,15 +399,16 @@ impl<'s> AllocDecodingSession<'s> {
399399
let poly_trait_ref =
400400
<Option<ty::PolyExistentialTraitRef<'_>> as Decodable<D>>::decode(decoder);
401401
trace!("decoded vtable alloc instance: {ty:?}, {poly_trait_ref:?}");
402-
let alloc_id = decoder.interner().create_vtable_alloc(ty, poly_trait_ref);
402+
let alloc_id =
403+
decoder.interner().reserve_and_set_vtable_alloc(ty, poly_trait_ref);
403404
alloc_id
404405
}
405406
AllocDiscriminant::Static => {
406407
assert!(alloc_id.is_none());
407408
trace!("creating extern static alloc ID");
408409
let did = <DefId as Decodable<D>>::decode(decoder);
409410
trace!("decoded static def-ID: {:?}", did);
410-
let alloc_id = decoder.interner().create_static_alloc(did);
411+
let alloc_id = decoder.interner().reserve_and_set_static_alloc(did);
411412
alloc_id
412413
}
413414
}
@@ -544,13 +545,13 @@ impl<'tcx> TyCtxt<'tcx> {
544545

545546
/// Generates an `AllocId` for a static or return a cached one in case this function has been
546547
/// called on the same static before.
547-
pub fn create_static_alloc(self, static_id: DefId) -> AllocId {
548+
pub fn reserve_and_set_static_alloc(self, static_id: DefId) -> AllocId {
548549
self.reserve_and_set_dedup(GlobalAlloc::Static(static_id))
549550
}
550551

551552
/// Generates an `AllocId` for a function. Depending on the function type,
552553
/// this might get deduplicated or assigned a new ID each time.
553-
pub fn create_fn_alloc(self, instance: Instance<'tcx>) -> AllocId {
554+
pub fn reserve_and_set_fn_alloc(self, instance: Instance<'tcx>) -> AllocId {
554555
// Functions cannot be identified by pointers, as asm-equal functions can get deduplicated
555556
// by the linker (we set the "unnamed_addr" attribute for LLVM) and functions can be
556557
// duplicated across crates.
@@ -575,7 +576,7 @@ impl<'tcx> TyCtxt<'tcx> {
575576
}
576577

577578
/// Generates an `AllocId` for a (symbolic, not-reified) vtable. Will get deduplicated.
578-
pub fn create_vtable_alloc(
579+
pub fn reserve_and_set_vtable_alloc(
579580
self,
580581
ty: Ty<'tcx>,
581582
poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
@@ -588,7 +589,7 @@ impl<'tcx> TyCtxt<'tcx> {
588589
/// Statics with identical content will still point to the same `Allocation`, i.e.,
589590
/// their data will be deduplicated through `Allocation` interning -- but they
590591
/// are different places in memory and as such need different IDs.
591-
pub fn create_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
592+
pub fn reserve_and_set_memory_alloc(self, mem: ConstAllocation<'tcx>) -> AllocId {
592593
let id = self.reserve_alloc_id();
593594
self.set_alloc_id_memory(id, mem);
594595
id

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ impl<'tcx> TyCtxt<'tcx> {
647647
// Create an allocation that just contains these bytes.
648648
let alloc = interpret::Allocation::from_bytes_byte_aligned_immutable(bytes);
649649
let alloc = self.mk_const_alloc(alloc);
650-
self.create_memory_alloc(alloc)
650+
self.reserve_and_set_memory_alloc(alloc)
651651
}
652652

653653
/// Returns a range of the start/end indices specified with the

compiler/rustc_middle/src/ty/vtable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
8484
let scalar = match entry {
8585
VtblEntry::MetadataDropInPlace => {
8686
let instance = ty::Instance::resolve_drop_in_place(tcx, ty);
87-
let fn_alloc_id = tcx.create_fn_alloc(instance);
87+
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance);
8888
let fn_ptr = Pointer::from(fn_alloc_id);
8989
Scalar::from_pointer(fn_ptr, &tcx)
9090
}
@@ -94,7 +94,7 @@ pub(super) fn vtable_allocation_provider<'tcx>(
9494
VtblEntry::Method(instance) => {
9595
// Prepare the fn ptr we write into the vtable.
9696
let instance = instance.polymorphize(tcx);
97-
let fn_alloc_id = tcx.create_fn_alloc(instance);
97+
let fn_alloc_id = tcx.reserve_and_set_fn_alloc(instance);
9898
let fn_ptr = Pointer::from(fn_alloc_id);
9999
Scalar::from_pointer(fn_ptr, &tcx)
100100
}
@@ -112,5 +112,5 @@ pub(super) fn vtable_allocation_provider<'tcx>(
112112
}
113113

114114
vtable.mutability = Mutability::Not;
115-
tcx.create_memory_alloc(tcx.mk_const_alloc(vtable))
115+
tcx.reserve_and_set_memory_alloc(tcx.mk_const_alloc(vtable))
116116
}

compiler/rustc_mir_build/src/build/expr/as_constant.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ fn lit_to_mir_constant<'tcx>(
133133
let s = s.as_str();
134134
let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes());
135135
let allocation = tcx.mk_const_alloc(allocation);
136-
let alloc_id = tcx.create_memory_alloc(allocation);
136+
let alloc_id = tcx.reserve_and_set_memory_alloc(allocation);
137137
ConstValue::Slice { alloc_id: Some(alloc_id), start: 0, end: s.len() }
138138
}
139139
(ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _))
140140
if matches!(inner_ty.kind(), ty::Slice(_)) =>
141141
{
142142
let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
143143
let allocation = tcx.mk_const_alloc(allocation);
144-
let alloc_id = tcx.create_memory_alloc(allocation);
144+
let alloc_id = tcx.reserve_and_set_memory_alloc(allocation);
145145
ConstValue::Slice { alloc_id: Some(alloc_id), start: 0, end: data.len() }
146146
}
147147
(ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
@@ -152,7 +152,7 @@ fn lit_to_mir_constant<'tcx>(
152152
{
153153
let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
154154
let allocation = tcx.mk_const_alloc(allocation);
155-
let alloc_id = tcx.create_memory_alloc(allocation);
155+
let alloc_id = tcx.reserve_and_set_memory_alloc(allocation);
156156
ConstValue::Slice { alloc_id: Some(alloc_id), start: 0, end: data.len() }
157157
}
158158
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {

compiler/rustc_mir_build/src/thir/cx/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ impl<'tcx> Cx<'tcx> {
950950
let kind = if self.tcx.is_thread_local_static(id) {
951951
ExprKind::ThreadLocalRef(id)
952952
} else {
953-
let alloc_id = self.tcx.create_static_alloc(id);
953+
let alloc_id = self.tcx.reserve_and_set_static_alloc(id);
954954
ExprKind::StaticRef { alloc_id, ty, def_id: id }
955955
};
956956
ExprKind::Deref {

compiler/rustc_mir_transform/src/large_enums.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl EnumSizeOpt {
114114
tcx.data_layout.ptr_sized_integer().align(&tcx.data_layout).abi,
115115
Mutability::Not,
116116
);
117-
let alloc = tcx.create_memory_alloc(tcx.mk_const_alloc(alloc));
117+
let alloc = tcx.reserve_and_set_memory_alloc(tcx.mk_const_alloc(alloc));
118118
Some((*adt_def, num_discrs, *alloc_cache.entry(ty).or_insert(alloc)))
119119
}
120120
fn optim<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

src/tools/miri/src/eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
382382
.unwrap()
383383
.unwrap();
384384

385-
let main_ptr = ecx.create_fn_alloc_ptr(FnVal::Instance(entry_instance));
385+
let main_ptr = ecx.fn_ptr(FnVal::Instance(entry_instance));
386386

387387
// Inlining of `DEFAULT` from
388388
// https://github.com/rust-lang/rust/blob/master/compiler/rustc_session/src/config/sigpipe.rs.

src/tools/miri/src/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
711711
let layout = this.machine.layouts.const_raw_ptr;
712712
let dlsym = Dlsym::from_str("signal".as_bytes(), &this.tcx.sess.target.os)?
713713
.expect("`signal` must be an actual dlsym on android");
714-
let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym));
714+
let ptr = this.fn_ptr(FnVal::Other(dlsym));
715715
let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, this), layout);
716716
Self::alloc_extern_static(this, "signal", val)?;
717717
// A couple zero-initialized pointer-sized extern statics.

src/tools/miri/src/shims/backtrace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6363
// to reconstruct the needed frame information in `handle_miri_resolve_frame`.
6464
// Note that we never actually read or write anything from/to this pointer -
6565
// all of the data is represented by the pointer value itself.
66-
let fn_ptr = this.create_fn_alloc_ptr(FnVal::Instance(instance));
66+
let fn_ptr = this.fn_ptr(FnVal::Instance(instance));
6767
fn_ptr.wrapping_offset(Size::from_bytes(pos.0), this)
6868
})
6969
.collect();
@@ -159,7 +159,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
159159

160160
// Reconstruct the original function pointer,
161161
// which we pass to user code.
162-
let fn_ptr = this.create_fn_alloc_ptr(FnVal::Instance(fn_instance));
162+
let fn_ptr = this.fn_ptr(FnVal::Instance(fn_instance));
163163

164164
let num_fields = dest.layout.fields.count();
165165

src/tools/miri/src/shims/unix/foreign_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
232232
let symbol = this.read_pointer(symbol)?;
233233
let symbol_name = this.read_c_str(symbol)?;
234234
if let Some(dlsym) = Dlsym::from_str(symbol_name, &this.tcx.sess.target.os)? {
235-
let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym));
235+
let ptr = this.fn_ptr(FnVal::Other(dlsym));
236236
this.write_pointer(ptr, dest)?;
237237
} else {
238238
this.write_null(dest)?;

src/tools/miri/src/shims/windows/foreign_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
335335
this.read_target_isize(hModule)?;
336336
let name = this.read_c_str(this.read_pointer(lpProcName)?)?;
337337
if let Some(dlsym) = Dlsym::from_str(name, &this.tcx.sess.target.os)? {
338-
let ptr = this.create_fn_alloc_ptr(FnVal::Other(dlsym));
338+
let ptr = this.fn_ptr(FnVal::Other(dlsym));
339339
this.write_pointer(ptr, dest)?;
340340
} else {
341341
this.write_null(dest)?;

0 commit comments

Comments
 (0)