Skip to content

Commit 7379620

Browse files
committed
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) ```
2 parents bd62230 + d09cf46 commit 7379620

17 files changed

+296
-49
lines changed

src/librustc/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ fn takes_u8(_: u8) {}
16311631
16321632
fn main() {
16331633
unsafe { takes_u8(::std::mem::transmute(0u16)); }
1634-
// error: transmute called with differently sized types
1634+
// error: transmute called with types of different sizes
16351635
}
16361636
```
16371637

src/librustc/middle/intrinsicck.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,16 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
8686
// Special-case transmutting from `typeof(function)` and
8787
// `Option<typeof(function)>` to present a clearer error.
8888
let from = unpack_option_like(self.tcx.global_tcx(), from);
89-
match (&from.sty, sk_to) {
90-
(&ty::TyFnDef(..), SizeSkeleton::Known(size_to))
91-
if size_to == Pointer.size(self.tcx) => {
89+
if let (&ty::TyFnDef(..), SizeSkeleton::Known(size_to)) = (&from.sty, sk_to) {
90+
if size_to == Pointer.size(self.tcx) {
9291
struct_span_err!(self.tcx.sess, span, E0591,
93-
"`{}` is zero-sized and can't be transmuted to `{}`",
94-
from, to)
95-
.span_note(span, "cast with `as` to a pointer instead")
92+
"can't transmute zero-sized type")
93+
.note(&format!("source type: {}", from))
94+
.note(&format!("target type: {}", to))
95+
.help("cast with `as` to a pointer instead")
9696
.emit();
9797
return;
9898
}
99-
_ => {}
10099
}
101100
}
102101

@@ -111,7 +110,7 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
111110
}
112111
Err(LayoutError::Unknown(bad)) => {
113112
if bad == ty {
114-
format!("size can vary")
113+
format!("this type's size can vary")
115114
} else {
116115
format!("size can vary because of {}", bad)
117116
}
@@ -121,14 +120,9 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
121120
};
122121

123122
struct_span_err!(self.tcx.sess, span, E0512,
124-
"transmute called with differently sized types: \
125-
{} ({}) to {} ({})",
126-
from, skeleton_string(from, sk_from),
127-
to, skeleton_string(to, sk_to))
128-
.span_label(span,
129-
format!("transmuting between {} and {}",
130-
skeleton_string(from, sk_from),
131-
skeleton_string(to, sk_to)))
123+
"transmute called with types of different sizes")
124+
.note(&format!("source type: {} ({})", from, skeleton_string(from, sk_from)))
125+
.note(&format!("target type: {} ({})", to, skeleton_string(to, sk_to)))
132126
.emit();
133127
}
134128
}

src/test/compile-fail/E0512.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ fn takes_u8(_: u8) {}
1212

1313
fn main() {
1414
unsafe { takes_u8(::std::mem::transmute(0u16)); } //~ ERROR E0512
15-
//~| transmuting between 16 bits and 8 bits
1615
}

src/test/compile-fail/issue-21174.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait Trait<'a> {
1515

1616
fn foo<'a, T: Trait<'a>>(value: T::A) {
1717
let new: T::B = unsafe { std::mem::transmute(value) };
18-
//~^ ERROR: transmute called with differently sized types
18+
//~^ ERROR: transmute called with types of different sizes
1919
}
2020

2121
fn main() { }

src/test/compile-fail/issue-28625.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct ArrayPeano<T: Bar> {
1717
}
1818

1919
fn foo<T>(a: &ArrayPeano<T>) -> &[T] where T: Bar {
20-
unsafe { std::mem::transmute(a) } //~ ERROR transmute called with differently sized types
20+
unsafe { std::mem::transmute(a) } //~ ERROR transmute called with types of different sizes
2121
}
2222

2323
impl Bar for () {

src/test/compile-fail/issue-32377.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Bar<U: Foo> {
2121

2222
fn foo<U: Foo>(x: [usize; 2]) -> Bar<U> {
2323
unsafe { mem::transmute(x) }
24-
//~^ ERROR transmute called with differently sized types
24+
//~^ ERROR transmute called with types of different sizes
2525
}
2626

2727
fn main() {}

src/test/compile-fail/packed-struct-generic-transmute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// the error points to the start of the file, not the line with the
1414
// transmute
1515

16-
// error-pattern: transmute called with differently sized types
16+
// error-pattern: transmute called with types of different sizes
1717

1818
use std::mem;
1919

src/test/compile-fail/packed-struct-transmute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// the error points to the start of the file, not the line with the
1414
// transmute
1515

16-
// error-pattern: transmute called with differently sized types
16+
// error-pattern: transmute called with types of different sizes
1717

1818
use std::mem;
1919

src/test/compile-fail/transmute-different-sizes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ use std::mem::transmute;
1717

1818
unsafe fn f() {
1919
let _: i8 = transmute(16i16);
20-
//~^ ERROR transmute called with differently sized types
20+
//~^ ERROR transmute called with types of different sizes
2121
}
2222

2323
unsafe fn g<T>(x: &T) {
2424
let _: i8 = transmute(x);
25-
//~^ ERROR transmute called with differently sized types
25+
//~^ ERROR transmute called with types of different sizes
2626
}
2727

2828
trait Specializable { type Output; }
@@ -33,7 +33,7 @@ impl<T> Specializable for T {
3333

3434
unsafe fn specializable<T>(x: u16) -> <T as Specializable>::Output {
3535
transmute(x)
36-
//~^ ERROR transmute called with differently sized types
36+
//~^ ERROR transmute called with types of different sizes
3737
}
3838

3939
fn main() {}

src/test/compile-fail/transmute-fat-pointers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use std::mem::transmute;
1616

1717
fn a<T, U: ?Sized>(x: &[T]) -> &U {
18-
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
18+
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
1919
}
2020

2121
fn b<T: ?Sized, U: ?Sized>(x: &T) -> &U {
22-
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
22+
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
2323
}
2424

2525
fn c<T, U>(x: &T) -> &U {
@@ -31,11 +31,11 @@ fn d<T, U>(x: &[T]) -> &[U] {
3131
}
3232

3333
fn e<T: ?Sized, U>(x: &T) -> &U {
34-
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
34+
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
3535
}
3636

3737
fn f<T, U: ?Sized>(x: &T) -> &U {
38-
unsafe { transmute(x) } //~ ERROR transmute called with differently sized types
38+
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
3939
}
4040

4141
fn main() { }

0 commit comments

Comments
 (0)