about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src
diff options
context:
space:
mode:
authorwoppopo <woppopo@protonmail.com>2022-01-26 13:06:09 +0900
committerwoppopo <woppopo@protonmail.com>2022-01-26 13:06:09 +0900
commit29932db09bb985072dc68d9d4e4acab09e69562a (patch)
tree6f0e8d2b0a52aabfc67996bc5ed4fe0ef9bd162b /compiler/rustc_const_eval/src
parentaa6795e2d4cdae6af2ea854744c3bec2eb30d16e (diff)
downloadrust-29932db09bb985072dc68d9d4e4acab09e69562a.tar.gz
rust-29932db09bb985072dc68d9d4e4acab09e69562a.zip
`const_deallocate`: Don't deallocate memory allocated in an another const. Does nothing at runtime.
`const_allocate`:  Returns a null pointer at runtime.
Diffstat (limited to 'compiler/rustc_const_eval/src')
-rw-r--r--compiler/rustc_const_eval/src/const_eval/machine.rs20
1 files changed, 15 insertions, 5 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/machine.rs b/compiler/rustc_const_eval/src/const_eval/machine.rs
index 62eaf333340..89717b75f12 100644
--- a/compiler/rustc_const_eval/src/const_eval/machine.rs
+++ b/compiler/rustc_const_eval/src/const_eval/machine.rs
@@ -358,11 +358,21 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
                     Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
                 };
 
-                ecx.memory.deallocate(
-                    ptr,
-                    Some((size, align)),
-                    interpret::MemoryKind::Machine(MemoryKind::Heap),
-                )?;
+                // If an allocation is created in an another const,
+                // we don't deallocate it.
+                let (alloc_id, _, _) = ecx.memory.ptr_get_alloc(ptr)?;
+                let is_allocated_in_another_const = matches!(
+                    ecx.tcx.get_global_alloc(alloc_id),
+                    Some(interpret::GlobalAlloc::Memory(_))
+                );
+
+                if !is_allocated_in_another_const {
+                    ecx.memory.deallocate(
+                        ptr,
+                        Some((size, align)),
+                        interpret::MemoryKind::Machine(MemoryKind::Heap),
+                    )?;
+                }
             }
             _ => {
                 return Err(ConstEvalErrKind::NeedsRfc(format!(