-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
transmute_ptr_to_ptr
doesn’t justify its existence
#6372
Comments
cc @taralx |
I really hope that we get some safe transmutes soon-ish. |
Maybe, but in the mean time, what's the right option here? Maybe this should be disabled by default? |
Half year later, there are still no explanations. So, clearly, no one really knows why it even exists. I believe it was supposed to be the other way around, but someone somewhere just messed up the cause and effect kind of stuff. |
Well, let's try a PR and see what happens. |
Switch transmute_ptr_to_ptr to "pedantic" class. Per discussion in #6372, this lint has significant false positives. changelog: transmute_ptr_to_ptr defaults to "allow".
Old issue, but I'd like to propose the following lint documentation (maybe less of this): Warning
Slice-to-slice transmutation is extremely unlikely to be what you wantTL;DR: There are pitfallsNote in particular that the internal length remains the same (see fig1). This may not what you expect:
You might want to consider using the following alternatives:
fig1. general memory layout between slices:
fig2. Intended to have the same effect as
fig3. The reverse case of fig2: write to
Case studyConsider transmuting a
So you can always convert a GenelifyIn general, transmuting
Personally, I don't believe it is pedantic entirely; I believe some cases fall under suspicious and/or correctness. (thanks @sozysozbot for correcting my paragraphs) Footnotes
|
Adding to the problems listed above, |
Suggest `.cast`/`.cast_const`/`.cast_mut` in `transmute_ptr_as_ptr` Essentially pre-applies `ptr_as_ptr` - #6372 (comment) changelog: none
The
transmute_ptr_to_ptr
lint fires ontransmute<T, U>
whenboth
T
andU
are reference types. The docs say:But this doesn’t explain anything. These casts are just as unsafe as the
transmute
. In fact,transmute
can be written in terms of suchcasts:
…so clearly they don’t bring any safety benefit.
I am aware that the docs for
transmute
itself also mention that youcan use
as
-casts in place oftransmute
. But those docs alsooffer no explanation. They were added in rust-lang/rust#34609, where
@sfackler voiced the same concern but received no response.
(@sfackler also points out that
transmute::<&T, &U>
at least checksstatically that
&T
and&U
have the same size, which pointer casts donot. See size-checking example.)
Some veteran programmers on my team have suggested using
transmute
inthese cases for clarity, and I have to agree that it’s clearer:
The former says what it’s doing right on the tin. In the latter, you
have to make sure that your
&
s and*
s are all matching, and it’sless self-documenting.
Does this lint exist for a reason other than safety theater or cargo
culting? If so, could you please add an explanation to the docs?
The text was updated successfully, but these errors were encountered: