dealing with cyclic (self-referential) values with const_heap
Open
Nobody has claimed this yet.
A-const-eval
C-bug
F-const_heap
WG-const-eval
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
See the following snippet: (currently ICEs)
#![feature(core_intrinsics)]
#![feature(const_heap)]
use std::mem::{align_of, size_of};
use std::intrinsics;
struct SelfReferential {
me: *const SelfReferential,
}
const Y: &'static SelfReferential = unsafe {
let size = size_of::<SelfReferential>();
let align = align_of::<SelfReferential>();
let ptr_raw = intrinsics::const_allocate(size, align);
let ptr = ptr_raw as *mut SelfReferential;
let me_addr = &raw mut (*ptr).me;
*me_addr = ptr;
intrinsics::const_make_global(ptr_raw, size, align);
&*ptr
};
fn main() {}
me's provenance says it wasn't derived from an immutable reference. We can't really change that because it can't be mutated after we callconst_make_global.- The fact above hits "accepted a mutable pointer that should not have accepted" at https://github.com/rust-lang/rust/blob/040e2f8b9ff2d76fbe2146d6003e297ed4532088/compiler/rustc_const_eval/src/interpret/intern.rs#L247-L265
- We can check if
alloc_idhas kindHeap { was_made_immut: true }and skip the error that way. intern_shallowwill keep returning alloc2 adding to our todo list being weird left and right. I added a.filterin https://github.com/rust-lang/rust/blob/040e2f8b9ff2d76fbe2146d6003e297ed4532088/compiler/rustc_const_eval/src/interpret/intern.rs#L311-L312 to skip provs that are injust_interned.- Apparently that wasn't enough and rustc hits an infinite loop at some point. The stack trace isn't helpful enough and I didn't have enough time to find a way to debug the stack overflow.
Originally posted by @fee1-dead in https://github.com/rust-lang/rust/pull/143595#discussion_r2192969346
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the const_heap reproducer in the issue and inspect rustc_const_eval/src/interpret/intern.rs at the mutable-pointer check and the intern_shallow filtering around the linked lines. Trace the alloc_id kind, just_interned handling, and todo-list behavior to identify the infinite loop; done means the reproducer no longer ICEs or overflows the stack.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100