Fix sync fallout

This commit is contained in:
flip1995 2020-08-11 13:57:32 +02:00
parent 9e73d33680
commit 9311c11d7c
No known key found for this signature in database
GPG key ID: 2CEFCDB27ED0BE79
5 changed files with 59 additions and 80 deletions

View file

@ -61,12 +61,14 @@ declare_clippy_lint! {
/// ///
/// **Example:** /// **Example:**
/// ///
/// ```rust,ignore /// ```rust
/// core::intrinsics::transmute::<*const [i32], *const [u16]>(p) /// # let p: *const [i32] = &[];
/// unsafe { std::mem::transmute::<*const [i32], *const [u16]>(p) };
/// ``` /// ```
/// Use instead: /// Use instead:
/// ```rust /// ```rust
/// p as *const [u16] /// # let p: *const [i32] = &[];
/// p as *const [u16];
/// ``` /// ```
pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, pub TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS,
complexity, complexity,
@ -704,14 +706,14 @@ fn can_be_expressed_as_pointer_cast<'tcx>(
from_ty: Ty<'tcx>, from_ty: Ty<'tcx>,
to_ty: Ty<'tcx>, to_ty: Ty<'tcx>,
) -> bool { ) -> bool {
use CastKind::*; use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast};
matches!( matches!(
check_cast(cx, e, from_ty, to_ty), check_cast(cx, e, from_ty, to_ty),
Some(PtrPtrCast | PtrAddrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast) 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 /// 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 /// to pointers, this may cause additional errors to be emitted and/or ICE error
/// messages. This function will panic if that occurs. /// messages. This function will panic if that occurs.

View file

@ -147,9 +147,6 @@ fn run_ui_toml(config: &mut compiletest::Config) {
} }
fn run_ui_cargo(config: &mut compiletest::Config) { fn run_ui_cargo(config: &mut compiletest::Config) {
if cargo::is_rustc_test_suite() {
return;
}
fn run_tests( fn run_tests(
config: &compiletest::Config, config: &compiletest::Config,
filter: &Option<String>, filter: &Option<String>,
@ -217,6 +214,10 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
Ok(result) Ok(result)
} }
if cargo::is_rustc_test_suite() {
return;
}
config.mode = TestMode::Ui; config.mode = TestMode::Ui;
config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap(); config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();

View file

@ -9,60 +9,48 @@
use std::mem::{size_of, transmute}; 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. // valid, which we quote from below.
fn main() { fn main() {
// We should see an error message for each transmute, and no error messages for // We should see an error message for each transmute, and no error messages for
// the casts, since the casts are the recommended fixes. // 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 // e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
let _ptr_i32_transmute = unsafe { let _ptr_i32_transmute = unsafe { usize::MAX as *const i32 };
usize::MAX as *const i32
};
let ptr_i32 = 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 ... // e has type *T, U is *U_0, and either U_0: Sized ...
let _ptr_i8_transmute = unsafe { let _ptr_i8_transmute = unsafe { ptr_i32 as *const i8 };
ptr_i32 as *const i8
};
let _ptr_i8 = 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 // ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
let _ptr_to_unsized_transmute = unsafe { let _ptr_to_unsized_transmute = unsafe { slice_ptr as *const [u16] };
slice_ptr as *const [u16]
};
let _ptr_to_unsized = 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 // TODO: We could try testing vtable casts here too, but maybe
// we should wait until std::raw::TraitObject is stabilized? // 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 // e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
let _usize_from_int_ptr_transmute = unsafe { let _usize_from_int_ptr_transmute = unsafe { ptr_i32 as usize };
ptr_i32 as usize
};
let _usize_from_int_ptr = 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 // e has type &[T; n] and U is *const T; array-ptr-cast
let _array_ptr_transmute = unsafe { let _array_ptr_transmute = unsafe { array_ref as *const [i32; 4] };
array_ref as *const [i32; 4]
};
let _array_ptr = 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 // e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
let _usize_ptr_transmute = unsafe { let _usize_ptr_transmute = unsafe { foo as *const usize };
foo as *const usize
};
let _usize_ptr_transmute = 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 // e is a function pointer type and U is an integer; fptr-addr-cast
let _usize_from_fn_ptr_transmute = unsafe { let _usize_from_fn_ptr_transmute = unsafe { foo as usize };
foo as usize
};
let _usize_from_fn_ptr = foo as *const usize; let _usize_from_fn_ptr = foo as *const usize;
} }

View file

@ -9,60 +9,48 @@
use std::mem::{size_of, transmute}; 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. // valid, which we quote from below.
fn main() { fn main() {
// We should see an error message for each transmute, and no error messages for // We should see an error message for each transmute, and no error messages for
// the casts, since the casts are the recommended fixes. // 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 // e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
let _ptr_i32_transmute = unsafe { let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
transmute::<usize, *const i32>(usize::MAX)
};
let ptr_i32 = 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 ... // e has type *T, U is *U_0, and either U_0: Sized ...
let _ptr_i8_transmute = unsafe { let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
transmute::<*const i32, *const i8>(ptr_i32)
};
let _ptr_i8 = 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 // ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
let _ptr_to_unsized_transmute = unsafe { let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u16]>(slice_ptr) };
transmute::<*const [i32], *const [u16]>(slice_ptr)
};
let _ptr_to_unsized = 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 // TODO: We could try testing vtable casts here too, but maybe
// we should wait until std::raw::TraitObject is stabilized? // 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 // e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
let _usize_from_int_ptr_transmute = unsafe { let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
transmute::<*const i32, usize>(ptr_i32)
};
let _usize_from_int_ptr = 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 // e has type &[T; n] and U is *const T; array-ptr-cast
let _array_ptr_transmute = unsafe { let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
transmute::<&[i32; 4], *const [i32; 4]>(array_ref)
};
let _array_ptr = 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 // e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
let _usize_ptr_transmute = unsafe { let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
transmute::<fn(usize) -> u8, *const usize>(foo)
};
let _usize_ptr_transmute = 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 // e is a function pointer type and U is an integer; fptr-addr-cast
let _usize_from_fn_ptr_transmute = unsafe { let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
transmute::<fn(usize) -> u8, usize>(foo)
};
let _usize_from_fn_ptr = foo as *const usize; let _usize_from_fn_ptr = foo as *const usize;
} }

View file

@ -1,53 +1,53 @@
error: transmute from an integer to a pointer 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) LL | let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `usize::MAX as *const i32` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `usize::MAX as *const i32`
| |
= note: `-D clippy::useless-transmute` implied by `-D warnings` = note: `-D clippy::useless-transmute` implied by `-D warnings`
error: transmute from a pointer to a pointer 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) LL | let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as *const i8`
| |
= note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings` = note: `-D clippy::transmute-ptr-to-ptr` implied by `-D warnings`
error: transmute from a pointer to a pointer 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) LL | let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u16]>(slice_ptr) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `slice_ptr as *const [u16]`
error: transmute from `*const i32` to `usize` which could be expressed as a pointer cast instead 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) LL | let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `ptr_i32 as usize`
| |
= note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings` = note: `-D clippy::transmutes-expressible-as-ptr-casts` implied by `-D warnings`
error: transmute from a reference to a pointer 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) LL | let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 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) LL | let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 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) LL | let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as usize`
error: transmute from a reference to a pointer 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) } LL | unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `in_param as *const [i32; 1] as *const u8`