-
Notifications
You must be signed in to change notification settings - Fork 13k
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
clean up transmute
s in core
#110094
clean up transmute
s in core
#110094
Conversation
r? @thomcc (rustbot has picked a reviewer for you, use r? to override) |
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
10cf175
to
ebf766a
Compare
library/core/src/mem/maybe_uninit.rs
Outdated
// SAFETY: &[T] and &[MaybeUninit<T>] have the same layout | ||
let uninit_src: &[MaybeUninit<T>] = unsafe { super::transmute(src) }; | ||
// SAFETY: `[T]` and `[MaybeUninit<T>]` have the same layout. | ||
let uninit_src = unsafe { &*(src as *const [T] as *const [MaybeUninit<T>]) }; |
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.
It might make sense to use ptr::from_ref
and .cast::<T>()
instead of as
casts for these right away, see #109255.
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.
let uninit_src = unsafe { &*(src as *const [T] as *const [MaybeUninit<T>]) }; | |
let uninit_src = unsafe { &*ptr::from_ref(src).cast::<[MaybeUninit<T>]>() }; |
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.
I agree with this change, but I'd rather land it separately. IMO, if we decide to change these pointer casts (which are everywhere in core) they should be changed all at once, preferably with a lint.
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.
this is introducing new uses of as
, which can be avoided by using those methods.
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.
Looks like cast
doesn't work for unsized pointee types yet (and won't work without moderate changes to the compiler), but I've replaced the reference -> pointer casts with ptr::from_ref
now.
library/core/src/tuple.rs
Outdated
@@ -129,13 +128,9 @@ const fn ordering_is_some(c: Option<Ordering>, x: Ordering) -> bool { | |||
// This isn't using `match` because that optimizes worse due to |
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.
It uses match
now. Maybe remove/edit the comment if that's no longer a problem.
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.
Yeah, this essentially uses
const fn ordering_is_some1(c: Option<Ordering>, x: Ordering) -> bool {
x as i8 == match c {
Some(c) => c as i8,
None => 2,
}
}
instead of
const fn ordering_is_some2(c: Option<Ordering>, x: Ordering) -> bool {
match c {
Some(c) => c as i8 == x as i8,
None => false,
}
}
, because the first one optimizes better.
Do you have a suggestion for a better comment?
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.
// This is mapping None to 2 and then doing the comparison afterwards
// because it optimizes better.
☔ The latest upstream changes (presumably #110324) made this pull request unmergeable. Please resolve the merge conflicts. |
4e83ff2
to
464c35d
Compare
☔ The latest upstream changes (presumably #110393) made this pull request unmergeable. Please resolve the merge conflicts. |
464c35d
to
9ec4999
Compare
9ec4999
to
f38b3d8
Compare
Hm, I'm not entirely sure I see why As a concrete example, using Am I missing something? |
Yeah, I'm no longer convinced, that reborrows + pointer casts are better than transmuting references either. I just noticed, that we recommend this pattern in the transmute docs and use it in most parts if the standard library already. I can drop the reference transmute change and just keep the |
Yeah, those look fine, I'd r+ it with just that. @rustbot author |
Oh, you already did it. @bors r+ |
Rollup of 7 pull requests Successful merges: - rust-lang#105583 (Operand::extract_field: only cast llval if it's a pointer and replace bitcast w/ pointercast.) - rust-lang#110094 (clean up `transmute`s in `core`) - rust-lang#111150 (added TraitAlias to check_item() for missing_docs) - rust-lang#111293 (rustc --explain E0726 - grammar fixing (it's => its + add a `the` where it felt right to do so)) - rust-lang#111300 (Emit while_true lint spanning the entire loop condition) - rust-lang#111301 (Remove calls to `mem::forget` and `mem::replace` in `Option::get_or_insert_with`.) - rust-lang#111303 (update Rust Unstable Book docs for `--extern force`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
transmute_unchecked
instead oftransmute_copy
forMaybeUninit::transpose
.Option<Ordering>
→i8
.