-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add align_offset intrinsic #43903
Add align_offset intrinsic #43903
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
src/libcore/intrinsics.rs
Outdated
/// overflow or go beyond the allocation that `ptr` points into. | ||
/// It is up to the caller to ensure that the returned offset is correct | ||
/// in all terms other than alignment. | ||
pub fn align_offset(ptr: *const (), align: usize) -> usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Should this be unsafe, for consistency with the real intrinsic?
/// There are no guarantees whatsover that offsetting the pointer will not | ||
/// overflow or go beyond the allocation that `ptr` points into. | ||
/// It is up to the caller to ensure that the returned offset is correct | ||
/// in all terms other than alignment. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding an # Examples
doctest to demo a few simple cases.
src/libcore/intrinsics.rs
Outdated
/// # use std::mem::align_of; | ||
/// let x = [5u8, 6u8, 7u8, 8u8, 9u8]; | ||
/// let ptr = &x[n] as *const u8; | ||
/// let offset = align_offset(ptr as *const (), align_of::<u16>()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc test failed.
[00:56:48] failures:
[00:56:48]
[00:56:48] ---- intrinsics.rs - intrinsics::align_offset (line 1361) stdout ----
[00:56:48] error: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library (see issue #0)
[00:56:48] --> intrinsics.rs:9:14
[00:56:48] |
[00:56:48] 9 | let offset = align_offset(ptr as *const (), align_of::<u16>());
[00:56:48] | ^^^^^^^^^^^^
[00:56:48] |
[00:56:48] = help: add #![feature(core_intrinsics)] to the crate attributes to enable
[00:56:48]
[00:56:48] error: use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library (see issue #0)
[00:56:48] --> intrinsics.rs:5:5
[00:56:48] |
[00:56:48] 5 | use std::intrinsics::align_offset;
[00:56:48] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[00:56:48] |
[00:56:48] = help: add #![feature(core_intrinsics)] to the crate attributes to enable
[00:56:48]
[00:56:48] error[E0308]: mismatched types
[00:56:48] --> intrinsics.rs:11:30
[00:56:48] |
[00:56:48] 11 | let u16_ptr = ptr.offset(offset) as *const u16;
[00:56:48] | ^^^^^^ expected isize, found usize
[00:56:48]
[00:56:48] thread 'rustc' panicked at 'couldn't compile the test', /checkout/src/librustdoc/test.rs:280:12
[00:56:48] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[00:56:48]
[00:56:48]
[00:56:48] failures:
[00:56:48] intrinsics.rs - intrinsics::align_offset (line 1361)
[00:56:48]
[00:56:48] test result: FAILED. 1231 passed; 1 failed; 1 ignored; 0 measured; 0 filtered out
[00:56:48]
[00:56:48] error: test failed, to rerun pass '--doc'
see rust-lang/rfcs#2043 for details
ping @aturon |
@bors: r+ |
📌 Commit be96ad2 has been approved by |
Add align_offset intrinsic see rust-lang/rfcs#2043 for details and the plan towards stabilization (reexport in `core::mem` via various convenience functions) as per @scottmcm 's [comment](rust-lang/rfcs#2043 (comment)), this is just the intrinsic (which is obviously unstable).
☀️ Test successful - status-appveyor, status-travis |
…trochenkov Fix the wrong subtraction in align_offset intrinsic. Given how the stage0 implementation in rust-lang#43903 is written, as well as that in the RFC, I suppose the current implementation has a typo. cc rust-lang#44488, cc @oli-obk.
see rust-lang/rfcs#2043 for details and the plan towards stabilization (reexport in
core::mem
via various convenience functions)as per @scottmcm 's comment, this is just the intrinsic (which is obviously unstable).