-
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
Conflicting documentation on the alignment requirements for ptr::copy
#62313
Comments
Good catch! What we'd really want is
Feel free to suggest code that we can use instead. We have several doctests that need rust-lang/rfcs#2582. They have been there since 1.0 and longer, probably. But nobody listens to me when I say that there is existing code that we need to support ASAP. |
The example given is just using
I think we should probably mention the caveat WRT the optimizer eliminating the copy (or can it even do that in this case? The docs mention that the drop glue generated for packed structs does this copy automatically, I'm curious if it's doing so in a way that is UB or if the copy is just the price you have to pay here. It's also worth noting that there's no way to |
Oh, good point. Yeah then you can basically just make it
Well but wouldn't it make sense for that text to come with an example?
It cannot, because then it would be introducing alignment errors.
Uh... yeah. With Doesn't |
Presumably because they wanted the example to continue to use
Sure, we could -- it just wouldn't ever call the function being documented.
Yes, it's just unrelated to dropping unaligned pointers since the only place you can reasonably copy them to is the stack. Unless you happen to already have an valid pointer of the proper size and alignment sitting around |
I'm not actually sure unalgined DSTs are so unreasonable. For example, unsizing
Which could be solved if we had e.g. a way to express "does not have drop glue" as a bound ( In any case, I think "copy unaligned" functions make sense but we might want to consider multiple variants for src only / dest only / both unaligned. This can be expressed in LLVM IR though in a brief test I couldn't demonstrate that it currently makes a codegen difference (but I could only try on x86_64, where alignment rarely matters for codegen). |
🤣 fair. I guess all of this is basically saying an in-place drop of unaligned data is just not possible. Do an "out-of-place" drop instead. |
Actually looks like the use of an unaligned pointer in the doctest just caused #62103 to fail to land. ;) |
Proposed fix at #62351. |
remove bogus example from drop_in_place Fixes rust-lang#62313
remove bogus example from drop_in_place Fixes rust-lang#62313
remove bogus example from drop_in_place Fixes rust-lang#62313
The docs for
ptr::drop_in_place
state:And include an example which uses
ptr::copy
to copy the value from an unaligned pointer into an aligned one. However, the docs forptr::copy
state:Either the docs for
ptr::copy
are wrong, andsrc
does not need to be aligned, or the example fordrop_in_place
invokes UB and should be updated to useread_unaligned
The text was updated successfully, but these errors were encountered: