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

Suggest .cast/.cast_const/.cast_mut in transmute_ptr_as_ptr #13143

Merged
merged 2 commits into from
Jul 23, 2024

Conversation

Alexendoo
Copy link
Member

Essentially pre-applies ptr_as_ptr - #6372 (comment)

changelog: none

@rustbot
Copy link
Collaborator

rustbot commented Jul 22, 2024

r? @y21

rustbot has assigned @y21.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 22, 2024
format!("{}.cast::<{to_pointee_ty}>()", arg.maybe_par()),
Applicability::MaybeIncorrect,
);
} else if from_pointee_ty == to_pointee_ty
Copy link
Member

Choose a reason for hiding this comment

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

I think this could end up as a wrong suggestion for lifetime transmutes (*mut Lt<'a> to *const Lt<'static>). Maybe we can add a check if the from- or to type has TypeFlags::RE_ERASED?

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting, is there a good example? I assume this would also effect ptr_as_ptr

Copy link
Member

Choose a reason for hiding this comment

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

Can't think of a good (practical) example, but I was thinking of something like:

struct Lt<'a>(&'a ());

unsafe fn f(v: *mut Lt<'_>) -> *const Lt<'static> {
  std::mem::transmute(v)
}

This looks like it has .cast_const() for the suggestion, but that doesn't change the lifetime

Basically just this test case but with raw pointers and with differing mutability (so it doesn't go into the first if for pointer::cast):

unsafe fn transmute_lifetime_to_static<'a, T>(t: &'a T) -> &'static 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.

Thanks, unrealistic is fine I just can never remember which lifetimes are erased or early/late or any of that stuff 😅

Fixed it for ptr_cast_constness too

@y21
Copy link
Member

y21 commented Jul 23, 2024

Looks good to me now! @bors r+

@bors
Copy link
Contributor

bors commented Jul 23, 2024

📌 Commit 7010d3c has been approved by y21

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jul 23, 2024

⌛ Testing commit 7010d3c with merge e91d82e...

@bors
Copy link
Contributor

bors commented Jul 23, 2024

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: y21
Pushing e91d82e to master...

@bors bors merged commit e91d82e into rust-lang:master Jul 23, 2024
8 checks passed
@Alexendoo Alexendoo deleted the transmute-ptr-to-ptr-suggestions branch July 23, 2024 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants