summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_transmute
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_transmute')
-rw-r--r--compiler/rustc_transmute/src/layout/tree.rs28
-rw-r--r--compiler/rustc_transmute/src/lib.rs2
-rw-r--r--compiler/rustc_transmute/src/maybe_transmutable/mod.rs27
-rw-r--r--compiler/rustc_transmute/src/maybe_transmutable/query_context.rs4
-rw-r--r--compiler/rustc_transmute/src/maybe_transmutable/tests.rs6
5 files changed, 35 insertions, 32 deletions
diff --git a/compiler/rustc_transmute/src/layout/tree.rs b/compiler/rustc_transmute/src/layout/tree.rs
index 295b65c2c..2a89494c8 100644
--- a/compiler/rustc_transmute/src/layout/tree.rs
+++ b/compiler/rustc_transmute/src/layout/tree.rs
@@ -167,31 +167,31 @@ where
}
}
-#[derive(Debug, Copy, Clone)]
-pub(crate) enum Err {
- /// The layout of the type is unspecified.
- Unspecified,
- /// This error will be surfaced elsewhere by rustc, so don't surface it.
- Unknown,
-}
-
#[cfg(feature = "rustc")]
pub(crate) mod rustc {
- use super::{Err, Tree};
+ use super::Tree;
use crate::layout::rustc::{Def, Ref};
- use rustc_middle::ty;
use rustc_middle::ty::layout::LayoutError;
use rustc_middle::ty::util::Discr;
use rustc_middle::ty::AdtDef;
use rustc_middle::ty::ParamEnv;
use rustc_middle::ty::SubstsRef;
- use rustc_middle::ty::Ty;
- use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::VariantDef;
+ use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
+ use rustc_span::ErrorGuaranteed;
use rustc_target::abi::Align;
use std::alloc;
+ #[derive(Debug, Copy, Clone)]
+ pub(crate) enum Err {
+ /// The layout of the type is unspecified.
+ Unspecified,
+ /// This error will be surfaced elsewhere by rustc, so don't surface it.
+ Unknown,
+ TypeError(ErrorGuaranteed),
+ }
+
impl<'tcx> From<LayoutError<'tcx>> for Err {
fn from(err: LayoutError<'tcx>) -> Self {
match err {
@@ -261,6 +261,10 @@ pub(crate) mod rustc {
use rustc_middle::ty::UintTy::*;
use rustc_target::abi::HasDataLayout;
+ if let Err(e) = ty.error_reported() {
+ return Err(Err::TypeError(e));
+ }
+
let target = tcx.data_layout();
match ty.kind() {
diff --git a/compiler/rustc_transmute/src/lib.rs b/compiler/rustc_transmute/src/lib.rs
index 4b4a8ebd0..a93a42987 100644
--- a/compiler/rustc_transmute/src/lib.rs
+++ b/compiler/rustc_transmute/src/lib.rs
@@ -149,7 +149,7 @@ mod rustc {
.iter()
.enumerate()
.find(|(_, field_def)| name == field_def.name)
- .expect(&format!("There were no fields named `{name}`."));
+ .unwrap_or_else(|| panic!("There were no fields named `{name}`."));
fields[field_idx].unwrap_leaf() == ScalarInt::TRUE
};
diff --git a/compiler/rustc_transmute/src/maybe_transmutable/mod.rs b/compiler/rustc_transmute/src/maybe_transmutable/mod.rs
index 1186eac37..2e2fb90e7 100644
--- a/compiler/rustc_transmute/src/maybe_transmutable/mod.rs
+++ b/compiler/rustc_transmute/src/maybe_transmutable/mod.rs
@@ -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 {
diff --git a/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs b/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs
index e4f3e7928..0cae0377e 100644
--- a/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs
+++ b/compiler/rustc_transmute/src/maybe_transmutable/query_context.rs
@@ -58,9 +58,7 @@ mod rustc {
use rustc_middle::ty;
let parent = if let ty::Adt(adt_def, ..) = scope.kind() {
- use rustc_middle::ty::DefIdTree;
- let parent = self.parent(adt_def.did());
- parent
+ self.parent(adt_def.did())
} else {
// Is this always how we want to handle a non-ADT scope?
return false;
diff --git a/compiler/rustc_transmute/src/maybe_transmutable/tests.rs b/compiler/rustc_transmute/src/maybe_transmutable/tests.rs
index 4d5772a4f..a8675f4ae 100644
--- a/compiler/rustc_transmute/src/maybe_transmutable/tests.rs
+++ b/compiler/rustc_transmute/src/maybe_transmutable/tests.rs
@@ -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
};