From a716d186e89728a5ccb4dedb23c7c7b921210e76 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 16 Aug 2024 22:24:01 -0700 Subject: [PATCH 1/4] Replace transmute with pointer cast --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5f1a729..8e90c2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -119,7 +119,6 @@ use core::fmt::{self, Debug}; #[cfg(not(no_const_type_id))] use core::hash::{Hash, Hasher}; use core::marker::PhantomData; -use core::mem; #[cfg(not(no_const_type_id))] #[derive(Copy, Clone)] @@ -217,6 +216,6 @@ where let phantom_data = PhantomData::; NonStaticAny::get_type_id(unsafe { - mem::transmute::<&dyn NonStaticAny, &(dyn NonStaticAny + 'static)>(&phantom_data) + &*(&phantom_data as *const dyn NonStaticAny as *const (dyn NonStaticAny + 'static)) }) } From 47a831c591992477924594a0f055a398f7249536 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 16 Aug 2024 22:24:41 -0700 Subject: [PATCH 2/4] Ignore unnecessary_cast clippy false positive warning: casting raw pointers to the same type and constness is unnecessary (`*const dyn of::NonStaticAny` -> `*const dyn of::NonStaticAny`) --> src/lib.rs:219:11 | 219 | &*(&phantom_data as *const dyn NonStaticAny as *const (dyn NonStaticAny + 'static)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&phantom_data as *const dyn NonStaticAny` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast = note: `#[warn(clippy::unnecessary_cast)]` on by default --- src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 8e90c2a..306e083 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,7 +107,11 @@ #![no_std] #![doc(html_root_url = "https://docs.rs/typeid/1.0.1")] -#![allow(clippy::doc_markdown, clippy::inline_always)] +#![allow( + clippy::doc_markdown, + clippy::inline_always, + clippy::unnecessary_cast, // https://github.com/rust-lang/rust-clippy/issues/12860 +)] extern crate self as typeid; From 41f2b9f3b64b720128e8abf76b9839849f42a9b1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 16 Aug 2024 22:25:22 -0700 Subject: [PATCH 3/4] Raise required compiler to Rust 1.75 On older compilers, the pointer cast does not compile due to a rustc bug. error[E0310]: the parameter type `T` may not live long enough --> src/lib.rs:223:12 | 223 | &*(&phantom_data as *const dyn NonStaticAny as *const (dyn NonStaticAny + 'static)) | ^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds | help: consider adding an explicit lifetime bound... | 203 | T: ?Sized + 'static, | +++++++++ --- .github/workflows/ci.yml | 2 +- Cargo.toml | 2 +- build.rs | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 94a1d63..b89816e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,7 +44,7 @@ jobs: strategy: fail-fast: false matrix: - rust: [stable, 1.61.0, 1.34.0] + rust: [stable, 1.75.0] timeout-minutes: 45 steps: - uses: actions/checkout@v4 diff --git a/Cargo.toml b/Cargo.toml index d9bd018..2b940c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ documentation = "https://docs.rs/typeid" edition = "2018" license = "MIT OR Apache-2.0" repository = "https://github.com/dtolnay/typeid" -rust-version = "1.34" +rust-version = "1.75" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/build.rs b/build.rs index 969cf4e..d79437e 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,5 @@ +#![allow(clippy::manual_let_else)] + use std::env; use std::process::Command; use std::str; From 215fd135c6978e8489c2651a87c3157b5844f242 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 16 Aug 2024 22:31:44 -0700 Subject: [PATCH 4/4] Resolve borrow_as_ptr pedantic clippy lint warning: borrow as raw pointer --> src/lib.rs:223:12 | 223 | &*(&phantom_data as *const dyn NonStaticAny as *const (dyn NonStaticAny + 'static)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `core::ptr::addr_of!(phantom_data)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#borrow_as_ptr = note: `-W clippy::borrow-as-ptr` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::borrow_as_ptr)]` --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 306e083..2518bfb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -123,6 +123,7 @@ use core::fmt::{self, Debug}; #[cfg(not(no_const_type_id))] use core::hash::{Hash, Hasher}; use core::marker::PhantomData; +use core::ptr; #[cfg(not(no_const_type_id))] #[derive(Copy, Clone)] @@ -220,6 +221,6 @@ where let phantom_data = PhantomData::; NonStaticAny::get_type_id(unsafe { - &*(&phantom_data as *const dyn NonStaticAny as *const (dyn NonStaticAny + 'static)) + &*(ptr::addr_of!(phantom_data) as *const (dyn NonStaticAny + 'static)) }) }