Skip to content

Commit dc35339

Browse files
committed
Safe Transmute: Use 'not yet supported', not 'unspecified' in errors
We can (and will) support analyzing the transmutability of types whose layouts aren't completely specified by its repr. This change ensures that the error messages remain sensible after this support lands.
1 parent c5b5713 commit dc35339

File tree

8 files changed

+43
-43
lines changed

8 files changed

+43
-43
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3073,12 +3073,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
30733073
let src = trait_ref.args.type_at(1);
30743074
let err_msg = format!("`{src}` cannot be safely transmuted into `{dst}`");
30753075
let safe_transmute_explanation = match reason {
3076-
rustc_transmute::Reason::SrcIsUnspecified => {
3077-
format!("`{src}` does not have a well-specified layout")
3076+
rustc_transmute::Reason::SrcIsNotYetSupported => {
3077+
format!("analyzing the transmutability of `{src}` is not yet supported.")
30783078
}
30793079

3080-
rustc_transmute::Reason::DstIsUnspecified => {
3081-
format!("`{dst}` does not have a well-specified layout")
3080+
rustc_transmute::Reason::DstIsNotYetSupported => {
3081+
format!("analyzing the transmutability of `{dst}` is not yet supported.")
30823082
}
30833083

30843084
rustc_transmute::Reason::DstIsBitIncompatible => {

compiler/rustc_transmute/src/layout/tree.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ pub(crate) mod rustc {
186186

187187
#[derive(Debug, Copy, Clone)]
188188
pub(crate) enum Err {
189-
/// The layout of the type is unspecified.
190-
Unspecified,
189+
/// The layout of the type is not yet supported.
190+
NotYetSupported,
191191
/// This error will be surfaced elsewhere by rustc, so don't surface it.
192192
UnknownLayout,
193193
/// Overflow size
@@ -288,14 +288,14 @@ pub(crate) mod rustc {
288288
if members.len() == 0 {
289289
Ok(Tree::unit())
290290
} else {
291-
Err(Err::Unspecified)
291+
Err(Err::NotYetSupported)
292292
}
293293
}
294294

295295
ty::Array(ty, len) => {
296296
let len = len
297297
.try_eval_target_usize(tcx, ParamEnv::reveal_all())
298-
.ok_or(Err::Unspecified)?;
298+
.ok_or(Err::NotYetSupported)?;
299299
let elt = Tree::from_ty(*ty, tcx)?;
300300
Ok(std::iter::repeat(elt)
301301
.take(len as usize)
@@ -307,7 +307,7 @@ pub(crate) mod rustc {
307307

308308
// If the layout is ill-specified, halt.
309309
if !(adt_def.repr().c() || adt_def.repr().int.is_some()) {
310-
return Err(Err::Unspecified);
310+
return Err(Err::NotYetSupported);
311311
}
312312

313313
// Compute a summary of the type's layout.
@@ -348,7 +348,7 @@ pub(crate) mod rustc {
348348
AdtKind::Union => {
349349
// is the layout well-defined?
350350
if !adt_def.repr().c() {
351-
return Err(Err::Unspecified);
351+
return Err(Err::NotYetSupported);
352352
}
353353

354354
let ty_layout = layout_of(tcx, ty)?;
@@ -384,7 +384,7 @@ pub(crate) mod rustc {
384384
}))
385385
}
386386

387-
_ => Err(Err::Unspecified),
387+
_ => Err(Err::NotYetSupported),
388388
}
389389
}
390390

compiler/rustc_transmute/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ pub enum Condition<R> {
4343
/// Answers "why wasn't the source type transmutable into the destination type?"
4444
#[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Clone)]
4545
pub enum Reason<T> {
46-
/// The layout of the source type is unspecified.
47-
SrcIsUnspecified,
48-
/// The layout of the destination type is unspecified.
49-
DstIsUnspecified,
46+
/// The layout of the source type is not yet supported.
47+
SrcIsNotYetSupported,
48+
/// The layout of the destination type is not yet supported.
49+
DstIsNotYetSupported,
5050
/// The layout of the destination type is bit-incompatible with the source type.
5151
DstIsBitIncompatible,
5252
/// The destination type may carry safety invariants.

compiler/rustc_transmute/src/maybe_transmutable/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ mod rustc {
5656
}
5757
(Err(Err::UnknownLayout), _) => Answer::No(Reason::SrcLayoutUnknown),
5858
(_, Err(Err::UnknownLayout)) => Answer::No(Reason::DstLayoutUnknown),
59-
(Err(Err::Unspecified), _) => Answer::No(Reason::SrcIsUnspecified),
60-
(_, Err(Err::Unspecified)) => Answer::No(Reason::DstIsUnspecified),
59+
(Err(Err::NotYetSupported), _) => Answer::No(Reason::SrcIsNotYetSupported),
60+
(_, Err(Err::NotYetSupported)) => Answer::No(Reason::DstIsNotYetSupported),
6161
(Err(Err::SizeOverflow), _) => Answer::No(Reason::SrcSizeOverflow),
6262
(_, Err(Err::SizeOverflow)) => Answer::No(Reason::DstSizeOverflow),
6363
(Ok(src), Ok(dst)) => MaybeTransmutableQuery { src, dst, assume, context }.answer(),

tests/ui/transmutability/arrays/should_require_well_defined_layout.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: `[String; 0]` cannot be safely transmuted into `()`
22
--> $DIR/should_require_well_defined_layout.rs:25:52
33
|
44
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
5-
| ^^ `[String; 0]` does not have a well-specified layout
5+
| ^^ analyzing the transmutability of `[String; 0]` is not yet supported.
66
|
77
note: required by a bound in `is_maybe_transmutable`
88
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -23,7 +23,7 @@ error[E0277]: `u128` cannot be safely transmuted into `[String; 0]`
2323
--> $DIR/should_require_well_defined_layout.rs:26:47
2424
|
2525
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
26-
| ^^^^^^^^^ `[String; 0]` does not have a well-specified layout
26+
| ^^^^^^^^^ analyzing the transmutability of `[String; 0]` is not yet supported.
2727
|
2828
note: required by a bound in `is_maybe_transmutable`
2929
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -44,7 +44,7 @@ error[E0277]: `[String; 1]` cannot be safely transmuted into `()`
4444
--> $DIR/should_require_well_defined_layout.rs:31:52
4545
|
4646
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
47-
| ^^ `[String; 1]` does not have a well-specified layout
47+
| ^^ analyzing the transmutability of `[String; 1]` is not yet supported.
4848
|
4949
note: required by a bound in `is_maybe_transmutable`
5050
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -65,7 +65,7 @@ error[E0277]: `u128` cannot be safely transmuted into `[String; 1]`
6565
--> $DIR/should_require_well_defined_layout.rs:32:47
6666
|
6767
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
68-
| ^^^^^^^^^ `[String; 1]` does not have a well-specified layout
68+
| ^^^^^^^^^ analyzing the transmutability of `[String; 1]` is not yet supported.
6969
|
7070
note: required by a bound in `is_maybe_transmutable`
7171
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -86,7 +86,7 @@ error[E0277]: `[String; 2]` cannot be safely transmuted into `()`
8686
--> $DIR/should_require_well_defined_layout.rs:37:52
8787
|
8888
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
89-
| ^^ `[String; 2]` does not have a well-specified layout
89+
| ^^ analyzing the transmutability of `[String; 2]` is not yet supported.
9090
|
9191
note: required by a bound in `is_maybe_transmutable`
9292
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -107,7 +107,7 @@ error[E0277]: `u128` cannot be safely transmuted into `[String; 2]`
107107
--> $DIR/should_require_well_defined_layout.rs:38:47
108108
|
109109
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
110-
| ^^^^^^^^^ `[String; 2]` does not have a well-specified layout
110+
| ^^^^^^^^^ analyzing the transmutability of `[String; 2]` is not yet supported.
111111
|
112112
note: required by a bound in `is_maybe_transmutable`
113113
--> $DIR/should_require_well_defined_layout.rs:12:14

tests/ui/transmutability/enums/repr/should_require_well_defined_layout.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: `void::repr_rust` cannot be safely transmuted into `()`
22
--> $DIR/should_require_well_defined_layout.rs:27:52
33
|
44
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
5-
| ^^ `void::repr_rust` does not have a well-specified layout
5+
| ^^ analyzing the transmutability of `void::repr_rust` is not yet supported.
66
|
77
note: required by a bound in `is_maybe_transmutable`
88
--> $DIR/should_require_well_defined_layout.rs:13:14
@@ -24,7 +24,7 @@ error[E0277]: `u128` cannot be safely transmuted into `void::repr_rust`
2424
--> $DIR/should_require_well_defined_layout.rs:28:47
2525
|
2626
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
27-
| ^^^^^^^^^ `void::repr_rust` does not have a well-specified layout
27+
| ^^^^^^^^^ analyzing the transmutability of `void::repr_rust` is not yet supported.
2828
|
2929
note: required by a bound in `is_maybe_transmutable`
3030
--> $DIR/should_require_well_defined_layout.rs:13:14
@@ -46,7 +46,7 @@ error[E0277]: `singleton::repr_rust` cannot be safely transmuted into `()`
4646
--> $DIR/should_require_well_defined_layout.rs:33:52
4747
|
4848
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
49-
| ^^ `singleton::repr_rust` does not have a well-specified layout
49+
| ^^ analyzing the transmutability of `singleton::repr_rust` is not yet supported.
5050
|
5151
note: required by a bound in `is_maybe_transmutable`
5252
--> $DIR/should_require_well_defined_layout.rs:13:14
@@ -68,7 +68,7 @@ error[E0277]: `u128` cannot be safely transmuted into `singleton::repr_rust`
6868
--> $DIR/should_require_well_defined_layout.rs:34:47
6969
|
7070
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
71-
| ^^^^^^^^^ `singleton::repr_rust` does not have a well-specified layout
71+
| ^^^^^^^^^ analyzing the transmutability of `singleton::repr_rust` is not yet supported.
7272
|
7373
note: required by a bound in `is_maybe_transmutable`
7474
--> $DIR/should_require_well_defined_layout.rs:13:14
@@ -90,7 +90,7 @@ error[E0277]: `duplex::repr_rust` cannot be safely transmuted into `()`
9090
--> $DIR/should_require_well_defined_layout.rs:39:52
9191
|
9292
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
93-
| ^^ `duplex::repr_rust` does not have a well-specified layout
93+
| ^^ analyzing the transmutability of `duplex::repr_rust` is not yet supported.
9494
|
9595
note: required by a bound in `is_maybe_transmutable`
9696
--> $DIR/should_require_well_defined_layout.rs:13:14
@@ -112,7 +112,7 @@ error[E0277]: `u128` cannot be safely transmuted into `duplex::repr_rust`
112112
--> $DIR/should_require_well_defined_layout.rs:40:47
113113
|
114114
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
115-
| ^^^^^^^^^ `duplex::repr_rust` does not have a well-specified layout
115+
| ^^^^^^^^^ analyzing the transmutability of `duplex::repr_rust` is not yet supported.
116116
|
117117
note: required by a bound in `is_maybe_transmutable`
118118
--> $DIR/should_require_well_defined_layout.rs:13:14

tests/ui/transmutability/structs/repr/should_require_well_defined_layout.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: `should_reject_repr_rust::unit::repr_rust` cannot be safely transm
22
--> $DIR/should_require_well_defined_layout.rs:27:52
33
|
44
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
5-
| ^^ `should_reject_repr_rust::unit::repr_rust` does not have a well-specified layout
5+
| ^^ analyzing the transmutability of `should_reject_repr_rust::unit::repr_rust` is not yet supported.
66
|
77
note: required by a bound in `is_maybe_transmutable`
88
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -24,7 +24,7 @@ error[E0277]: `u128` cannot be safely transmuted into `should_reject_repr_rust::
2424
--> $DIR/should_require_well_defined_layout.rs:28:47
2525
|
2626
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
27-
| ^^^^^^^^^ `should_reject_repr_rust::unit::repr_rust` does not have a well-specified layout
27+
| ^^^^^^^^^ analyzing the transmutability of `should_reject_repr_rust::unit::repr_rust` is not yet supported.
2828
|
2929
note: required by a bound in `is_maybe_transmutable`
3030
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -46,7 +46,7 @@ error[E0277]: `should_reject_repr_rust::tuple::repr_rust` cannot be safely trans
4646
--> $DIR/should_require_well_defined_layout.rs:33:52
4747
|
4848
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
49-
| ^^ `should_reject_repr_rust::tuple::repr_rust` does not have a well-specified layout
49+
| ^^ analyzing the transmutability of `should_reject_repr_rust::tuple::repr_rust` is not yet supported.
5050
|
5151
note: required by a bound in `is_maybe_transmutable`
5252
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -68,7 +68,7 @@ error[E0277]: `u128` cannot be safely transmuted into `should_reject_repr_rust::
6868
--> $DIR/should_require_well_defined_layout.rs:34:47
6969
|
7070
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
71-
| ^^^^^^^^^ `should_reject_repr_rust::tuple::repr_rust` does not have a well-specified layout
71+
| ^^^^^^^^^ analyzing the transmutability of `should_reject_repr_rust::tuple::repr_rust` is not yet supported.
7272
|
7373
note: required by a bound in `is_maybe_transmutable`
7474
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -90,7 +90,7 @@ error[E0277]: `should_reject_repr_rust::braces::repr_rust` cannot be safely tran
9090
--> $DIR/should_require_well_defined_layout.rs:39:52
9191
|
9292
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
93-
| ^^ `should_reject_repr_rust::braces::repr_rust` does not have a well-specified layout
93+
| ^^ analyzing the transmutability of `should_reject_repr_rust::braces::repr_rust` is not yet supported.
9494
|
9595
note: required by a bound in `is_maybe_transmutable`
9696
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -112,7 +112,7 @@ error[E0277]: `u128` cannot be safely transmuted into `should_reject_repr_rust::
112112
--> $DIR/should_require_well_defined_layout.rs:40:47
113113
|
114114
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
115-
| ^^^^^^^^^ `should_reject_repr_rust::braces::repr_rust` does not have a well-specified layout
115+
| ^^^^^^^^^ analyzing the transmutability of `should_reject_repr_rust::braces::repr_rust` is not yet supported.
116116
|
117117
note: required by a bound in `is_maybe_transmutable`
118118
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -134,7 +134,7 @@ error[E0277]: `aligned::repr_rust` cannot be safely transmuted into `()`
134134
--> $DIR/should_require_well_defined_layout.rs:45:52
135135
|
136136
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
137-
| ^^ `aligned::repr_rust` does not have a well-specified layout
137+
| ^^ analyzing the transmutability of `aligned::repr_rust` is not yet supported.
138138
|
139139
note: required by a bound in `is_maybe_transmutable`
140140
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -156,7 +156,7 @@ error[E0277]: `u128` cannot be safely transmuted into `aligned::repr_rust`
156156
--> $DIR/should_require_well_defined_layout.rs:46:47
157157
|
158158
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
159-
| ^^^^^^^^^ `aligned::repr_rust` does not have a well-specified layout
159+
| ^^^^^^^^^ analyzing the transmutability of `aligned::repr_rust` is not yet supported.
160160
|
161161
note: required by a bound in `is_maybe_transmutable`
162162
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -178,7 +178,7 @@ error[E0277]: `packed::repr_rust` cannot be safely transmuted into `()`
178178
--> $DIR/should_require_well_defined_layout.rs:51:52
179179
|
180180
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
181-
| ^^ `packed::repr_rust` does not have a well-specified layout
181+
| ^^ analyzing the transmutability of `packed::repr_rust` is not yet supported.
182182
|
183183
note: required by a bound in `is_maybe_transmutable`
184184
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -200,7 +200,7 @@ error[E0277]: `u128` cannot be safely transmuted into `packed::repr_rust`
200200
--> $DIR/should_require_well_defined_layout.rs:52:47
201201
|
202202
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
203-
| ^^^^^^^^^ `packed::repr_rust` does not have a well-specified layout
203+
| ^^^^^^^^^ analyzing the transmutability of `packed::repr_rust` is not yet supported.
204204
|
205205
note: required by a bound in `is_maybe_transmutable`
206206
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -222,7 +222,7 @@ error[E0277]: `nested::repr_c` cannot be safely transmuted into `()`
222222
--> $DIR/should_require_well_defined_layout.rs:58:49
223223
|
224224
LL | assert::is_maybe_transmutable::<repr_c, ()>();
225-
| ^^ `nested::repr_c` does not have a well-specified layout
225+
| ^^ analyzing the transmutability of `nested::repr_c` is not yet supported.
226226
|
227227
note: required by a bound in `is_maybe_transmutable`
228228
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -244,7 +244,7 @@ error[E0277]: `u128` cannot be safely transmuted into `nested::repr_c`
244244
--> $DIR/should_require_well_defined_layout.rs:59:47
245245
|
246246
LL | assert::is_maybe_transmutable::<u128, repr_c>();
247-
| ^^^^^^ `nested::repr_c` does not have a well-specified layout
247+
| ^^^^^^ analyzing the transmutability of `nested::repr_c` is not yet supported.
248248
|
249249
note: required by a bound in `is_maybe_transmutable`
250250
--> $DIR/should_require_well_defined_layout.rs:12:14

tests/ui/transmutability/unions/repr/should_require_well_defined_layout.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: `should_reject_repr_rust::repr_rust` cannot be safely transmuted i
22
--> $DIR/should_require_well_defined_layout.rs:29:48
33
|
44
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
5-
| ^^ `should_reject_repr_rust::repr_rust` does not have a well-specified layout
5+
| ^^ analyzing the transmutability of `should_reject_repr_rust::repr_rust` is not yet supported.
66
|
77
note: required by a bound in `is_maybe_transmutable`
88
--> $DIR/should_require_well_defined_layout.rs:12:14
@@ -24,7 +24,7 @@ error[E0277]: `u128` cannot be safely transmuted into `should_reject_repr_rust::
2424
--> $DIR/should_require_well_defined_layout.rs:30:43
2525
|
2626
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
27-
| ^^^^^^^^^ `should_reject_repr_rust::repr_rust` does not have a well-specified layout
27+
| ^^^^^^^^^ analyzing the transmutability of `should_reject_repr_rust::repr_rust` is not yet supported.
2828
|
2929
note: required by a bound in `is_maybe_transmutable`
3030
--> $DIR/should_require_well_defined_layout.rs:12:14

0 commit comments

Comments
 (0)