Skip to content

Commit f39bd9b

Browse files
committed
Auto merge of #57044 - varkor:E0512-equal-type, r=matthewjasper
Add specific diagnostic when attempting to transmute between equal generic types Also clarifies the wording of E0512. Fixes #49793.
2 parents 2cf7f55 + 7d5f6ce commit f39bd9b

27 files changed

+149
-123
lines changed

src/librustc/diagnostics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,8 @@ fn takes_u8(_: u8) {}
15521552
15531553
fn main() {
15541554
unsafe { takes_u8(::std::mem::transmute(0u16)); }
1555-
// error: transmute called with types of different sizes
1555+
// error: cannot transmute between types of different sizes,
1556+
// or dependently-sized types
15561557
}
15571558
```
15581559

src/librustc/middle/intrinsicck.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
9797
format!("{} bits", size.bits())
9898
}
9999
Ok(SizeSkeleton::Pointer { tail, .. }) => {
100-
format!("pointer to {}", tail)
100+
format!("pointer to `{}`", tail)
101101
}
102102
Err(LayoutError::Unknown(bad)) => {
103103
if bad == ty {
104-
"this type's size can vary".to_owned()
104+
"this type does not have a fixed size".to_owned()
105105
} else {
106106
format!("size can vary because of {}", bad)
107107
}
@@ -110,11 +110,16 @@ impl<'a, 'tcx> ExprVisitor<'a, 'tcx> {
110110
}
111111
};
112112

113-
struct_span_err!(self.tcx.sess, span, E0512,
114-
"transmute called with types of different sizes")
115-
.note(&format!("source type: {} ({})", from, skeleton_string(from, sk_from)))
116-
.note(&format!("target type: {} ({})", to, skeleton_string(to, sk_to)))
117-
.emit();
113+
let mut err = struct_span_err!(self.tcx.sess, span, E0512,
114+
"cannot transmute between types of different sizes, \
115+
or dependently-sized types");
116+
if from == to {
117+
err.note(&format!("`{}` does not have a fixed size", from));
118+
} else {
119+
err.note(&format!("source type: `{}` ({})", from, skeleton_string(from, sk_from)))
120+
.note(&format!("target type: `{}` ({})", to, skeleton_string(to, sk_to)));
121+
}
122+
err.emit()
118123
}
119124
}
120125

src/test/ui/error-codes/E0512.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0512]: transmute called with types of different sizes
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
22
--> $DIR/E0512.rs:4:23
33
|
44
LL | unsafe { takes_u8(::std::mem::transmute(0u16)); } //~ ERROR E0512
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: u16 (16 bits)
8-
= note: target type: u8 (8 bits)
7+
= note: source type: `u16` (16 bits)
8+
= note: target type: `u8` (8 bits)
99

1010
error: aborting due to previous error
1111

src/test/ui/issues/issue-21174.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ trait Trait<'a> {
55

66
fn foo<'a, T: Trait<'a>>(value: T::A) {
77
let new: T::B = unsafe { std::mem::transmute(value) };
8-
//~^ ERROR: transmute called with types of different sizes
8+
//~^ ERROR: cannot transmute between types of different sizes, or dependently-sized types
99
}
1010

1111
fn main() { }

src/test/ui/issues/issue-21174.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0512]: transmute called with types of different sizes
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
22
--> $DIR/issue-21174.rs:7:30
33
|
44
LL | let new: T::B = unsafe { std::mem::transmute(value) };
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: <T as Trait<'a>>::A (size can vary because of <T as Trait>::A)
8-
= note: target type: <T as Trait<'a>>::B (size can vary because of <T as Trait>::B)
7+
= note: source type: `<T as Trait<'a>>::A` (size can vary because of <T as Trait>::A)
8+
= note: target type: `<T as Trait<'a>>::B` (size can vary because of <T as Trait>::B)
99

1010
error: aborting due to previous error
1111

src/test/ui/issues/issue-28625.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct ArrayPeano<T: Bar> {
99
}
1010

1111
fn foo<T>(a: &ArrayPeano<T>) -> &[T] where T: Bar {
12-
unsafe { std::mem::transmute(a) } //~ ERROR transmute called with types of different sizes
12+
unsafe { std::mem::transmute(a) } //~ ERROR cannot transmute between types of different sizes
1313
}
1414

1515
impl Bar for () {

src/test/ui/issues/issue-28625.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0512]: transmute called with types of different sizes
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
22
--> $DIR/issue-28625.rs:12:14
33
|
4-
LL | unsafe { std::mem::transmute(a) } //~ ERROR transmute called with types of different sizes
4+
LL | unsafe { std::mem::transmute(a) } //~ ERROR cannot transmute between types of different sizes
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: &ArrayPeano<T> (N bits)
8-
= note: target type: &[T] (N bits)
7+
= note: source type: `&ArrayPeano<T>` (N bits)
8+
= note: target type: `&[T]` (N bits)
99

1010
error: aborting due to previous error
1111

src/test/ui/issues/issue-32377.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Bar<U: Foo> {
1313

1414
fn foo<U: Foo>(x: [usize; 2]) -> Bar<U> {
1515
unsafe { mem::transmute(x) }
16-
//~^ ERROR transmute called with types of different sizes
16+
//~^ ERROR cannot transmute between types of different sizes, or dependently-sized types
1717
}
1818

1919
fn main() {}

src/test/ui/issues/issue-32377.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0512]: transmute called with types of different sizes
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
22
--> $DIR/issue-32377.rs:15:14
33
|
44
LL | unsafe { mem::transmute(x) }
55
| ^^^^^^^^^^^^^^
66
|
7-
= note: source type: [usize; 2] (N bits)
8-
= note: target type: Bar<U> (N bits)
7+
= note: source type: `[usize; 2]` (N bits)
8+
= note: target type: `Bar<U>` (N bits)
99

1010
error: aborting due to previous error
1111

src/test/ui/packed-struct/packed-struct-generic-transmute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// the error points to the start of the file, not the line with the
44
// transmute
55

6-
// error-pattern: transmute called with types of different sizes
6+
// error-pattern: cannot transmute between types of different sizes, or dependently-sized types
77

88
use std::mem;
99

src/test/ui/packed-struct/packed-struct-generic-transmute.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0512]: transmute called with types of different sizes
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
22
--> $DIR/packed-struct-generic-transmute.rs:24:38
33
|
44
LL | let oof: Oof<[u8; 5], i32> = mem::transmute(foo);
55
| ^^^^^^^^^^^^^^
66
|
7-
= note: source type: Foo<[u8; 5], i32> (72 bits)
8-
= note: target type: Oof<[u8; 5], i32> (96 bits)
7+
= note: source type: `Foo<[u8; 5], i32>` (72 bits)
8+
= note: target type: `Oof<[u8; 5], i32>` (96 bits)
99

1010
error: aborting due to previous error
1111

src/test/ui/packed-struct/packed-struct-transmute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// transmute
55

66
// normalize-stderr-test "\d+ bits" -> "N bits"
7-
// error-pattern: transmute called with types of different sizes
7+
// error-pattern: cannot transmute between types of different sizes, or dependently-sized types
88

99
use std::mem;
1010

src/test/ui/packed-struct/packed-struct-transmute.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0512]: transmute called with types of different sizes
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
22
--> $DIR/packed-struct-transmute.rs:26:24
33
|
44
LL | let oof: Oof = mem::transmute(foo);
55
| ^^^^^^^^^^^^^^
66
|
7-
= note: source type: Foo (N bits)
8-
= note: target type: Oof (N bits)
7+
= note: source type: `Foo` (N bits)
8+
= note: target type: `Oof` (N bits)
99

1010
error: aborting due to previous error
1111

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
trait Foo {
2+
type Bar;
3+
}
4+
5+
unsafe fn noop<F: Foo>(foo: F::Bar) -> F::Bar {
6+
::std::mem::transmute(foo) //~ ERROR cannot transmute between types of different sizes
7+
}
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2+
--> $DIR/transmute-equal-assoc-types.rs:6:5
3+
|
4+
LL | ::std::mem::transmute(foo) //~ ERROR cannot transmute between types of different sizes
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `<F as Foo>::Bar` does not have a fixed size
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0512`.

src/test/ui/transmute/main.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// normalize-stderr-32bit: "&str \(64 bits\)" -> "&str ($$STR bits)"
2-
// normalize-stderr-64bit: "&str \(128 bits\)" -> "&str ($$STR bits)"
1+
// normalize-stderr-32bit: "`&str` \(64 bits\)" -> "`&str` ($$STR bits)"
2+
// normalize-stderr-64bit: "`&str` \(128 bits\)" -> "`&str` ($$STR bits)"
33

44

55

@@ -13,20 +13,20 @@ pub trait TypeConstructor<'a> {
1313
unsafe fn transmute_lifetime<'a, 'b, C>(x: <C as TypeConstructor<'a>>::T)
1414
-> <C as TypeConstructor<'b>>::T
1515
where for<'z> C: TypeConstructor<'z> {
16-
transmute(x) //~ ERROR transmute called with types of different sizes
16+
transmute(x) //~ ERROR cannot transmute between types of different sizes
1717
}
1818

1919
unsafe fn sizes() {
20-
let x: u8 = transmute(10u16); //~ ERROR transmute called with types of different sizes
20+
let x: u8 = transmute(10u16); //~ ERROR cannot transmute between types of different sizes
2121
}
2222

2323
unsafe fn ptrs() {
24-
let x: u8 = transmute("test"); //~ ERROR transmute called with types of different sizes
24+
let x: u8 = transmute("test"); //~ ERROR cannot transmute between types of different sizes
2525
}
2626

2727
union Foo { x: () }
2828
unsafe fn vary() {
29-
let x: Foo = transmute(10); //~ ERROR transmute called with types of different sizes
29+
let x: Foo = transmute(10); //~ ERROR cannot transmute between types of different sizes
3030
}
3131

3232
fn main() {}

src/test/ui/transmute/main.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
error[E0512]: transmute called with types of different sizes
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
22
--> $DIR/main.rs:16:5
33
|
4-
LL | transmute(x) //~ ERROR transmute called with types of different sizes
4+
LL | transmute(x) //~ ERROR cannot transmute between types of different sizes
55
| ^^^^^^^^^
66
|
7-
= note: source type: <C as TypeConstructor<'a>>::T (size can vary because of <C as TypeConstructor>::T)
8-
= note: target type: <C as TypeConstructor<'b>>::T (size can vary because of <C as TypeConstructor>::T)
7+
= note: source type: `<C as TypeConstructor<'a>>::T` (size can vary because of <C as TypeConstructor>::T)
8+
= note: target type: `<C as TypeConstructor<'b>>::T` (size can vary because of <C as TypeConstructor>::T)
99

10-
error[E0512]: transmute called with types of different sizes
10+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
1111
--> $DIR/main.rs:20:17
1212
|
13-
LL | let x: u8 = transmute(10u16); //~ ERROR transmute called with types of different sizes
13+
LL | let x: u8 = transmute(10u16); //~ ERROR cannot transmute between types of different sizes
1414
| ^^^^^^^^^
1515
|
16-
= note: source type: u16 (16 bits)
17-
= note: target type: u8 (8 bits)
16+
= note: source type: `u16` (16 bits)
17+
= note: target type: `u8` (8 bits)
1818

19-
error[E0512]: transmute called with types of different sizes
19+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2020
--> $DIR/main.rs:24:17
2121
|
22-
LL | let x: u8 = transmute("test"); //~ ERROR transmute called with types of different sizes
22+
LL | let x: u8 = transmute("test"); //~ ERROR cannot transmute between types of different sizes
2323
| ^^^^^^^^^
2424
|
25-
= note: source type: &str ($STR bits)
26-
= note: target type: u8 (8 bits)
25+
= note: source type: `&str` ($STR bits)
26+
= note: target type: `u8` (8 bits)
2727

28-
error[E0512]: transmute called with types of different sizes
28+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2929
--> $DIR/main.rs:29:18
3030
|
31-
LL | let x: Foo = transmute(10); //~ ERROR transmute called with types of different sizes
31+
LL | let x: Foo = transmute(10); //~ ERROR cannot transmute between types of different sizes
3232
| ^^^^^^^^^
3333
|
34-
= note: source type: i32 (32 bits)
35-
= note: target type: Foo (0 bits)
34+
= note: source type: `i32` (32 bits)
35+
= note: target type: `Foo` (0 bits)
3636

3737
error: aborting due to 4 previous errors
3838

src/test/ui/transmute/transmute-different-sizes.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ use std::mem::transmute;
99

1010
unsafe fn f() {
1111
let _: i8 = transmute(16i16);
12-
//~^ ERROR transmute called with types of different sizes
12+
//~^ ERROR cannot transmute between types of different sizes, or dependently-sized types
1313
}
1414

1515
unsafe fn g<T>(x: &T) {
1616
let _: i8 = transmute(x);
17-
//~^ ERROR transmute called with types of different sizes
17+
//~^ ERROR cannot transmute between types of different sizes, or dependently-sized types
1818
}
1919

2020
trait Specializable { type Output; }
@@ -25,7 +25,7 @@ impl<T> Specializable for T {
2525

2626
unsafe fn specializable<T>(x: u16) -> <T as Specializable>::Output {
2727
transmute(x)
28-
//~^ ERROR transmute called with types of different sizes
28+
//~^ ERROR cannot transmute between types of different sizes, or dependently-sized types
2929
}
3030

3131
fn main() {}

src/test/ui/transmute/transmute-different-sizes.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
error[E0512]: transmute called with types of different sizes
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
22
--> $DIR/transmute-different-sizes.rs:11:17
33
|
44
LL | let _: i8 = transmute(16i16);
55
| ^^^^^^^^^
66
|
7-
= note: source type: i16 (N bits)
8-
= note: target type: i8 (N bits)
7+
= note: source type: `i16` (N bits)
8+
= note: target type: `i8` (N bits)
99

10-
error[E0512]: transmute called with types of different sizes
10+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
1111
--> $DIR/transmute-different-sizes.rs:16:17
1212
|
1313
LL | let _: i8 = transmute(x);
1414
| ^^^^^^^^^
1515
|
16-
= note: source type: &T (N bits)
17-
= note: target type: i8 (N bits)
16+
= note: source type: `&T` (N bits)
17+
= note: target type: `i8` (N bits)
1818

19-
error[E0512]: transmute called with types of different sizes
19+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2020
--> $DIR/transmute-different-sizes.rs:27:5
2121
|
2222
LL | transmute(x)
2323
| ^^^^^^^^^
2424
|
25-
= note: source type: u16 (N bits)
26-
= note: target type: <T as Specializable>::Output (this type's size can vary)
25+
= note: source type: `u16` (N bits)
26+
= note: target type: `<T as Specializable>::Output` (this type does not have a fixed size)
2727

2828
error: aborting due to 3 previous errors
2929

src/test/ui/transmute/transmute-fat-pointers.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use std::mem::transmute;
88

99
fn a<T, U: ?Sized>(x: &[T]) -> &U {
10-
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
10+
unsafe { transmute(x) } //~ ERROR cannot transmute between types of different sizes
1111
}
1212

1313
fn b<T: ?Sized, U: ?Sized>(x: &T) -> &U {
14-
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
14+
unsafe { transmute(x) } //~ ERROR cannot transmute between types of different sizes
1515
}
1616

1717
fn c<T, U>(x: &T) -> &U {
@@ -23,11 +23,11 @@ fn d<T, U>(x: &[T]) -> &[U] {
2323
}
2424

2525
fn e<T: ?Sized, U>(x: &T) -> &U {
26-
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
26+
unsafe { transmute(x) } //~ ERROR cannot transmute between types of different sizes
2727
}
2828

2929
fn f<T, U: ?Sized>(x: &T) -> &U {
30-
unsafe { transmute(x) } //~ ERROR transmute called with types of different sizes
30+
unsafe { transmute(x) } //~ ERROR cannot transmute between types of different sizes
3131
}
3232

3333
fn main() { }

0 commit comments

Comments
 (0)