-
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
Is Rc’s and Arc’s data_offset correct? #62522
Comments
If this were user-facing code it would be a clear case of "no that's not specified, don't assume that". However, I believe rustc does currently honor this assumption and libstd can of course rely on rustc implementation details. @eddyb would know best.
Note that because unsizing happens with "reorganizing" the struct, the struct with a sized T has to be laid out "as if unsized" from the start. For example, This reasoning wouldn't apply if there was a type that couldn't ever be involved in unsizing, but I don't think the compiler can ever realistically rule that out locally, and I'm rather certain that it doesn't today. |
Ah that’s a good point. The constraint that layout cannot change while unsizing is probably enough indeed. |
We should probably add a comment to that effect so "don't assume that" is clearer to everyone. |
We should! See rust-lang/unsafe-code-guidelines#90. |
FWIW these are |
In the implementation of
Arc
, we find:rust/src/liballoc/sync.rs
Lines 258 to 269 in 09ab31b
rust/src/liballoc/sync.rs
Lines 2288 to 2304 in 09ab31b
(And similarly in
rc.rs
withstruct RcBox
.)Note the comment:
Because it is ?Sized, it will always be the last field in memory.
Is this actually true, even for instantiations where
T
happens to beSized
? Does the language guarantee that there exist no typeFoo
such that the fields ofArcInner<Foo>
would be re-ordered?Or do we need to add
#[repr(C)]
toArcInner
andRcBox
?CC @rust-lang/wg-unsafe-code-guidelines
The text was updated successfully, but these errors were encountered: