Skip to content

Conversation

fee1-dead
Copy link
Member

@fee1-dead fee1-dead commented Oct 19, 2025

Example:

const X: &'static [u32] = {
    let mut v = Vec::with_capacity(6);
    let mut x = 1;
    while x < 42 {
        v.push(x);
        x *= 2;
    }
    assert!(v.len() == 6);
    v.const_leak()
};

assert_eq!([1, 2, 4, 8, 16, 32], X);

Oh this is fun...

  • We split out the implementation of Global such that it calls intrinsics::const_allocate and intrinsics::const_deallocate during compile time. This is achieved using const_eval_select
  • This allows us to impl const Allocator for Global
  • We then constify everything necessary for Vec::with_capacity and Vec::push.
  • Added Vec::const_leak to leak the final value via intrinsics::const_make_global. If we see any pointer in the final value of a const that did not call const_make_global, we error as implemented in add const_make_global; err for const_allocate ptrs if didn't call #143595. (should we name the Vec method const_make_global as well?)

r? @rust-lang/wg-const-eval

To-do for me:

  • Assess the rustdoc impact of additional bounds in the method
  • Increase test coverage

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 20, 2025
@fee1-dead fee1-dead marked this pull request as draft October 20, 2025 00:00
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 20, 2025
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

---- [mir-opt] tests/mir-opt/pre-codegen/drop_boxed_slice.rs stdout ----
25                         }
26                     }
27                     scope 18 (inlined <std::alloc::Global as Allocator>::deallocate) {
-                         let mut _9: *mut u8;
-                         scope 19 (inlined Layout::size) {
-                         }
-                         scope 20 (inlined NonNull::<u8>::as_ptr) {
-                         }
-                         scope 21 (inlined std::alloc::dealloc) {
-                             let mut _10: usize;
---
40                             }
41                         }


thread '[mir-opt] tests/mir-opt/pre-codegen/drop_boxed_slice.rs' panicked at src/tools/compiletest/src/runtest/mir_opt.rs:84:21:
Actual MIR output differs from expected MIR output /checkout/tests/mir-opt/pre-codegen/drop_boxed_slice.generic_in_place.PreCodegen.after.64bit.panic-unwind.mir
stack backtrace:
   5: __rustc::rust_begin_unwind
             at /rustc/bb624dcb4c8ab987e10c0808d92d76f3b84dd117/library/std/src/panicking.rs:698:5
   6: core::panicking::panic_fmt
             at /rustc/bb624dcb4c8ab987e10c0808d92d76f3b84dd117/library/core/src/panicking.rs:75:14

@oli-obk oli-obk assigned oli-obk and unassigned RalfJung Oct 20, 2025
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We used to have the plan to have a dedicated compile-time allocator type... this is an interesting alternative. Given that it also involves the library surface, we should probably involve t-libs-api.

Why have you picked Vec as the first type for this? I think it'd make more sense for Box to go first since that is the most primitive type for heap allocations.

View changes since this review

Comment on lines +377 to +382
let mut offset = 0;
while offset < size {
offset += 1;
// SAFETY: the pointer returned by `const_allocate` is valid to write to.
ptr.add(offset).write(0)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be much more efficient to use write_bytes here.

/// `Vec<T>` created during compile time.
#[unstable(feature = "const_heap", issue = "79597")]
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub const fn const_leak(mut self) -> &'static [T] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is conceptually quite different from leak which IMO should be reflected in the name, so I would indeed prefer const_make_global.

/// `Vec<T>` created during compile time.
#[unstable(feature = "const_heap", issue = "79597")]
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
pub const fn const_leak(mut self) -> &'static [T] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a where T: Freeze for soundness.

}

/// Leaks the `Vec<T>` to be interned statically. This mut be done for all
/// `Vec<T>` created during compile time.
Copy link
Member

@RalfJung RalfJung Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't need to be done for all Vec created during compile-time -- only for those that you want to carry over to runtime. You can use intermediate Vec as scratch space or so during compile time that you never call this method on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants