1
Fork 0

Improve safe transmute error reporting

This patch updates the error reporting when Safe Transmute is not
possible between 2 types by including the reason.

Also, fix some small bugs that occur when computing the `Answer` for
transmutability.
This commit is contained in:
Bryan Garza 2023-04-06 01:58:53 +00:00
parent 59a05ad118
commit 36febe1f4d
29 changed files with 495 additions and 593 deletions

View file

@ -56,7 +56,7 @@ where
#[cfg(feature = "rustc")]
mod rustc {
use super::*;
use crate::layout::tree::Err;
use crate::layout::tree::rustc::Err;
use rustc_middle::ty::Ty;
use rustc_middle::ty::TyCtxt;
@ -71,19 +71,20 @@ mod rustc {
// representations. If these conversions fail, conclude that the transmutation is
// unacceptable; the layouts of both the source and destination types must be
// well-defined.
let src = Tree::from_ty(src, context).map_err(|err| match err {
// Answer `Yes` here, because "Unknown Type" will already be reported by
// rustc. No need to spam the user with more errors.
Err::Unknown => Answer::Yes,
Err::Unspecified => Answer::No(Reason::SrcIsUnspecified),
})?;
let src = Tree::from_ty(src, context);
let dst = Tree::from_ty(dst, context);
let dst = Tree::from_ty(dst, context).map_err(|err| match err {
Err::Unknown => Answer::Yes,
Err::Unspecified => Answer::No(Reason::DstIsUnspecified),
})?;
Ok((src, dst))
match (src, dst) {
// Answer `Yes` here, because 'unknown layout' and type errors will already
// be reported by rustc. No need to spam the user with more errors.
(Err(Err::TypeError(_)), _) => Err(Answer::Yes),
(_, Err(Err::TypeError(_))) => Err(Answer::Yes),
(Err(Err::Unknown), _) => Err(Answer::Yes),
(_, Err(Err::Unknown)) => Err(Answer::Yes),
(Err(Err::Unspecified), _) => Err(Answer::No(Reason::SrcIsUnspecified)),
(_, Err(Err::Unspecified)) => Err(Answer::No(Reason::DstIsUnspecified)),
(Ok(src), Ok(dst)) => Ok((src, dst)),
}
});
match query_or_answer {

View file

@ -1,6 +1,6 @@
use super::query_context::test::{Def, UltraMinimal};
use crate::maybe_transmutable::MaybeTransmutableQuery;
use crate::{layout, Answer, Reason, Set};
use crate::{layout, Answer, Reason};
use itertools::Itertools;
mod bool {
@ -48,9 +48,9 @@ mod bool {
let into_set = |alts: Vec<_>| {
#[cfg(feature = "rustc")]
let mut set = Set::default();
let mut set = crate::Set::default();
#[cfg(not(feature = "rustc"))]
let mut set = Set::new();
let mut set = std::collections::HashSet::new();
set.extend(alts);
set
};