Skip to content
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

Make size_of_val a const fn (and stabilize it as such) #46571

Open
Gankra opened this issue Dec 7, 2017 · 16 comments
Open

Make size_of_val a const fn (and stabilize it as such) #46571

Gankra opened this issue Dec 7, 2017 · 16 comments
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@Gankra
Copy link
Contributor

Gankra commented Dec 7, 2017

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).

@Gankra
Copy link
Contributor Author

Gankra commented Dec 7, 2017

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

@Gankra
Copy link
Contributor Author

Gankra commented Dec 7, 2017

cc @eddyb @oli-obk

@oli-obk
Copy link
Contributor

oli-obk commented Dec 7, 2017

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

@Gankra
Copy link
Contributor Author

Gankra commented Dec 7, 2017

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

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. labels Jan 23, 2018
@polarathene
Copy link

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?

@oli-obk
Copy link
Contributor

oli-obk commented Apr 27, 2018

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

@Gankra
Copy link
Contributor Author

Gankra commented May 9, 2018

i'm implementing this

@kennytm
Copy link
Member

kennytm commented May 9, 2018

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.

@whitequark
Copy link
Member

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?

@oli-obk
Copy link
Contributor

oli-obk commented May 9, 2018

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

@kennytm
Copy link
Member

kennytm commented May 9, 2018

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
}

@steveklabnik
Copy link
Member

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?

@oli-obk
Copy link
Contributor

oli-obk commented Sep 22, 2019

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

bors added a commit to rust-lang-ci/rust that referenced this issue Jul 30, 2020
…l, r=oli-obk

Make `mem::size_of_val` and `mem::align_of_val` unstably const

Implements rust-lang#46571 but does not stabilize it. I wanted this while working on something today.

The only reason not to immediately stabilize are concerns around [custom DSTs](rust-lang#46571 (comment)). That proposal has made zero progress in the last two years and const eval is rich enough to support pretty much any user-defined `len` function as long as nightly features are allowed (`raw_ptr_deref`).

Currently, this raises a `const_err` lint when passed an `extern type`.

r? @oli-obk

cc @rust-lang/wg-const-eval
@CAD97
Copy link
Contributor

CAD97 commented Feb 21, 2022

Just recording here that these functions being const has an implication for user defined custom DSTs (specifically requiring size/align access to be const).

@ScSofts

This comment was marked as off-topic.

@iddm

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-fn Area: const fn foo(..) {..}. Pure functions which can be applied at compile time. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

10 participants