-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 offset_of! macro (RFC 3308) #106934
Add offset_of! macro (RFC 3308) #106934
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
Some changes occurred in src/tools/rustfmt cc @rust-lang/rustfmt Some changes occured in cc @BoxyUwU Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 This PR changes MIR cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
Some changes occurred to the CTFE / Miri engine cc @rust-lang/miri Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
What a great PR. It also adds DST support from the get-go which is amazing (I care about that use case a bit xD). I've seen tests for nested fields, but couldn't find any tests for DSTs. Maybe something like this: #[test]
#[cfg(not(bootstrap))]
fn offset_in_dst() {
#[repr(C)]
struct Foo {
x: u8,
y: u16,
more: [u8],
}
const X_OFFSET: usize = offset_of!(Foo, x);
const Y_OFFSET: usize = offset_of!(Foo, y);
assert_eq!(X_OFFSET, 0);
assert_eq!(Y_OFFSET, 2);
} The implementation also errors nicely in the case of |
This comment has been minimized.
This comment has been minimized.
b39b548
to
0d40d07
Compare
Some changes occurred to the core trait solver cc @rust-lang/initiative-trait-system-refactor Some changes occurred in src/tools/cargo cc @ehuss These commits modify compiler targets. Some changes occurred to the CTFE / Miri engine cc @rust-lang/miri Some changes occurred in diagnostic error codes |
0d40d07
to
4694ea2
Compare
This comment has been minimized.
This comment has been minimized.
Frontend LGTM, but someone else needs to look at MIR, const eval and codegen. |
This comment has been minimized.
This comment has been minimized.
Could you add a few tests?
assert_eq!(addr_of!(base).addr() + offset_of!(base_ty, field), addr_of!(base.field));
|
This comment has been minimized.
This comment has been minimized.
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.
Thank you for the work you've put into this, @drmeepster! I think this is ready to be merged now, congrats!
@bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (80a2ec4): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. |
Add offset_of! macro (RFC 3308) Implements rust-lang/rfcs#3308 (tracking issue rust-lang#106655) by adding the built in macro `core::mem::offset_of`. Two of the future possibilities are also implemented: * Nested field accesses (without array indexing) * DST support (for `Sized` fields) I wrote this a few months ago, before the RFC merged. Now that it's merged, I decided to rebase and finish it. cc `@thomcc` (RFC author)
Add offset_of! macro (RFC 3308) Implements rust-lang/rfcs#3308 (tracking issue rust-lang#106655) by adding the built in macro `core::mem::offset_of`. Two of the future possibilities are also implemented: * Nested field accesses (without array indexing) * DST support (for `Sized` fields) I wrote this a few months ago, before the RFC merged. Now that it's merged, I decided to rebase and finish it. cc `@thomcc` (RFC author)
Implement builtin # syntax and use it for offset_of!(...) Add `builtin #` syntax to the parser, as well as a generic infrastructure to support both item and expression position builtin syntaxes. The PR also uses this infrastructure for the implementation of the `offset_of!` macro, added by rust-lang#106934. cc `@petrochenkov` `@DrMeepster` cc rust-lang#110680 `builtin #` tracking issue cc rust-lang#106655 `offset_of!` tracking issue
Implements rust-lang/rfcs#3308 (tracking issue #106655) by adding the built in macro
core::mem::offset_of
. Two of the future possibilities are also implemented:Sized
fields)I wrote this a few months ago, before the RFC merged. Now that it's merged, I decided to rebase and finish it.
cc @thomcc (RFC author)