safe transmute: gracefully bubble-up layout errors

Changes `.unwrap()`s to `?` to avoid ICEs. Adds ui tests.

Fixes #129327
This commit is contained in:
Jack Wrenn
2024-08-21 17:01:32 +00:00
parent 982c6f8721
commit e2328ebd7f
9 changed files with 121 additions and 49 deletions

View File

@@ -44,18 +44,11 @@ mod rustc {
let Self { src, dst, assume, context } = self;
let layout_cx = LayoutCx { tcx: context, param_env: ParamEnv::reveal_all() };
let layout_of = |ty| {
crate::layout::rustc::layout_of(layout_cx, ty)
.map_err(|_| Err::NotYetSupported)
.and_then(|_| Tree::from_ty(ty, layout_cx))
};
// Convert `src` and `dst` from their rustc representations, to `Tree`-based
// 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 = layout_of(src);
let dst = layout_of(dst);
// representations.
let src = Tree::from_ty(src, layout_cx);
let dst = Tree::from_ty(dst, layout_cx);
match (src, dst) {
(Err(Err::TypeError(_)), _) | (_, Err(Err::TypeError(_))) => {