-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Tracking issue for const_size_of_val
and const_align_of_val
#46571
Comments
I believe this is currently blocked on miri becoming the primary/only CTFE provider. |
I think this is possible without miri const eval, but it would be wasteful to implement in old ctfe |
Yeah I consider "the people who would implement/review this are able but unwilling" to be a blocker |
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? |
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 |
i'm implementing this |
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 |
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 |
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
} |
Triage: looks like the original PR didn't get merged. We've been making more and more stuff |
…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
Just recording here that these functions being |
This comment has been minimized.
This comment has been minimized.
This comment was marked as off-topic.
This comment was marked as off-topic.
const_size_of_val
and const_align_of_val
@rust-lang/lang @rust-lang/wg-const-eval are there any concerns about making these functions const-stable? I think we should go ahead and make them const-stable. @CAD97 noted above that
However, (a) that seems like a reasonable limitation, and (b) this might block custom DSTs on const trait support, but given that custom DSTs don't even have a consensus vision for what the design may look like, that's unlikely to become a problem. |
@rfcbot fcp merge The proposal is to make these const stable. (Untagging libs-api on the advice of @Amanieu and @joshtriplett.) |
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
This sounds reasonable to me. It makes me wish that we had the @rfcbot reviewed |
@rfcbot reviewed |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
i suppose a const #![feature(const_size_of_val)]
assert_eq!(6, (const { std::mem::size_of_val(c"hello") })); |
https://doc.rust-lang.org/nightly/std/ffi/struct.CStr.html#method.from_bytes_until_nul and friends are const-callable, so yeah we can determine the size of a C string in |
This could actually be a problem for scalable SIMD vectors where the size of |
Hm, yeah. Though those are very non-standard types anyway and so far it is unclear how to make them work in Rust for various reasons. We always have the option of aborting const-eval when it tries to determine the size of a type like that (similar to, e.g., |
@Amanieu That's one of the things I was referring to with the trait split above. We already need to make changes -- for non-const too -- to support things like |
I'm not sure how scalable SIMD vector is problematic here. To use // can't write this even if the alignment of `[u8]` is always 1.
fn x(a: &[u8]) -> usize {
// error[E0435]: attempt to use a non-constant value in a constant
const { std::mem::align_of_val(a) }
} so the only way this matters in safe code is if you can construct an static VALUE: svfloat32_t = ????;
const SIZE: usize = size_of_val(&VALUE); you could bypass the constructibility question by using const PTR: *const svfloat32_t = ptr::null();
// error[E0080]: evaluation of constant value failed
const SIZE: usize = unsafe { size_of_val_raw(PTR) }; conceptually the const fn size_of_val_raw(_: *const svfloat32_t) -> usize {
const fn size_of_svfloat32_t_ct() -> usize { panic!() }
fn size_of_svfloat32_t_rt() -> usize { 4 * svcntw() }
const_eval_select((), size_of_svfloat32_t_ct, size_of_svfloat32_t_rt)
} there is no need to introduce |
We're aware of the challenges related to constness and scalable vectors and the RFC we're working on addresses it. There's a similar issue with the already const-stable |
Aforementioned RFC is rust-lang/rfcs#3729. |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@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? |
This is a tracking issue for constness of the following stable API:
It would be useful for macros to be able to do things like:
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).
The text was updated successfully, but these errors were encountered: