Skip to content

Commit

Permalink
Fix sync fallout
Browse files Browse the repository at this point in the history
  • Loading branch information
flip1995 committed Aug 11, 2020
1 parent 9e73d33 commit 5cb8deb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 77 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,14 +704,14 @@ fn can_be_expressed_as_pointer_cast<'tcx>(
from_ty: Ty<'tcx>,
to_ty: Ty<'tcx>,
) -> bool {
use CastKind::*;
use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
matches!(
check_cast(cx, e, from_ty, to_ty),
Some(PtrPtrCast | PtrAddrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast)
)
}

/// If a cast from from_ty to to_ty is valid, returns an Ok containing the kind of
/// If a cast from `from_ty` to `to_ty` is valid, returns an Ok containing the kind of
/// the cast. In certain cases, including some invalid casts from array references
/// to pointers, this may cause additional errors to be emitted and/or ICE error
/// messages. This function will panic if that occurs.
Expand Down
7 changes: 4 additions & 3 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,6 @@ fn run_ui_toml(config: &mut compiletest::Config) {
}

fn run_ui_cargo(config: &mut compiletest::Config) {
if cargo::is_rustc_test_suite() {
return;
}
fn run_tests(
config: &compiletest::Config,
filter: &Option<String>,
Expand Down Expand Up @@ -217,6 +214,10 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
Ok(result)
}

if cargo::is_rustc_test_suite() {
return;
}

config.mode = TestMode::Ui;
config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();

Expand Down
38 changes: 13 additions & 25 deletions tests/ui/transmutes_expressible_as_ptr_casts.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,48 @@

use std::mem::{size_of, transmute};

// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
// valid, which we quote from below.
fn main() {
// We should see an error message for each transmute, and no error messages for
// the casts, since the casts are the recommended fixes.

// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
let _ptr_i32_transmute = unsafe {
usize::MAX as *const i32
};
let _ptr_i32_transmute = unsafe { usize::MAX as *const i32 };
let ptr_i32 = usize::MAX as *const i32;

// e has type *T, U is *U_0, and either U_0: Sized ...
let _ptr_i8_transmute = unsafe {
ptr_i32 as *const i8
};
let _ptr_i8_transmute = unsafe { ptr_i32 as *const i8 };
let _ptr_i8 = ptr_i32 as *const i8;

let slice_ptr = &[0,1,2,3] as *const [i32];
let slice_ptr = &[0, 1, 2, 3] as *const [i32];

// ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
let _ptr_to_unsized_transmute = unsafe {
slice_ptr as *const [u16]
};
let _ptr_to_unsized_transmute = unsafe { slice_ptr as *const [u16] };
let _ptr_to_unsized = slice_ptr as *const [u16];
// TODO: We could try testing vtable casts here too, but maybe
// we should wait until std::raw::TraitObject is stabilized?

// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
let _usize_from_int_ptr_transmute = unsafe {
ptr_i32 as usize
};
let _usize_from_int_ptr_transmute = unsafe { ptr_i32 as usize };
let _usize_from_int_ptr = ptr_i32 as usize;

let array_ref: &[i32; 4] = &[1,2,3,4];
let array_ref: &[i32; 4] = &[1, 2, 3, 4];

// e has type &[T; n] and U is *const T; array-ptr-cast
let _array_ptr_transmute = unsafe {
array_ref as *const [i32; 4]
};
let _array_ptr_transmute = unsafe { array_ref as *const [i32; 4] };
let _array_ptr = array_ref as *const [i32; 4];

fn foo(_: usize) -> u8 { 42 }
fn foo(_: usize) -> u8 {
42
}

// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
let _usize_ptr_transmute = unsafe {
foo as *const usize
};
let _usize_ptr_transmute = unsafe { foo as *const usize };
let _usize_ptr_transmute = foo as *const usize;

// e is a function pointer type and U is an integer; fptr-addr-cast
let _usize_from_fn_ptr_transmute = unsafe {
foo as usize
};
let _usize_from_fn_ptr_transmute = unsafe { foo as usize };
let _usize_from_fn_ptr = foo as *const usize;
}

Expand Down
38 changes: 13 additions & 25 deletions tests/ui/transmutes_expressible_as_ptr_casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,48 @@

use std::mem::{size_of, transmute};

// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
// valid, which we quote from below.
fn main() {
// We should see an error message for each transmute, and no error messages for
// the casts, since the casts are the recommended fixes.

// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
let _ptr_i32_transmute = unsafe {
transmute::<usize, *const i32>(usize::MAX)
};
let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
let ptr_i32 = usize::MAX as *const i32;

// e has type *T, U is *U_0, and either U_0: Sized ...
let _ptr_i8_transmute = unsafe {
transmute::<*const i32, *const i8>(ptr_i32)
};
let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
let _ptr_i8 = ptr_i32 as *const i8;

let slice_ptr = &[0,1,2,3] as *const [i32];
let slice_ptr = &[0, 1, 2, 3] as *const [i32];

// ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
let _ptr_to_unsized_transmute = unsafe {
transmute::<*const [i32], *const [u16]>(slice_ptr)
};
let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u16]>(slice_ptr) };
let _ptr_to_unsized = slice_ptr as *const [u16];
// TODO: We could try testing vtable casts here too, but maybe
// we should wait until std::raw::TraitObject is stabilized?

// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
let _usize_from_int_ptr_transmute = unsafe {
transmute::<*const i32, usize>(ptr_i32)
};
let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
let _usize_from_int_ptr = ptr_i32 as usize;

let array_ref: &[i32; 4] = &[1,2,3,4];
let array_ref: &[i32; 4] = &[1, 2, 3, 4];

// e has type &[T; n] and U is *const T; array-ptr-cast
let _array_ptr_transmute = unsafe {
transmute::<&[i32; 4], *const [i32; 4]>(array_ref)
};
let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
let _array_ptr = array_ref as *const [i32; 4];

fn foo(_: usize) -> u8 { 42 }
fn foo(_: usize) -> u8 {
42
}

// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
let _usize_ptr_transmute = unsafe {
transmute::<fn(usize) -> u8, *const usize>(foo)
};
let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
let _usize_ptr_transmute = foo as *const usize;

// e is a function pointer type and U is an integer; fptr-addr-cast
let _usize_from_fn_ptr_transmute = unsafe {
transmute::<fn(usize) -> u8, usize>(foo)
};
let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
let _usize_from_fn_ptr = foo as *const usize;
}

Expand Down
44 changes: 22 additions & 22 deletions tests/ui/transmutes_expressible_as_ptr_casts.stderr
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
error: transmute from an integer to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:20:9
--> $DIR/transmutes_expressible_as_ptr_casts.rs:19:39
|
LL | transmute::<usize, *const i32>(usize::MAX)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `usize::MAX as *const i32`
LL | let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `usize::MAX as *const i32`
|
= note: `-D clippy::useless-transmute` implied by `-D warnings`

error: transmute from a pointer to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:26:9
--> $DIR/transmutes_expressible_as_ptr_casts.rs:23:38
|
LL | transmute::<*const i32, *const i8>(ptr_i32)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
LL | let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
|
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`

error: transmute from a pointer to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:34:9
--> $DIR/transmutes_expressible_as_ptr_casts.rs:29:46
|
LL | transmute::<*const [i32], *const [u16]>(slice_ptr)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]`
LL | let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u16]>(slice_ptr) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]`

error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:42:9
--> $DIR/transmutes_expressible_as_ptr_casts.rs:35:50
|
LL | transmute::<*const i32, usize>(ptr_i32)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
|
= note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings`

error: transmute from a reference to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:50:9
--> $DIR/transmutes_expressible_as_ptr_casts.rs:41:41
|
LL | transmute::<&[i32; 4], *const [i32; 4]>(array_ref)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`
LL | let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`

error: transmute from `fn(usize) -> u8 {main::foo}` to `*const usize` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:58:9
--> $DIR/transmutes_expressible_as_ptr_casts.rs:49:41
|
LL | transmute::<fn(usize) -> u8, *const usize>(foo)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
LL | let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`

error: transmute from `fn(usize) -> u8 {main::foo}` to `usize` which could be expressed as a pointer cast instead
--> $DIR/transmutes_expressible_as_ptr_casts.rs:64:9
--> $DIR/transmutes_expressible_as_ptr_casts.rs:53:49
|
LL | transmute::<fn(usize) -> u8, usize>(foo)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
LL | let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`

error: transmute from a reference to a pointer
--> $DIR/transmutes_expressible_as_ptr_casts.rs:77:14
--> $DIR/transmutes_expressible_as_ptr_casts.rs:65:14
|
LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8`
Expand Down

0 comments on commit 5cb8deb

Please sign in to comment.