Skip to content

Tracking Issue for const_heap #79597

@oli-obk

Description

@oli-obk
Contributor

This is a tracking issue for rust-lang/const-eval#20
The feature gate for the issue is #![feature(const_heap)].
The general design can be found here

About tracking issues

Tracking issues are used to record the overall progress of implementation.
They are also used as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.

Steps

Unresolved Questions

  • Do we want to allow the regular Global allocator? This would give us post-monomorpization errors. More details can be found in https://hackmd.io/h2O2vkj3RimrBTfm9hvZWA#Transient-heap-allocations-via-Global
  • If we stabilize this, we fundamentally change the nature of const fn: before this change, a const fn() -> T would inevitably have to always return the same value; after this change, that is no longer the case. With ptr_guaranteed_eq, this can even to some extend be observed during CTFE. So in that sense, this feature is much bigger than anything we ever stabilized for CTFE.
  • We can already emulate all this via build.rs scripts or proc macros that generate the appropriate constants/statics and references them. Const heap will "just" make generating static/const allocations more convenient.
  • Once we permit references to things that (can) contain interior mutability, it would be possible for users to write a const fn foo() -> &'static Cell<i32>, which could then be used to initialize a const FOO: &'static Cell<i32> which is unsound (and only caught by dynamic checks). The tracking issue for interior mutability behind references is Tracking Issue for const_refs_to_cell #80384
  • const_heap feature can be used to leak mutable memory into final value of constant #129233
  • ICEs when making self-referential values: dealing with cyclic (self-referential) values with const_heap #143869
  • Whether or not const_make_global can be called twice on the same allocation.

Implementation history

Sub-issues

Sub-issues

1 of 2 Issues completed

Activity

added
T-langRelevant to the language team
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
on Dec 1, 2020
RalfJung

RalfJung commented on Dec 1, 2020

@RalfJung
Member

As discussed here, this feature makes const fn observably impure: let x = f(); (x, x) is no longer always equivalent to (f(), f()). We are at a crossroads for the language here: if we want to support heap allocations in const fn, we have to either introduce some kind of effect system or say goodbye to the idea that const fn denotes a form of purity that would be useful to unsafe code.

added
Libs-TrackedLibs issues that are tracked on the team's project board.
on Dec 16, 2020
RalfJung

RalfJung commented on Dec 19, 2020

@RalfJung
Member

Btw, given how far-reaching the consequences of this feature are, I wonder if doing this without an RFC is really appropriate. But it could make sense to experiment some more with what can be done for transient vs. non-transient allocations, and how Global comes in, so that we have a better idea of the design space.

@oli-obk something is wrong with "implementation history", it links to the same PR twice.

oli-obk

oli-obk commented on Dec 20, 2020

@oli-obk
ContributorAuthor

The experimentation that @vn-ki has been doing has exposed various problems that aren't even related to heap stuff itself, but are prerequisites. I believe we should keep on doing this experimentation, just to find out what all the problems are and how a possible implementation could look. Doing this in the abstract has not allowed us to realize what the dependency graph actually is and I will be updating the skill tree to reflect that as we gain more information. Some examples off the top of my head:

  • generic types are considered having interior mutability within constants and thus forbid short lived immutable references to them
  • trait bounds on methods only work if the bounds are on the method and not if they are on the containing impl block
  • the existing alloc code needs support for closures or at least impl const Fn for const fn()
  • just needs an impl: use fallible allocations for Allocation to make sure that we report an error instead of crashing rustc with OOM (this can be done on stable via the use of large arrays)
RalfJung

RalfJung commented on Dec 20, 2020

@RalfJung
Member

The first three points don't seem really related to heap allocations though?

oli-obk

oli-obk commented on Dec 20, 2020

@oli-obk
ContributorAuthor

They are not, they are prerequisites for Box::new_in and whatever things are called by that.

RalfJung

RalfJung commented on Dec 21, 2020

@RalfJung
Member

generic types are considered having interior mutability within constants and thus forbid short lived immutable references to them

Is there a separate issue for this? I think this is closely related to some of the other issues that came up over the last year -- where we have a restriction that applies to the final value of all consts, but be unnecessarily apply it even during the course of the computation -- since we have no good way to otherwise ensure that the final value is okay. &mut references have basically the same problem.

oli-obk

oli-obk commented on Dec 21, 2020

@oli-obk
ContributorAuthor

There is not. I have a local branch that generalizes to all such checks, so we'll be able to just check them on the final value

RalfJung

RalfJung commented on Dec 21, 2020

@RalfJung
Member

For associated consts, I assume that's a post-monomorphization error then?

(I know this is off-topic here, that's why I asked if there is a better place.^^)

12 remaining items

added a commit that references this issue on Jan 29, 2022
added
needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.
on Jul 20, 2022
ghost
added
F-raw_dylib`#![feature(raw_dylib)]`
F-const_heap`#[feature(const_heap)]`
and removed
F-raw_dylib`#![feature(raw_dylib)]`
on Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-allocatorsArea: Custom and system allocatorsA-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCF-const_heap`#[feature(const_heap)]`Libs-TrackedLibs issues that are tracked on the team's project board.S-tracking-impl-incompleteStatus: The implementation is incomplete.T-langRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.needs-rfcThis change is large or controversial enough that it should have an RFC accepted before doing it.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @joshtriplett@RalfJung@oli-obk@petrochenkov@arnabanimesh

        Issue actions

          Tracking Issue for `const_heap` · Issue #79597 · rust-lang/rust