-
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
#[repr(packed(N))]
(tracking issue for RFC 1399)
#33158
Comments
#27060 is closely related to this. |
This should probably use |
Is anyone working on this? If not I think I mostly know how to do it, modulo someone telling me how the attribute parsing and verification needs to be extended higher up. |
Unless someone comes in and says they're doing this before then, I'm going to take a crack at it Friday or thereabouts. My intent is to use the attribute literals approach (or support both if that's what people want), plus potentially submitting a PR against the RFC repo to update the RFC to attribute literals. @eddyb or whoever else might know: is inserting a bunch of |
@camlorn everything currently has those usually-empty arrays, for |
@eddyb |
I'd say so, yes, at least for the trans part. In fact, I'm not sure there is anything left to do once I'm done, other than use the value from the attribute in the |
@eddyb did you land the work-in-progress changes that were being discussed here? |
Is there anything I can do to help implement and stabilize this feature? We need it badly for |
Is this waiting for anything more than #45225? That PR should land not too far away from now. |
#45225 appears to have landed. |
If no one is working on this I might have a go. I started looking into it on the weekend and got something going but don't know yet if it's correct. There are already a lot of tests for repr packed, seems like a sharp edge! |
I'm nearly finished with this, I haven't run into and big problems so far. If you're interested my WIP branch is here https://github.com/bitshifter/rust/tree/repr_packed. I just have a couple more tests I want to add and then I'll make a PR. |
@retep998 pointed out a mistake in my implementation which was a misunderstanding on my part of how packing works (I've never actually used pack in C/C++). My misunderstanding was I was raising the alignment of fields that were smaller than the packed size. Packing does make a lot more sense when you don't do that! It does raise a follow up question though. At the moment, packing by default disables struct field reordering optimisations, this makes sense for |
Personally I would enable struct field reordering for |
Spent a far too much time trying to work out why This is the relevant parts of
What I wanted was |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
No, we require that today and always have. |
Safe Rust code has knowingly accepted taking unaligned references to packed struct fields to create raw pointers since 1.0 and users rely on them for correctness. At this point, if the implementation requires them, then IMO the implementation is wrong. This is not as bad as it sounds, there is an RFC Any other alternative I can think of sends a really poor message. Or in other words, you are not wrong: we "rustc" require references to be aligned and always have, but if we don't tell stable safe-Rust users what we require by rejecting unsound code, then what we require does not really matter. From a user POV, their code has worked for two years and now it does not compile. |
See:
There is no official requirement about this; but it's something I and others want when stabilization reports are made. In other words: it is internal to the language team's operation.
It's not in FCP.
Please refer to RFC 1122 for rules and recommendations around handling unsoundness and breakage. As for "we don't tell stable safe-Rust users", we do emit a future compat warning, do we not? #[repr(packed)]
struct Foo {
a: u8,
b: u64
}
fn bar() {
let x = Foo { a: 1, b: 2 };
&x.b as *const _;
} |
After 24 releases accepting the code, an incorrect future compat warning was emitted. 5 releases later, the incorrect future compat warning was corrected. The corrected future compatibility warning currently states "don't do this", but it crucially does not suggest what to do instead.
I am unsure whether RFC 1122 applies here. RFC 1122 assumes that there is a path to upgrade and/or correct affected code and all its recommendations are based on this assumption holding. AFAICT one cannot construct a pointer to an unaligned field of a The policy about emitting warnings or errors that are breaking changes might need to be revisited, because it does not make much sense to do this without having a migration path for users in place unless the breaking changes are "impactless" like RFC 1122 mentions. |
RFC 1122 definitely applies and it talks about and suggests recommendations around mitigation strategies (plural), transitions, and easing things. In general, there is no hard requirement that there must be a path to upgrade; it may be in some cases that a path to upgrade is simply untenable and something simply cannot be done. However, that does not apply to |
My point is that there should probably be a hard requirement or a very very strong encouragement to provide one. If a migration path is not provided, and enough code is affected, then In particular, the solution proposed in RFC1240 to fix the issue, that is, to require FWIW the C++ committee does have a hard requirement on a migration path being available before making a breaking change. This is not always respected and that's ok, but it is always on the checklist for breaking changes. They do consider the feasibility of performing a breaking change on whether the migration path can be applied automatically, manually, or there not being a migration path at all. |
Clarifying since I think we're talking across each other: there has never been an operation in Rust that allows you to get the address of an unaligned field. This has never been a supported thing. There's an RFC in the works currently that would make this possible. It used to be that there was a way to spell something that looked like taking the address of an unaligned field, but was in fact UB and anything in the language reference, the compiler internals, or anyone on the lang team that you asked should have told you that The thing that is There's an RFC out now that would permit taking the address of an unaligned field in a way that looks like how some people have attempted to spell that operation in the past, but up till now has always been undefined behavior. There's no breaking change being made here-- we're specifying that we'll allow |
Fortunately all that discussion of whether it is currently possible to take the address of an unaligned field without causing UB does not actually matter for stabilizing |
The final comment period, with a disposition to merge, as per the review above, is now complete. |
Any takers for a stabilization PR? |
I'll do that. |
Stabilize #[repr(packed(N))] Fixes rust-lang#33158
Stabilize #[repr(packed(N))] Fixes rust-lang#33158
Reference documentation issue: rust-lang/reference#483. |
Noting action item
seems to have been missed in review (unless I missed it reading through all the comments). |
That case still errors currently. So the resolution "keep this a hard error" was picked, maybe implicitly. This should be forward-compatible with the other options. If you have further questions, I suggest asking on Zulip. Issue necromancy doesn't usually end well. ;) |
@RalfJung Would opening a new issue be the correct solution then? If we want to have complete interoperability with C, this is a needed feature since C can align and pack at the same time. The linked issue does show a need for this feature. |
There are already issues for
|
Tracking issue for rust-lang/rfcs#1399:
#[repr(packed(N))]
Unresolved issues
#[repr(packed(_))]
structs cannot transitively contain#[repr(align(_))]
structs due to differing behavior between msvc and gcc. Do we want to keep this a hard error, pick one behavior over the other, or provide some way to choose which behavior is desired?The text was updated successfully, but these errors were encountered: