Skip to content
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 transmutes in core #110094

Merged
merged 1 commit into from
May 7, 2023
Merged

Conversation

lukas-code
Copy link
Member

@lukas-code lukas-code commented Apr 8, 2023

  • Use transmute_unchecked instead of transmute_copy for MaybeUninit::transpose.
  • Use manual transmute for Option<Ordering>i8.

@rustbot
Copy link
Collaborator

rustbot commented Apr 8, 2023

r? @thomcc

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Apr 8, 2023
@rustbot
Copy link
Collaborator

rustbot commented Apr 8, 2023

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

// 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>]) };
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let uninit_src = unsafe { &*(src as *const [T] as *const [MaybeUninit<T>]) };
let uninit_src = unsafe { &*ptr::from_ref(src).cast::<[MaybeUninit<T>]>() };

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

@@ -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
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

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.

@bors
Copy link
Contributor

bors commented Apr 14, 2023

☔ The latest upstream changes (presumably #110324) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Contributor

bors commented Apr 19, 2023

☔ The latest upstream changes (presumably #110393) made this pull request unmergeable. Please resolve the merge conflicts.

@thomcc
Copy link
Member

thomcc commented May 5, 2023

Hm, I'm not entirely sure I see why &*(ptr::from_ref(thing) as *const T) and such is an improvement.

As a concrete example, using transmute catches compile errors in the case where destination is thin pointer and the source is a fat pointer, which is not caught by that pattern. Obviously this code doesn't have that issue, but I don't really see the justification to make the change everywhere.

Am I missing something?

@lukas-code
Copy link
Member Author

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 transmute_copy -> transmute_unchecked in MaybeUninit::transpose and manual transmute for Option<Ordering> if these look fine too you.

@lukas-code lukas-code changed the title less transmute in core clean up transmutes in core May 6, 2023
@thomcc
Copy link
Member

thomcc commented May 6, 2023

Yeah, those look fine, I'd r+ it with just that.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 6, 2023
@thomcc
Copy link
Member

thomcc commented May 6, 2023

Oh, you already did it.

@bors r+

@bors
Copy link
Contributor

bors commented May 6, 2023

📌 Commit 7e3b934 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 6, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request May 7, 2023
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
@bors bors merged commit aef008a into rust-lang:master May 7, 2023
@rustbot rustbot added this to the 1.71.0 milestone May 7, 2023
@lukas-code lukas-code deleted the less-transmute branch May 7, 2023 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants