Skip to content

Commit 21d1ab4

Browse files
committed
safe transmute: add rustc_on_unimplemented to BikeshedIntrinsicFrom
ref: #92268 (comment)
1 parent 402644f commit 21d1ab4

40 files changed

+486
-364
lines changed

library/core/src/mem/transmutability.rs

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
/// notwithstanding whatever safety checks you have asked the compiler to [`Assume`] are satisfied.
66
#[unstable(feature = "transmutability", issue = "none")]
77
#[cfg_attr(not(bootstrap), lang = "transmute_trait")]
8+
#[rustc_on_unimplemented(
9+
message = "`{Src}` cannot be safely transmuted into `{Self}` in the defining scope of `{Context}`.",
10+
label = "`{Src}` cannot be safely transmuted into `{Self}` in the defining scope of `{Context}`."
11+
)]
812
pub unsafe trait BikeshedIntrinsicFrom<
913
Src,
1014
Context,

src/test/ui/transmutability/arrays/should_require_well_defined_layout.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ fn should_reject_repr_rust()
1818
{
1919
fn unit() {
2020
type repr_rust = [String; 0];
21-
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR not satisfied
22-
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR not satisfied
21+
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
22+
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
2323
}
2424

2525
fn singleton() {
2626
type repr_rust = [String; 1];
27-
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR not satisfied
28-
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR not satisfied
27+
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
28+
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
2929
}
3030

3131
fn duplex() {
3232
type repr_rust = [String; 2];
33-
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR not satisfied
34-
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR not satisfied
33+
assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
34+
assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
3535
}
3636
}
3737

src/test/ui/transmutability/arrays/should_require_well_defined_layout.stderr

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
error[E0277]: the trait bound `(): BikeshedIntrinsicFrom<[String; 0], assert::Context, true, true, true, true>` is not satisfied
1+
error[E0277]: `[String; 0]` cannot be safely transmuted into `()` in the defining scope of `assert::Context`.
22
--> $DIR/should_require_well_defined_layout.rs:21:52
33
|
44
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
5-
| ^^ the trait `BikeshedIntrinsicFrom<[String; 0], assert::Context, true, true, true, true>` is not implemented for `()`
5+
| ^^ `[String; 0]` cannot be safely transmuted into `()` in the defining scope of `assert::Context`.
66
|
7+
= help: the trait `BikeshedIntrinsicFrom<[String; 0], assert::Context, true, true, true, true>` is not implemented for `()`
78
note: required by a bound in `is_maybe_transmutable`
89
--> $DIR/should_require_well_defined_layout.rs:13:14
910
|
@@ -13,12 +14,13 @@ LL | where
1314
LL | Dst: BikeshedIntrinsicFrom<Src, Context, true, true, true, true>
1415
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
1516

16-
error[E0277]: the trait bound `[String; 0]: BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not satisfied
17+
error[E0277]: `u128` cannot be safely transmuted into `[String; 0]` in the defining scope of `assert::Context`.
1718
--> $DIR/should_require_well_defined_layout.rs:22:47
1819
|
1920
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
20-
| ^^^^^^^^^ the trait `BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not implemented for `[String; 0]`
21+
| ^^^^^^^^^ `u128` cannot be safely transmuted into `[String; 0]` in the defining scope of `assert::Context`.
2122
|
23+
= help: the trait `BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not implemented for `[String; 0]`
2224
note: required by a bound in `is_maybe_transmutable`
2325
--> $DIR/should_require_well_defined_layout.rs:13:14
2426
|
@@ -28,12 +30,13 @@ LL | where
2830
LL | Dst: BikeshedIntrinsicFrom<Src, Context, true, true, true, true>
2931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
3032

31-
error[E0277]: the trait bound `(): BikeshedIntrinsicFrom<[String; 1], assert::Context, true, true, true, true>` is not satisfied
33+
error[E0277]: `[String; 1]` cannot be safely transmuted into `()` in the defining scope of `assert::Context`.
3234
--> $DIR/should_require_well_defined_layout.rs:27:52
3335
|
3436
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
35-
| ^^ the trait `BikeshedIntrinsicFrom<[String; 1], assert::Context, true, true, true, true>` is not implemented for `()`
37+
| ^^ `[String; 1]` cannot be safely transmuted into `()` in the defining scope of `assert::Context`.
3638
|
39+
= help: the trait `BikeshedIntrinsicFrom<[String; 1], assert::Context, true, true, true, true>` is not implemented for `()`
3740
note: required by a bound in `is_maybe_transmutable`
3841
--> $DIR/should_require_well_defined_layout.rs:13:14
3942
|
@@ -43,12 +46,13 @@ LL | where
4346
LL | Dst: BikeshedIntrinsicFrom<Src, Context, true, true, true, true>
4447
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
4548

46-
error[E0277]: the trait bound `[String; 1]: BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not satisfied
49+
error[E0277]: `u128` cannot be safely transmuted into `[String; 1]` in the defining scope of `assert::Context`.
4750
--> $DIR/should_require_well_defined_layout.rs:28:47
4851
|
4952
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
50-
| ^^^^^^^^^ the trait `BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not implemented for `[String; 1]`
53+
| ^^^^^^^^^ `u128` cannot be safely transmuted into `[String; 1]` in the defining scope of `assert::Context`.
5154
|
55+
= help: the trait `BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not implemented for `[String; 1]`
5256
note: required by a bound in `is_maybe_transmutable`
5357
--> $DIR/should_require_well_defined_layout.rs:13:14
5458
|
@@ -58,12 +62,13 @@ LL | where
5862
LL | Dst: BikeshedIntrinsicFrom<Src, Context, true, true, true, true>
5963
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
6064

61-
error[E0277]: the trait bound `(): BikeshedIntrinsicFrom<[String; 2], assert::Context, true, true, true, true>` is not satisfied
65+
error[E0277]: `[String; 2]` cannot be safely transmuted into `()` in the defining scope of `assert::Context`.
6266
--> $DIR/should_require_well_defined_layout.rs:33:52
6367
|
6468
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
65-
| ^^ the trait `BikeshedIntrinsicFrom<[String; 2], assert::Context, true, true, true, true>` is not implemented for `()`
69+
| ^^ `[String; 2]` cannot be safely transmuted into `()` in the defining scope of `assert::Context`.
6670
|
71+
= help: the trait `BikeshedIntrinsicFrom<[String; 2], assert::Context, true, true, true, true>` is not implemented for `()`
6772
note: required by a bound in `is_maybe_transmutable`
6873
--> $DIR/should_require_well_defined_layout.rs:13:14
6974
|
@@ -73,12 +78,13 @@ LL | where
7378
LL | Dst: BikeshedIntrinsicFrom<Src, Context, true, true, true, true>
7479
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
7580

76-
error[E0277]: the trait bound `[String; 2]: BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not satisfied
81+
error[E0277]: `u128` cannot be safely transmuted into `[String; 2]` in the defining scope of `assert::Context`.
7782
--> $DIR/should_require_well_defined_layout.rs:34:47
7883
|
7984
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
80-
| ^^^^^^^^^ the trait `BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not implemented for `[String; 2]`
85+
| ^^^^^^^^^ `u128` cannot be safely transmuted into `[String; 2]` in the defining scope of `assert::Context`.
8186
|
87+
= help: the trait `BikeshedIntrinsicFrom<u128, assert::Context, true, true, true, true>` is not implemented for `[String; 2]`
8288
note: required by a bound in `is_maybe_transmutable`
8389
--> $DIR/should_require_well_defined_layout.rs:13:14
8490
|

src/test/ui/transmutability/enums/repr/primitive_reprs_should_have_correct_length.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ fn n8() {
3838
fn i_should_have_correct_length() {
3939
type Current = V0i8;
4040

41-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
41+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
4242
assert::is_transmutable::<Current, Analog, Context>();
43-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
43+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
4444
}
4545

4646
fn u_should_have_correct_length() {
4747
type Current = V0u8;
4848

49-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
49+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
5050
assert::is_transmutable::<Current, Analog, Context>();
51-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
51+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
5252
}
5353
}
5454

@@ -62,17 +62,17 @@ fn n16() {
6262
fn i_should_have_correct_length() {
6363
type Current = V0i16;
6464

65-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
65+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
6666
assert::is_transmutable::<Current, Analog, Context>();
67-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
67+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
6868
}
6969

7070
fn u_should_have_correct_length() {
7171
type Current = V0u16;
7272

73-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
73+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
7474
assert::is_transmutable::<Current, Analog, Context>();
75-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
75+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
7676
}
7777
}
7878

@@ -86,17 +86,17 @@ fn n32() {
8686
fn i_should_have_correct_length() {
8787
type Current = V0i32;
8888

89-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
89+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
9090
assert::is_transmutable::<Current, Analog, Context>();
91-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
91+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
9292
}
9393

9494
fn u_should_have_correct_length() {
9595
type Current = V0u32;
9696

97-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
97+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
9898
assert::is_transmutable::<Current, Analog, Context>();
99-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
99+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
100100
}
101101
}
102102

@@ -110,17 +110,17 @@ fn n64() {
110110
fn i_should_have_correct_length() {
111111
type Current = V0i64;
112112

113-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
113+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
114114
assert::is_transmutable::<Current, Analog, Context>();
115-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
115+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
116116
}
117117

118118
fn u_should_have_correct_length() {
119119
type Current = V0u64;
120120

121-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
121+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
122122
assert::is_transmutable::<Current, Analog, Context>();
123-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
123+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
124124
}
125125
}
126126

@@ -134,16 +134,16 @@ fn nsize() {
134134
fn i_should_have_correct_length() {
135135
type Current = V0isize;
136136

137-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
137+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
138138
assert::is_transmutable::<Current, Analog, Context>();
139-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
139+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
140140
}
141141

142142
fn u_should_have_correct_length() {
143143
type Current = V0usize;
144144

145-
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR not satisfied
145+
assert::is_transmutable::<Smaller, Current, Context>(); //~ ERROR cannot be safely transmuted
146146
assert::is_transmutable::<Current, Analog, Context>();
147-
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR not satisfied
147+
assert::is_transmutable::<Current, Larger, Context>(); //~ ERROR cannot be safely transmuted
148148
}
149149
}

0 commit comments

Comments
 (0)