-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Vec::push
in consts MVP
#147893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Vec::push
in consts MVP
#147893
Conversation
The job Click to see the possible cause of the failure (guessed by this bot)
|
There was a problem hiding this 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.
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) | ||
} |
There was a problem hiding this comment.
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] { |
There was a problem hiding this comment.
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] { |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
Example:
Oh this is fun...
Global
such that it callsintrinsics::const_allocate
andintrinsics::const_deallocate
during compile time. This is achieved usingconst_eval_select
impl const Allocator for Global
Vec::with_capacity
andVec::push
.Vec::const_leak
to leak the final value viaintrinsics::const_make_global
. If we see any pointer in the final value of aconst
that did not callconst_make_global
, we error as implemented in addconst_make_global
; err forconst_allocate
ptrs if didn't call #143595. (should we name theVec
method const_make_global as well?)r? @rust-lang/wg-const-eval
To-do for me: