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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 12 additions & 7 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)