mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-22 20:53:37 +00:00
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.
This commit is contained in:
parent
c5b571310d
commit
dc35339514
@ -3073,12 +3073,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||
let src = trait_ref.args.type_at(1);
|
||||
let err_msg = format!("`{src}` cannot be safely transmuted into `{dst}`");
|
||||
let safe_transmute_explanation = match reason {
|
||||
rustc_transmute::Reason::SrcIsUnspecified => {
|
||||
format!("`{src}` does not have a well-specified layout")
|
||||
rustc_transmute::Reason::SrcIsNotYetSupported => {
|
||||
format!("analyzing the transmutability of `{src}` is not yet supported.")
|
||||
}
|
||||
|
||||
rustc_transmute::Reason::DstIsUnspecified => {
|
||||
format!("`{dst}` does not have a well-specified layout")
|
||||
rustc_transmute::Reason::DstIsNotYetSupported => {
|
||||
format!("analyzing the transmutability of `{dst}` is not yet supported.")
|
||||
}
|
||||
|
||||
rustc_transmute::Reason::DstIsBitIncompatible => {
|
||||
|
@ -186,8 +186,8 @@ pub(crate) mod rustc {
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub(crate) enum Err {
|
||||
/// The layout of the type is unspecified.
|
||||
Unspecified,
|
||||
/// The layout of the type is not yet supported.
|
||||
NotYetSupported,
|
||||
/// This error will be surfaced elsewhere by rustc, so don't surface it.
|
||||
UnknownLayout,
|
||||
/// Overflow size
|
||||
@ -288,14 +288,14 @@ pub(crate) mod rustc {
|
||||
if members.len() == 0 {
|
||||
Ok(Tree::unit())
|
||||
} else {
|
||||
Err(Err::Unspecified)
|
||||
Err(Err::NotYetSupported)
|
||||
}
|
||||
}
|
||||
|
||||
ty::Array(ty, len) => {
|
||||
let len = len
|
||||
.try_eval_target_usize(tcx, ParamEnv::reveal_all())
|
||||
.ok_or(Err::Unspecified)?;
|
||||
.ok_or(Err::NotYetSupported)?;
|
||||
let elt = Tree::from_ty(*ty, tcx)?;
|
||||
Ok(std::iter::repeat(elt)
|
||||
.take(len as usize)
|
||||
@ -307,7 +307,7 @@ pub(crate) mod rustc {
|
||||
|
||||
// If the layout is ill-specified, halt.
|
||||
if !(adt_def.repr().c() || adt_def.repr().int.is_some()) {
|
||||
return Err(Err::Unspecified);
|
||||
return Err(Err::NotYetSupported);
|
||||
}
|
||||
|
||||
// Compute a summary of the type's layout.
|
||||
@ -348,7 +348,7 @@ pub(crate) mod rustc {
|
||||
AdtKind::Union => {
|
||||
// is the layout well-defined?
|
||||
if !adt_def.repr().c() {
|
||||
return Err(Err::Unspecified);
|
||||
return Err(Err::NotYetSupported);
|
||||
}
|
||||
|
||||
let ty_layout = layout_of(tcx, ty)?;
|
||||
@ -384,7 +384,7 @@ pub(crate) mod rustc {
|
||||
}))
|
||||
}
|
||||
|
||||
_ => Err(Err::Unspecified),
|
||||
_ => Err(Err::NotYetSupported),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,10 +43,10 @@ pub enum Condition<R> {
|
||||
/// Answers "why wasn't the source type transmutable into the destination type?"
|
||||
#[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Clone)]
|
||||
pub enum Reason<T> {
|
||||
/// The layout of the source type is unspecified.
|
||||
SrcIsUnspecified,
|
||||
/// The layout of the destination type is unspecified.
|
||||
DstIsUnspecified,
|
||||
/// The layout of the source type is not yet supported.
|
||||
SrcIsNotYetSupported,
|
||||
/// The layout of the destination type is not yet supported.
|
||||
DstIsNotYetSupported,
|
||||
/// The layout of the destination type is bit-incompatible with the source type.
|
||||
DstIsBitIncompatible,
|
||||
/// The destination type may carry safety invariants.
|
||||
|
@ -56,8 +56,8 @@ mod rustc {
|
||||
}
|
||||
(Err(Err::UnknownLayout), _) => Answer::No(Reason::SrcLayoutUnknown),
|
||||
(_, Err(Err::UnknownLayout)) => Answer::No(Reason::DstLayoutUnknown),
|
||||
(Err(Err::Unspecified), _) => Answer::No(Reason::SrcIsUnspecified),
|
||||
(_, Err(Err::Unspecified)) => Answer::No(Reason::DstIsUnspecified),
|
||||
(Err(Err::NotYetSupported), _) => Answer::No(Reason::SrcIsNotYetSupported),
|
||||
(_, Err(Err::NotYetSupported)) => Answer::No(Reason::DstIsNotYetSupported),
|
||||
(Err(Err::SizeOverflow), _) => Answer::No(Reason::SrcSizeOverflow),
|
||||
(_, Err(Err::SizeOverflow)) => Answer::No(Reason::DstSizeOverflow),
|
||||
(Ok(src), Ok(dst)) => MaybeTransmutableQuery { src, dst, assume, context }.answer(),
|
||||
|
@ -2,7 +2,7 @@ error[E0277]: `[String; 0]` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:25:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `[String; 0]` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `[String; 0]` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -23,7 +23,7 @@ error[E0277]: `u128` cannot be safely transmuted into `[String; 0]`
|
||||
--> $DIR/should_require_well_defined_layout.rs:26:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `[String; 0]` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `[String; 0]` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -44,7 +44,7 @@ error[E0277]: `[String; 1]` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:31:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `[String; 1]` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `[String; 1]` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -65,7 +65,7 @@ error[E0277]: `u128` cannot be safely transmuted into `[String; 1]`
|
||||
--> $DIR/should_require_well_defined_layout.rs:32:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `[String; 1]` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `[String; 1]` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -86,7 +86,7 @@ error[E0277]: `[String; 2]` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:37:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `[String; 2]` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `[String; 2]` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -107,7 +107,7 @@ error[E0277]: `u128` cannot be safely transmuted into `[String; 2]`
|
||||
--> $DIR/should_require_well_defined_layout.rs:38:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `[String; 2]` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `[String; 2]` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
|
@ -2,7 +2,7 @@ error[E0277]: `void::repr_rust` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:27:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `void::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `void::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:13:14
|
||||
@ -24,7 +24,7 @@ error[E0277]: `u128` cannot be safely transmuted into `void::repr_rust`
|
||||
--> $DIR/should_require_well_defined_layout.rs:28:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `void::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `void::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:13:14
|
||||
@ -46,7 +46,7 @@ error[E0277]: `singleton::repr_rust` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:33:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `singleton::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `singleton::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:13:14
|
||||
@ -68,7 +68,7 @@ error[E0277]: `u128` cannot be safely transmuted into `singleton::repr_rust`
|
||||
--> $DIR/should_require_well_defined_layout.rs:34:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `singleton::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `singleton::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:13:14
|
||||
@ -90,7 +90,7 @@ error[E0277]: `duplex::repr_rust` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:39:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `duplex::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `duplex::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:13:14
|
||||
@ -112,7 +112,7 @@ error[E0277]: `u128` cannot be safely transmuted into `duplex::repr_rust`
|
||||
--> $DIR/should_require_well_defined_layout.rs:40:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `duplex::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `duplex::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:13:14
|
||||
|
@ -2,7 +2,7 @@ error[E0277]: `should_reject_repr_rust::unit::repr_rust` cannot be safely transm
|
||||
--> $DIR/should_require_well_defined_layout.rs:27:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `should_reject_repr_rust::unit::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `should_reject_repr_rust::unit::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $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::
|
||||
--> $DIR/should_require_well_defined_layout.rs:28:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `should_reject_repr_rust::unit::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `should_reject_repr_rust::unit::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $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
|
||||
--> $DIR/should_require_well_defined_layout.rs:33:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `should_reject_repr_rust::tuple::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `should_reject_repr_rust::tuple::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $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::
|
||||
--> $DIR/should_require_well_defined_layout.rs:34:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `should_reject_repr_rust::tuple::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `should_reject_repr_rust::tuple::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $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
|
||||
--> $DIR/should_require_well_defined_layout.rs:39:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `should_reject_repr_rust::braces::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `should_reject_repr_rust::braces::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $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::
|
||||
--> $DIR/should_require_well_defined_layout.rs:40:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `should_reject_repr_rust::braces::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `should_reject_repr_rust::braces::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -134,7 +134,7 @@ error[E0277]: `aligned::repr_rust` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:45:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `aligned::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `aligned::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -156,7 +156,7 @@ error[E0277]: `u128` cannot be safely transmuted into `aligned::repr_rust`
|
||||
--> $DIR/should_require_well_defined_layout.rs:46:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `aligned::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `aligned::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -178,7 +178,7 @@ error[E0277]: `packed::repr_rust` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:51:52
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `packed::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `packed::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -200,7 +200,7 @@ error[E0277]: `u128` cannot be safely transmuted into `packed::repr_rust`
|
||||
--> $DIR/should_require_well_defined_layout.rs:52:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `packed::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `packed::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -222,7 +222,7 @@ error[E0277]: `nested::repr_c` cannot be safely transmuted into `()`
|
||||
--> $DIR/should_require_well_defined_layout.rs:58:49
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_c, ()>();
|
||||
| ^^ `nested::repr_c` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `nested::repr_c` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
@ -244,7 +244,7 @@ error[E0277]: `u128` cannot be safely transmuted into `nested::repr_c`
|
||||
--> $DIR/should_require_well_defined_layout.rs:59:47
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_c>();
|
||||
| ^^^^^^ `nested::repr_c` does not have a well-specified layout
|
||||
| ^^^^^^ analyzing the transmutability of `nested::repr_c` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
|
@ -2,7 +2,7 @@ error[E0277]: `should_reject_repr_rust::repr_rust` cannot be safely transmuted i
|
||||
--> $DIR/should_require_well_defined_layout.rs:29:48
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<repr_rust, ()>();
|
||||
| ^^ `should_reject_repr_rust::repr_rust` does not have a well-specified layout
|
||||
| ^^ analyzing the transmutability of `should_reject_repr_rust::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $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::
|
||||
--> $DIR/should_require_well_defined_layout.rs:30:43
|
||||
|
|
||||
LL | assert::is_maybe_transmutable::<u128, repr_rust>();
|
||||
| ^^^^^^^^^ `should_reject_repr_rust::repr_rust` does not have a well-specified layout
|
||||
| ^^^^^^^^^ analyzing the transmutability of `should_reject_repr_rust::repr_rust` is not yet supported.
|
||||
|
|
||||
note: required by a bound in `is_maybe_transmutable`
|
||||
--> $DIR/should_require_well_defined_layout.rs:12:14
|
||||
|
Loading…
Reference in New Issue
Block a user