diff options
| author | Bryan Garza <1396101+bryangarza@users.noreply.github.com> | 2023-06-12 16:35:23 -0700 |
|---|---|---|
| committer | Bryan Garza <1396101+bryangarza@users.noreply.github.com> | 2023-06-12 16:56:21 -0700 |
| commit | f4cf8f65a5e8e110c8c36469d31f16e8571e2c1a (patch) | |
| tree | 81d55477f266bac64272420ca537903b35a17fb8 /compiler/rustc_transmute/src/lib.rs | |
| parent | 64a54df86faf1f55148433753296dc2bc2a7e31d (diff) | |
| download | rust-f4cf8f65a5e8e110c8c36469d31f16e8571e2c1a.tar.gz rust-f4cf8f65a5e8e110c8c36469d31f16e8571e2c1a.zip | |
Safe Transmute: Refactor error handling and Answer type
- Create `Answer` type that is not just a type alias of `Result` - Remove a usage of `map_layouts` to make the code easier to read - Don't hide errors related to Unknown Layout when computing transmutability
Diffstat (limited to 'compiler/rustc_transmute/src/lib.rs')
| -rw-r--r-- | compiler/rustc_transmute/src/lib.rs | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/compiler/rustc_transmute/src/lib.rs b/compiler/rustc_transmute/src/lib.rs index 7a8cbd50d45..34ad6bd8c69 100644 --- a/compiler/rustc_transmute/src/lib.rs +++ b/compiler/rustc_transmute/src/lib.rs @@ -19,10 +19,16 @@ pub struct Assume { pub validity: bool, } -/// Either we have an error, or we have an optional Condition that must hold. -pub type Answer<R> = Result<Option<Condition<R>>, Reason>; +/// Either we have an error, transmutation is allowed, or we have an optional +/// Condition that must hold. +#[derive(Debug, Hash, Eq, PartialEq, Clone)] +pub enum Answer<R> { + Yes, + No(Reason), + If(Condition<R>), +} -/// A condition which must hold for safe transmutation to be possible +/// A condition which must hold for safe transmutation to be possible. #[derive(Debug, Hash, Eq, PartialEq, Clone)] pub enum Condition<R> { /// `Src` is transmutable into `Dst`, if `src` is transmutable into `dst`. @@ -35,7 +41,7 @@ pub enum Condition<R> { IfAny(Vec<Condition<R>>), } -/// Answers: Why wasn't the source type transmutable into the destination type? +/// Answers "why wasn't the source type transmutable into the destination type?" #[derive(Debug, Hash, Eq, PartialEq, PartialOrd, Ord, Clone)] pub enum Reason { /// The layout of the source type is unspecified. @@ -52,6 +58,12 @@ pub enum Reason { DstHasStricterAlignment { src_min_align: usize, dst_min_align: usize }, /// Can't go from shared pointer to unique pointer DstIsMoreUnique, + /// Encountered a type error + TypeError, + /// The layout of src is unknown + SrcLayoutUnknown, + /// The layout of dst is unknown + DstLayoutUnknown, } #[cfg(feature = "rustc")] |
