Skip to content

Safe Transmute: Use 'not yet supported', not 'unspecified' in errors #122560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3073,29 +3073,29 @@ 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 => {
format!("At least one value of `{src}` isn't a bit-valid value of `{dst}`")
format!("at least one value of `{src}` isn't a bit-valid value of `{dst}`")
}

rustc_transmute::Reason::DstMayHaveSafetyInvariants => {
format!("`{dst}` may carry safety invariants")
}
rustc_transmute::Reason::DstIsTooBig => {
format!("The size of `{src}` is smaller than the size of `{dst}`")
format!("the size of `{src}` is smaller than the size of `{dst}`")
}
rustc_transmute::Reason::DstRefIsTooBig { src, dst } => {
let src_size = src.size;
let dst_size = dst.size;
format!(
"The referent size of `{src}` ({src_size} bytes) is smaller than that of `{dst}` ({dst_size} bytes)"
"the referent size of `{src}` ({src_size} bytes) is smaller than that of `{dst}` ({dst_size} bytes)"
)
}
rustc_transmute::Reason::SrcSizeOverflow => {
Expand All @@ -3113,7 +3113,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
dst_min_align,
} => {
format!(
"The minimum alignment of `{src}` ({src_min_align}) should be greater than that of `{dst}` ({dst_min_align})"
"the minimum alignment of `{src}` ({src_min_align}) should be greater than that of `{dst}` ({dst_min_align})"
)
}
rustc_transmute::Reason::DstIsMoreUnique => {
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_transmute/src/layout/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -384,7 +384,7 @@ pub(crate) mod rustc {
}))
}

_ => Err(Err::Unspecified),
_ => Err(Err::NotYetSupported),
}
}

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_transmute/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_transmute/src/maybe_transmutable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/transmutability/alignment/align-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0277]: `&[u8; 0]` cannot be safely transmuted into `&[u16; 0]`
--> $DIR/align-fail.rs:21:55
|
LL | ...tatic [u8; 0], &'static [u16; 0]>();
| ^^^^^^^^^^^^^^^^^ The minimum alignment of `&[u8; 0]` (1) should be greater than that of `&[u16; 0]` (2)
| ^^^^^^^^^^^^^^^^^ the minimum alignment of `&[u8; 0]` (1) should be greater than that of `&[u16; 0]` (2)
|
note: required by a bound in `is_maybe_transmutable`
--> $DIR/align-fail.rs:9:14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading
Loading