Skip to content

Tracking issue for const_size_of_val and const_align_of_val #46571

Closed

Description

@Gankra

This is a tracking issue for constness of the following stable API:

// core::mem

pub const fn size_of_val<T: ?Sized>(val: &T) -> usize;
pub const fn align_of_val<T: ?Sized>(val: &T) -> usize;

It would be useful for macros to be able to do things like:

static KEY: [u8; size_of_val($byte_string)] = $byte_string;

I believe the rust-objc devs would like something like this to properly set up statics containing messageSend selectors (which are magically processed by the objc runtime by putting them in the right linker sections).

Activity

  1. Gankra commented on Dec 7, 2017

    @Gankra
    ContributorAuthor

    I believe this is currently blocked on miri becoming the primary/only CTFE provider.

  2. Gankra commented on Dec 7, 2017

    @Gankra
    ContributorAuthor
  3. oli-obk commented on Dec 7, 2017

    @oli-obk
    Contributor

    I think this is possible without miri const eval, but it would be wasteful to implement in old ctfe

  4. Gankra commented on Dec 7, 2017

    @Gankra
    ContributorAuthor

    Yeah I consider "the people who would implement/review this are able but unwilling" to be a blocker

  5. polarathene commented on Apr 27, 2018

    @polarathene

    I believe this is currently blocked on miri becoming the primary/only CTFE provider.

    Where can that status be followed? If such a transition were quite a while out, would it be possible to support with only miri and/or feature flag of some kind?

  6. oli-obk commented on Apr 27, 2018

    @oli-obk
    Contributor

    Miri is here. So it's mostly a matter of opening a PR by copying over the code from https://github.com/solson/miri/blob/master/miri/intrinsic.rs#L501

  7. Gankra commented on May 9, 2018

    @Gankra
    ContributorAuthor

    i'm implementing this

  8. kennytm commented on May 9, 2018

    @kennytm
    Member

    Making these functions const would greatly interfere with custom DST.

    Custom DSTs will need the user to implement some custom functions to derive the size. Thus, semantically, making size_of_val const would mean custom DST now need to depend on the postponed rust-lang/rfcs#2337.

    Implementation-wise, if we want to support size_of_val for a thin CStr, we would need a const strlen, which means Miri would need to support some selected FFI functions (outside wasm), and also need to perform loop and dereference raw pointers (for wasm target).

    Therefore I'm 👎 to making size_of_val and align_of_val const until we have a concrete plan on custom DST. I don't mind having a const fn len() on [T] and str though.

  9. whitequark commented on May 9, 2018

    @whitequark
    Contributor

    we would need a const strlen, which means Miri would need to support some selected FFI functions (outside wasm)

    Why isn't it possible to implement strlen in Rust instead?

  10. oli-obk commented on May 9, 2018

    @oli-obk
    Contributor

    Why isn't it possible to implement strlen in Rust instead?

    This is done for wasm, but as noted, that would require loops, which have an RFC, but it's not been accepted yet: rust-lang/rfcs#2344 and additionally requires branches, which have an accepted RFC, but no implementation yet

  11. kennytm commented on May 9, 2018

    @kennytm
    Member

    It also requires dereferencing a raw pointer and offset it, which probably needs the unsafe guideline.

    pub unsafe fn strlen(mut s: *const c_char) -> usize {
        let mut n = 0;
        while *s != 0 {      // <-- loop & deref
            n += 1;
            s = s.offset(1); // <-- raw pointer offset
        }
        return n
    }
  12. steveklabnik commented on Sep 22, 2019

    @steveklabnik
    Contributor

    Triage: looks like the original PR didn't get merged. We've been making more and more stuff const fn, what's the current status here?

  13. oli-obk commented on Sep 22, 2019

    @oli-obk
    Contributor

    @Gankra #63770 solves your problem, albeit not with size_of_val

  14. 33 remaining items

  15. RalfJung commented on Nov 27, 2024

    @RalfJung
    Member

    @davidtwco so just to be sure, you are fine with moving ahead here with const stabilization, and do not think this will adversely affect the future of your RFC or the entire unsized types / extern type / only-runtime-sized type situation?

  16. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    and removed on Dec 1, 2024
  17. davidtwco commented on Dec 2, 2024

    @davidtwco
    Member

    @davidtwco so just to be sure, you are fine with moving ahead here with const stabilization, and do not think this will adversely affect the future of your RFC or the entire unsized types / extern type / only-runtime-sized type situation?

    I think it should be okay for this to proceed. As long as this can be changed to ~const ?Sized backwards compatibly, and I believe it can be, then it wouldn't interfere with the current proposal.

  18. RalfJung commented on Dec 2, 2024

    @RalfJung
    Member

    If that bound is equivalent to ~const ValueSized then yeah that should work -- whatever transition deals with existing size_of_val calls outside const should be able to also deal with size_of_val calls inside const.

    This does require const traits though, so the new hierarchy couldn't be stabilized until const traits are stable. But const traits are already a core part of that story so I assume that is anyway the case.

  19. RalfJung commented on Dec 2, 2024

    @RalfJung
    Member

    Stabilization PR is up: #133762

  20. added a commit that references this issue on Dec 3, 2024
  21. added a commit that references this issue on Dec 3, 2024
  22. added 2 commits that reference this issue on Dec 3, 2024
  23. added a commit that references this issue on Dec 3, 2024
  24. added this to the 1.85.0 milestone on Jan 20, 2025
  25. added a commit that references this issue on Mar 11, 2025
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-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language teamdisposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions