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

transmute_undefined_repr: false positive when TypeId has been checked #8499

Open
dtolnay opened this issue Mar 2, 2022 · 4 comments · Fixed by #8553
Open

transmute_undefined_repr: false positive when TypeId has been checked #8499

dtolnay opened this issue Mar 2, 2022 · 4 comments · Fixed by #8553
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@dtolnay
Copy link
Member

dtolnay commented Mar 2, 2022

Summary

This might be wontfix territory, but here is an interesting case derived from https://github.com/AleoHQ/snarkVM/blob/71e39319b6fb5bcaddd48c95d32e2f80ac103954/algorithms/src/msm/variable_base/cuda.rs.

Vec<u32> is guaranteed to have the same layout as Vec<u32>.

Lint Name

transmute_undefined_repr

Reproducer

use std::any::TypeId;
use std::mem;

fn f<T: 'static>(bases: Vec<T>) -> Vec<u32> {
    if TypeId::of::<T>() != TypeId::of::<u32>() {
        unimplemented!();
    }

    unsafe { mem::transmute::<Vec<T>, Vec<u32>>(bases) }
}
error: transmute from `std::vec::Vec<T>` to `std::vec::Vec<u32>`, both of which have an undefined layout
 --> src/main.rs:9:14
  |
9 |     unsafe { mem::transmute::<Vec<T>, Vec<u32>>(bases) }
  |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[deny(clippy::transmute_undefined_repr)]` on by default
  = note: two instances of the same generic type (`Vec`) may have different layouts
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_undefined_repr

Version

rustc 1.61.0-nightly (f0c4da499 2022-03-01)
binary: rustc
commit-hash: f0c4da49983aa699f715caf681e3154b445fb60b
commit-date: 2022-03-01
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Additional Labels

No response

@dtolnay dtolnay added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 2, 2022
@Jarcho
Copy link
Contributor

Jarcho commented Mar 16, 2022

This is definitely not detectable in general. The check could very easily be in a different function.

It would be possible to detect a conversion from a generic type to a concrete type and either add a note to the lint output about checking the type id, or not lint under the assumption that there is a check.

@dtolnay
Copy link
Member Author

dtolnay commented Mar 16, 2022

In my opinion we should aim to not lint if the two types could statically be a compatible pair of types.

The bar for a correctness lint that tells you "your types both have undefined layout, so what you are doing is UB" is very high. If there is some other high-false-positive checking that people want beyond this, that needs to not be a correctness lint. It could be a restriction lint instead.

Some examples that I believe should not trigger this lint, where T is a generic parameters of some surrounding scope:

  • transmute::<Vec<T>, Vec<u32>>
  • transmute::<Vec<u32>, Vec<T>>
  • transmute::<*const Ty2<u32, i32>, &Ty2<u32, T>>

@dtolnay
Copy link
Member Author

dtolnay commented Mar 16, 2022

Does this need to be reopened? #8553 says "Partially fixes #8499"

@Jarcho
Copy link
Contributor

Jarcho commented Mar 17, 2022

Type parameters are handled, but associated types are not. So Vec<T::Type> will still be a problem.

@llogiq llogiq reopened this Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants