Skip to content

Commit 7965a8c

Browse files
committed
Remove MemoryKind check in allocate_str
1 parent d7724f9 commit 7965a8c

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

compiler/rustc_const_eval/src/interpret/place.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@ where
995995
}
996996

997997
/// Returns a wide MPlace of type `str` to a new 1-aligned allocation.
998+
/// Immutable strings are deduplicated and stored in global memory.
998999
pub fn allocate_str(
9991000
&mut self,
10001001
str: &str,
@@ -1004,11 +1005,9 @@ where
10041005
let tcx = self.tcx.tcx;
10051006

10061007
// Use cache for immutable strings.
1007-
let ptr = if mutbl.is_not()
1008-
&& matches!(kind, MemoryKind::CallerLocation | MemoryKind::Machine(_))
1009-
{
1010-
// Same id for cached allocations.
1011-
let id = tcx.allocate_bytes(str.as_bytes());
1008+
let ptr = if mutbl.is_not() {
1009+
// Use dedup'd allocation function.
1010+
let id = tcx.allocate_bytes_dedup(str.as_bytes());
10121011

10131012
// Turn untagged "global" pointers (obtained via `tcx`) into the machine pointer to the allocation.
10141013
M::adjust_alloc_root_pointer(&self, Pointer::from(id), Some(kind))?

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

+5
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ impl<'tcx> TyCtxt<'tcx> {
434434
/// Reserves a new ID *if* this allocation has not been dedup-reserved before.
435435
fn reserve_and_set_dedup(self, alloc: GlobalAlloc<'tcx>) -> AllocId {
436436
let mut alloc_map = self.alloc_map.lock();
437+
if let GlobalAlloc::Memory(mem) = alloc {
438+
if mem.inner().mutability.is_mut() {
439+
bug!("trying to dedup-reserve mutable memory");
440+
}
441+
}
437442
if let Some(&alloc_id) = alloc_map.dedup.get(&alloc) {
438443
return alloc_id;
439444
}

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ impl<'tcx> TyCtxt<'tcx> {
14431443

14441444
/// Allocates a read-only byte or string literal for `mir::interpret`.
14451445
/// Returns the same `AllocId` if called again with the same bytes.
1446-
pub fn allocate_bytes(self, bytes: &[u8]) -> interpret::AllocId {
1446+
pub fn allocate_bytes_dedup(self, bytes: &[u8]) -> interpret::AllocId {
14471447
// Create an allocation that just contains these bytes.
14481448
let alloc = interpret::Allocation::from_bytes_byte_aligned_immutable(bytes);
14491449
let alloc = self.mk_const_alloc(alloc);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn lit_to_mir_constant<'tcx>(
140140
ConstValue::Slice { data: allocation, meta: allocation.inner().size().bytes() }
141141
}
142142
(ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
143-
let id = tcx.allocate_bytes(data);
143+
let id = tcx.allocate_bytes_dedup(data);
144144
ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
145145
}
146146
(ast::LitKind::CStr(data, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::CStr)) =>

0 commit comments

Comments
 (0)