Update transmute size lints.

Also moves a few transmute tests to UI tests to better test their
output.
This commit is contained in:
Mark Simulacrum 2017-05-29 17:22:41 -06:00
parent 28cc0c5a7b
commit effa869cab
17 changed files with 247 additions and 33 deletions

View file

@ -12,5 +12,4 @@ fn takes_u8(_: u8) {}
fn main() {
unsafe { takes_u8(::std::mem::transmute(0u16)); } //~ ERROR E0512
//~| transmuting between 16 bits and 8 bits
}

View file

@ -15,7 +15,7 @@ trait Trait<'a> {
fn foo<'a, T: Trait<'a>>(value: T::A) {
let new: T::B = unsafe { std::mem::transmute(value) };
//~^ ERROR: transmute called with differently sized types
//~^ ERROR: transmute called with types of different sizes
}
fn main() { }

View file

@ -17,7 +17,7 @@ struct ArrayPeano<T: Bar> {
}
fn foo<T>(a: &ArrayPeano<T>) -> &[T] where T: Bar {
unsafe { std::mem::transmute(a) } //~ ERROR transmute called with differently sized types
unsafe { std::mem::transmute(a) } //~ ERROR transmute called with types of different sizes
}
impl Bar for () {

View file

@ -21,7 +21,7 @@ struct Bar<U: Foo> {
fn foo<U: Foo>(x: [usize; 2]) -> Bar<U> {
unsafe { mem::transmute(x) }
//~^ ERROR transmute called with differently sized types
//~^ ERROR transmute called with types of different sizes
}
fn main() {}

View file

@ -13,7 +13,7 @@
// the error points to the start of the file, not the line with the
// transmute
// error-pattern: transmute called with differently sized types
// error-pattern: transmute called with types of different sizes
use std::mem;

View file

@ -13,7 +13,7 @@
// the error points to the start of the file, not the line with the
// transmute
// error-pattern: transmute called with differently sized types
// error-pattern: transmute called with types of different sizes
use std::mem;

View file

@ -17,12 +17,12 @@ use std::mem::transmute;
unsafe fn f() {
let _: i8 = transmute(16i16);
//~^ ERROR transmute called with differently sized types
//~^ ERROR transmute called with types of different sizes
}
unsafe fn g<T>(x: &T) {
let _: i8 = transmute(x);
//~^ ERROR transmute called with differently sized types
//~^ ERROR transmute called with types of different sizes
}
trait Specializable { type Output; }
@ -33,7 +33,7 @@ impl<T> Specializable for T {
unsafe fn specializable<T>(x: u16) -> <T as Specializable>::Output {
transmute(x)
//~^ ERROR transmute called with differently sized types
//~^ ERROR transmute called with types of different sizes
}
fn main() {}

View file

@ -15,11 +15,11 @@
use std::mem::transmute;
fn a<T, U: ?Sized>(x: &[T]) -> &U {
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
}
fn b<T: ?Sized, U: ?Sized>(x: &T) -> &U {
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
}
fn c<T, U>(x: &T) -> &U {
@ -31,11 +31,11 @@ fn d<T, U>(x: &[T]) -> &[U] {
}
fn e<T: ?Sized, U>(x: &T) -> &U {
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
}
fn f<T, U: ?Sized>(x: &T) -> &U {
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
}
fn main() { }

View file

@ -26,7 +26,7 @@ impl<T: ?Sized> Foo<T> {
fn n(x: &T) -> &isize {
// Not OK here, because T : Sized is not in scope.
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
}
}

View file

@ -0,0 +1,36 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(untagged_unions)]
use std::mem::transmute;
pub trait TypeConstructor<'a> {
type T;
}
unsafe fn transmute_lifetime<'a, 'b, C>(x: <C as TypeConstructor<'a>>::T)
-> <C as TypeConstructor<'b>>::T
where for<'z> C: TypeConstructor<'z> {
transmute(x) //~ ERROR transmute called with types of different sizes
}
unsafe fn sizes() {
let x: u8 = transmute(10u16); //~ ERROR transmute called with types of different sizes
}
unsafe fn ptrs() {
let x: u8 = transmute("test"); //~ ERROR transmute called with types of different sizes
}
union Foo { x: () }
unsafe fn vary() {
let x: Foo = transmute(10); //~ ERROR transmute called with types of different sizes
}
fn main() {}

View file

@ -0,0 +1,38 @@
error[E0512]: transmute called with types of different sizes
--> $DIR/main.rs:20:5
|
20 | transmute(x) //~ ERROR transmute called with types of different sizes
| ^^^^^^^^^
|
= note: source type: <C as TypeConstructor<'a>>::T (size can vary because of <C as TypeConstructor>::T)
= note: target type: <C as TypeConstructor<'b>>::T (size can vary because of <C as TypeConstructor>::T)
error[E0512]: transmute called with types of different sizes
--> $DIR/main.rs:24:17
|
24 | let x: u8 = transmute(10u16); //~ ERROR transmute called with types of different sizes
| ^^^^^^^^^
|
= note: source type: u16 (16 bits)
= note: target type: u8 (8 bits)
error[E0512]: transmute called with types of different sizes
--> $DIR/main.rs:28:17
|
28 | let x: u8 = transmute("test"); //~ ERROR transmute called with types of different sizes
| ^^^^^^^^^
|
= note: source type: &str (128 bits)
= note: target type: u8 (8 bits)
error[E0512]: transmute called with types of different sizes
--> $DIR/main.rs:33:18
|
33 | let x: Foo = transmute(10); //~ ERROR transmute called with types of different sizes
| ^^^^^^^^^
|
= note: source type: i32 (32 bits)
= note: target type: Foo (0 bits)
error: aborting due to previous error(s)

View file

@ -29,8 +29,8 @@ unsafe fn foo() -> (isize, *const (), Option<fn()>) {
unsafe fn bar() {
// Error as usual if the resulting type is not pointer-sized.
mem::transmute::<_, u8>(main);
//~^ ERROR transmute called with differently sized types
//~^^ NOTE transmuting between 0 bits and 8 bits
//~^ ERROR transmute called with types of different sizes
//~^^ NOTE transmuting between fn() {main} and u8
mem::transmute::<_, *mut ()>(foo);
//~^ ERROR is zero-sized and can't be transmuted

View file

@ -0,0 +1,91 @@
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:14:13
|
14 | let i = mem::transmute(bar);
| ^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() {bar}
= note: target type: isize
= help: cast with `as` to a pointer instead
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:18:13
|
18 | let p = mem::transmute(foo);
| ^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() -> (isize, *const (), std::option::Option<fn()>) {foo}
= note: target type: *const ()
= help: cast with `as` to a pointer instead
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:22:14
|
22 | let of = mem::transmute(main);
| ^^^^^^^^^^^^^^
|
= note: source type: fn() {main}
= note: target type: std::option::Option<fn()>
= help: cast with `as` to a pointer instead
error[E0512]: transmute called with types of different sizes
--> $DIR/transmute-from-fn-item-types-error.rs:31:5
|
31 | mem::transmute::<_, u8>(main);
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: fn() {main} (0 bits)
= note: target type: u8 (8 bits)
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:35:5
|
35 | mem::transmute::<_, *mut ()>(foo);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() -> (isize, *const (), std::option::Option<fn()>) {foo}
= note: target type: *mut ()
= help: cast with `as` to a pointer instead
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:39:5
|
39 | mem::transmute::<_, fn()>(bar);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() {bar}
= note: target type: fn()
= help: cast with `as` to a pointer instead
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:48:5
|
48 | mem::transmute::<_, *mut ()>(Some(foo));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() -> (isize, *const (), std::option::Option<fn()>) {foo}
= note: target type: *mut ()
= help: cast with `as` to a pointer instead
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:52:5
|
52 | mem::transmute::<_, fn()>(Some(bar));
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() {bar}
= note: target type: fn()
= help: cast with `as` to a pointer instead
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:56:5
|
56 | mem::transmute::<_, Option<fn()>>(Some(baz));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() {baz}
= note: target type: std::option::Option<fn()>
= help: cast with `as` to a pointer instead
error: aborting due to previous error(s)

View file

@ -0,0 +1,56 @@
error[E0512]: transmute called with types of different sizes
--> $DIR/transmute-type-parameters.rs:16:20
|
16 | let _: isize = transmute(x);
| ^^^^^^^^^
|
= note: source type: T (this type's size can vary)
= note: target type: isize (64 bits)
error[E0512]: transmute called with types of different sizes
--> $DIR/transmute-type-parameters.rs:21:20
|
21 | let _: isize = transmute(x);
| ^^^^^^^^^
|
= note: source type: (T, isize) (size can vary because of T)
= note: target type: isize (64 bits)
error[E0512]: transmute called with types of different sizes
--> $DIR/transmute-type-parameters.rs:26:20
|
26 | let _: isize = transmute(x);
| ^^^^^^^^^
|
= note: source type: [T; 10] (size can vary because of T)
= note: target type: isize (64 bits)
error[E0512]: transmute called with types of different sizes
--> $DIR/transmute-type-parameters.rs:35:20
|
35 | let _: isize = transmute(x);
| ^^^^^^^^^
|
= note: source type: Bad<T> (size can vary because of T)
= note: target type: isize (64 bits)
error[E0512]: transmute called with types of different sizes
--> $DIR/transmute-type-parameters.rs:45:20
|
45 | let _: isize = transmute(x);
| ^^^^^^^^^
|
= note: source type: Worse<T> (size can vary because of T)
= note: target type: isize (64 bits)
error[E0512]: transmute called with types of different sizes
--> $DIR/transmute-type-parameters.rs:50:20
|
50 | let _: isize = transmute(x);
| ^^^^^^^^^
|
= note: source type: std::option::Option<T> (size can vary because of T)
= note: target type: isize (64 bits)
error: aborting due to previous error(s)