-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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_align_offset
#90962
Comments
I agree this is a problem, but sadly the link seems to just go to the PR (links to collapsed discussions don't seem to work) so I have no idea what this has to do with the UTF8 methods -- could someone explain the connection? Is it just because |
@RalfJung I've unresolved the discussion, now the link should be working (it's the first discussion in the PR). TL;DR: originally in #90607 I've used |
Emphasis mine. :) Safe code can easily use align_offset incorrectly. So this is not something we can rely on for safety concerns. |
@WaffleLapkin Can you please explain why the const implementation always returns #![feature(const_align_offset)]
#![feature(const_mut_refs)]
#[repr(align(4096))]
struct PageAligned<T>(T);
static mut MEMORY: PageAligned<[u8; 1]> = PageAligned([0; 1]);
struct Foo;
impl Foo {
const fn new(mem: &[u8]) -> Self {
assert!(mem.as_ptr().align_offset(4096) == 0, "memory must be page aligned!");
Self
}
}
static FOO: Foo = unsafe {Foo::new(&mut MEMORY.0)};
fn main() {} This currently prevents me from doing an alignment check inside the constructor. Is this intended? What is a possible solution? |
@phip1611 during CTFE (compile time function execution) memory is organized differently, than while runtime execution Specifically it doesn't really have addresses or alignment, so there is no way to implement Also, see the documentation:
Also note that this function is unlikely to be stabilized in it's current form as a |
Thanks for the quick reply @WaffleLapkin. What do you think about the use case shown in my example? Shouldn't this be possible in the future? Do you think it is worth it to open a dedicated ticket for this? |
@phip1611 I don't think this can be ever possible, addresses of
|
Rust could deduce the alignment somehow from a |
It is true that in this specific case, the CTFE implementation of However, Cc @rust-lang/wg-const-eval |
@phip1611 aligning everything to the minimum alignment wouldn't work either 😄 Consider this example: static A: [u8; 2] = [0, 0];
assert!(
addr_of!(A[0]).align_offset(2) == 0
|| addr_of!(A[1]).align_offset(2) == 0
); This will pass in runtime, because either You can suggest aligning outer-most objects at minimum ( Upd: didn't see Ralfs comment when writing all of this, but similar problems would arise with "const-compatible alignment checks" |
@WaffleLapkin definitely, not all cases will work. A The specific case @phip1611 asked about, however, would work. In fact,
|
I wonder... is there even a good usecase for If we are okay with saying that (I'm aware that const Cc @rust-lang/wg-const-eval |
Not sure what constitutes "good", but the usecase for
In my opinion what we should do is explicitly say that Alternatively we could indeed provide some more powerful way of doing runtime-only optimizations in Also, it's surprising for me to learn about hacks, the initial implementation just always returned |
Yes: #102795
The evidence indicates that people expect |
It's interesting that these examples all use Would those examples use |
Interesting, I thought the issue was that people wanted more guarantees, not necessarily that it's confusing...
I don't know. I can't see why they couldn't use it, but I might be missing something. |
I think that's it, and created #105296 a while back |
Will people be happy if they get those guarantees but not when the function is called in |
@m-ou-se wrote in a different discussion
|
#132423 de-constifies these functions. |
Rollup merge of rust-lang#132423 - RalfJung:const-eval-align-offset, r=dtolnay remove const-support for align_offset and is_aligned As part of the recent discussion to stabilize `ptr.is_null()` in const context, the general vibe was that it's okay for a const function to panic when the same operation would work at runtime (that's just a case of "dynamically detecting that something is not supported as a const operation"), but it is *not* okay for a const function to just return a different result. Following that, `is_aligned` and `is_aligned_to` have their const status revoked in this PR, since they do return actively wrong results at const time. In the future we can consider having a new intrinsic or so that can check whether a pointer is "guaranteed to be aligned", but the current implementation based on `align_offset` does not have the behavior we want. In fact `align_offset` itself behaves quite strangely in const, and that support needs a bunch of special hacks. That doesn't seem worth it. Instead, the users that can fall back to a different implementation should just use const_eval_select directly, and everything else should not be made const-callable. So this PR does exactly that, and entirely removes const support for align_offset. Closes some tracking issues by removing the associated features: Closes rust-lang#90962 Closes rust-lang#104203 Cc `@rust-lang/wg-const-eval` `@rust-lang/libs-api`
Feature gate:
#![feature(const_align_offset)]
This is a tracking issue for
<*const _>::align_offset
and<*mut _>::align_offset
functions marked asconst fn
Public API
Steps / History
<*const _>::align_offset
and<*mut _>::align_offset
asconst fn
#90958NonNull
methods stabilized,const
ness moved to this gate Stabilizenon_null_convenience
#124498Unresolved Questions
usize::MAX
at compile time? This allows to distinguish compiletime and runtime function execution, see Make slice->str conversion and related functionsconst
#90607 (comment)The text was updated successfully, but these errors were encountered: