-
Notifications
You must be signed in to change notification settings - Fork 13.3k
transmute documentation does not explicitly mention alignment requirement #82493
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
That looks like well-defined behavior to me. The alignment of Regarding the quote paragraph, this references a conversion of |
Thanks for the clarification! So the documentation could still be updated to clarify when alignment matters, right? |
I do not see a feasible way for the You example (the text you quoted) is specific to |
However, maybe the comment in the example could be worded a bit more carefully to explain that alignment matters in this specific case because we are transmuting a pointer (implicitly as part of the Honestly, that entire example is a bit weird. It says "still using transmute" but the code this comments doesn't even use |
If alignment is not a concern when transmuting values (because the compiler will move the memory to a stack location which is properly aligned), I think it would be worth explicitly saying this. Unless I was explicitly told (like Heroic explained), it would not have occured to me that it's moving into a proper aligned location. With regards to "explain all possible types", I don't think this is necessary. Saying "The resulting type must be a valid value" might be enough. Or maybe the:
…should be changed to encourage reading that page more strongly ("Make sure you understand all the dangers of using
It's not even use // To "transmute" the contents of a heap-allocated container to something else,
// you must make sure that both the size *and alignment* of the items match,
// so that the internal representation of the container is not affected:
let v_from_raw = unsafe {
// Ensure the original vector is not dropped.
let mut v_clone = std::mem::ManuallyDrop::new(v_clone);
Vec::from_raw_parts(v_clone.as_mut_ptr() as *mut Option<&i32>,
v_clone.len(),
v_clone.capacity())
}; |
I'm not a fan of listing all the things that are not a concern. That's a long list. However, you are not the first person to suggest this, so I concede. I guess I will never understand why people expect alignment to be an issue here, but it seems they do, and I don't have to understand everything. ;)
It already says that (in double-negated form): "Neither the original, nor the result, may be an invalid value."
There are people (myself included) that use the term "to transmute" for any kind of "type-changing reinterpretation of raw bytes", whether it is via |
In my case, it's because I'm not sure if it was undefined behaviour to read a type from memory with the wrong alignment. IIRC, there are some CPU architectures that fault when a misaligned memory access occurs? Or maybe I'm misremembering. Basically, I wasn't sure if "interpret this bunch of bytes as this other type" is something that is okay to do when "the bunch of bytes" is misaligned.
My bad, somehow missed it. |
It is UB to perform an unaligned read or write. But Anyway, you made some good suggestions -- do you plan to turn these into a PR, or should we mark this as an E-easy issue for someone else to write the PR? |
Improve transmute docs with further clarifications Closes rust-lang#82493. Please let me know if any of the new wording sounds off, English is not my mother tongue.
Improve transmute docs with further clarifications Closes rust-lang#82493. Please let me know if any of the new wording sounds off, English is not my mother tongue.
Improve transmute docs with further clarifications Closes rust-lang#82493. Please let me know if any of the new wording sounds off, English is not my mother tongue.
The documentation for
std::mem::transmute
reads:However, it does not mention that they must also have the same alignment. You would only learn about this if you try to dig into the code examples further down the page:
Must the types
T
andU
have the same alignment, or is that a particular quirk of the example?In any case, I think the documentation should explicitly mention alignment, because the compiler succeeds to compile the following with no warnings (playground), and I believe this is undefined behaviour:
@rustbot modify labels: +T-doc +C-enhancement
The text was updated successfully, but these errors were encountered: