Auto merge of #42304 - Mark-Simulacrum:issue-37157, r=nikomatsakis

Print the two types in the span label for transmute errors.

Fixes #37157. I'm not entirely happy with the changes here but overall it's better in my opinion; we certainly avoid the odd language in that issue, which changes to:

```
error[E0512]: transmute called with differently sized types: <C as TypeConstructor<'a>>::T (size can vary because of <C as TypeConstructor>::T) to <C as TypeConstructor<'b>>::T (size can vary because of <C as TypeConstructor>::T)
 --> test.rs:8:5
  |
8 |     ::std::mem::transmute(x)
  |     ^^^^^^^^^^^^^^^^^^^^^ transmuting between <C as TypeConstructor<'a>>::T and <C as TypeConstructor<'b>>::T

error: aborting due to previous error(s)
```
This commit is contained in:
bors 2017-06-23 00:32:40 +00:00
commit 737962056a
17 changed files with 296 additions and 49 deletions

View File

@ -1631,7 +1631,7 @@ fn takes_u8(_: u8) {}
fn main() {
unsafe { takes_u8(::std::mem::transmute(0u16)); }
// error: transmute called with differently sized types
// error: transmute called with types of different sizes
}
```

View File

@ -86,17 +86,16 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
// Special-case transmutting from `typeof(function)` and
// `Option<typeof(function)>` to present a clearer error.
let from = unpack_option_like(self.tcx.global_tcx(), from);
match (&from.sty, sk_to) {
(&ty::TyFnDef(..), SizeSkeleton::Known(size_to))
if size_to == Pointer.size(self.tcx) => {
if let (&ty::TyFnDef(..), SizeSkeleton::Known(size_to)) = (&from.sty, sk_to) {
if size_to == Pointer.size(self.tcx) {
struct_span_err!(self.tcx.sess, span, E0591,
"`{}` is zero-sized and can't be transmuted to `{}`",
from, to)
.span_note(span, "cast with `as` to a pointer instead")
"can't transmute zero-sized type")
.note(&format!("source type: {}", from))
.note(&format!("target type: {}", to))
.help("cast with `as` to a pointer instead")
.emit();
return;
}
_ => {}
}
}
@ -111,7 +110,7 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
}
Err(LayoutError::Unknown(bad)) => {
if bad == ty {
format!("size can vary")
format!("this type's size can vary")
} else {
format!("size can vary because of {}", bad)
}
@ -121,14 +120,9 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
};
struct_span_err!(self.tcx.sess, span, E0512,
"transmute called with differently sized types: \
{} ({}) to {} ({})",
from, skeleton_string(from, sk_from),
to, skeleton_string(to, sk_to))
.span_label(span,
format!("transmuting between {} and {}",
skeleton_string(from, sk_from),
skeleton_string(to, sk_to)))
"transmute called with types of different sizes")
.note(&format!("source type: {} ({})", from, skeleton_string(from, sk_from)))
.note(&format!("target type: {} ({})", to, skeleton_string(to, sk_to)))
.emit();
}
}

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,42 @@
// 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.
// ignore-x86
// ignore-arm
// ignore-emscripten
// ignore 32-bit platforms (test output is different)
#![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:26:5
|
26 | 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:30:17
|
30 | 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:34:17
|
34 | 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:39:18
|
39 | 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

@ -8,9 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-x86
// ignore-arm
// ignore-emscripten
// ignore 32-bit platforms (test output is different)
use std::mem;
unsafe fn foo() -> (isize, *const (), Option<fn()>) {
unsafe fn foo() -> (i32, *const (), Option<fn()>) {
let i = mem::transmute(bar);
//~^ ERROR is zero-sized and can't be transmuted
//~^^ NOTE cast with `as` to a pointer instead
@ -29,8 +34,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
@ -41,7 +46,7 @@ unsafe fn bar() {
//~^^ NOTE cast with `as` to a pointer instead
// No error if a coercion would otherwise occur.
mem::transmute::<fn(), usize>(main);
mem::transmute::<fn(), u32>(main);
}
unsafe fn baz() {
@ -58,7 +63,7 @@ unsafe fn baz() {
//~^^ NOTE cast with `as` to a pointer instead
// No error if a coercion would otherwise occur.
mem::transmute::<Option<fn()>, usize>(Some(main));
mem::transmute::<Option<fn()>, u32>(Some(main));
}
fn main() {

View File

@ -0,0 +1,108 @@
error[E0512]: transmute called with types of different sizes
--> $DIR/transmute-from-fn-item-types-error.rs:19:13
|
19 | let i = mem::transmute(bar);
| ^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() {bar} (0 bits)
= note: target type: i32 (32 bits)
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:23:13
|
23 | let p = mem::transmute(foo);
| ^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() -> (i32, *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:27:14
|
27 | 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:36:5
|
36 | 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:40:5
|
40 | mem::transmute::<_, *mut ()>(foo);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() -> (i32, *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:44:5
|
44 | mem::transmute::<_, fn()>(bar);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() {bar}
= note: target type: 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:49:5
|
49 | mem::transmute::<fn(), u32>(main);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: fn() (64 bits)
= note: target type: u32 (32 bits)
error[E0591]: can't transmute zero-sized type
--> $DIR/transmute-from-fn-item-types-error.rs:53:5
|
53 | mem::transmute::<_, *mut ()>(Some(foo));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: unsafe fn() -> (i32, *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:57:5
|
57 | 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:61:5
|
61 | 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[E0512]: transmute called with types of different sizes
--> $DIR/transmute-from-fn-item-types-error.rs:66:5
|
66 | mem::transmute::<Option<fn()>, u32>(Some(main));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: source type: std::option::Option<fn()> (64 bits)
= note: target type: u32 (32 bits)
error: aborting due to previous error(s)

View File

@ -8,23 +8,28 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// ignore-x86
// ignore-arm
// ignore-emscripten
// ignore 32-bit platforms (test output is different)
// Tests that `transmute` cannot be called on type parameters.
use std::mem::transmute;
unsafe fn f<T>(x: T) {
let _: isize = transmute(x);
//~^ ERROR differently sized types: T (size can vary) to isize
let _: i32 = transmute(x);
//~^ ERROR differently sized types: T (size can vary) to i32
}
unsafe fn g<T>(x: (T, isize)) {
let _: isize = transmute(x);
//~^ ERROR differently sized types: (T, isize) (size can vary because of T) to isize
unsafe fn g<T>(x: (T, i32)) {
let _: i32 = transmute(x);
//~^ ERROR differently sized types: (T, i32) (size can vary because of T) to i32
}
unsafe fn h<T>(x: [T; 10]) {
let _: isize = transmute(x);
//~^ ERROR differently sized types: [T; 10] (size can vary because of T) to isize
let _: i32 = transmute(x);
//~^ ERROR differently sized types: [T; 10] (size can vary because of T) to i32
}
struct Bad<T> {
@ -32,8 +37,8 @@ struct Bad<T> {
}
unsafe fn i<T>(x: Bad<T>) {
let _: isize = transmute(x);
//~^ ERROR differently sized types: Bad<T> (size can vary because of T) to isize
let _: i32 = transmute(x);
//~^ ERROR differently sized types: Bad<T> (size can vary because of T) to i32
}
enum Worse<T> {
@ -42,13 +47,13 @@ enum Worse<T> {
}
unsafe fn j<T>(x: Worse<T>) {
let _: isize = transmute(x);
//~^ ERROR differently sized types: Worse<T> (size can vary because of T) to isize
let _: i32 = transmute(x);
//~^ ERROR differently sized types: Worse<T> (size can vary because of T) to i32
}
unsafe fn k<T>(x: Option<T>) {
let _: isize = transmute(x);
//~^ ERROR differently sized types: std::option::Option<T> (size can vary because of T) to isize
let _: i32 = transmute(x);
//~^ ERROR differently sized types: std::option::Option<T> (size can vary because of T) to i32
}
fn main() {}

View File

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