diff --git a/src/arena.rs b/src/arena.rs index 1727da30d4..58c65997d5 100644 --- a/src/arena.rs +++ b/src/arena.rs @@ -1,11 +1,8 @@ use std::{cmp::Ordering, fmt, hash, marker::PhantomData, num::NonZeroU32}; /// An unique index in the arena array that a handle points to. -/// -/// This type is independent of `spv::Word`. `spv::Word` is used in data -/// representation. It holds a SPIR-V and refers to that instruction. In -/// structured representation, we use Handle to refer to an SPIR-V instruction. -/// `Index` is an implementation detail to `Handle`. +/// The "non-zero" part ensures that an `Option>` has +/// the same size and representation as `Handle`. type Index = NonZeroU32; /// A strongly typed reference to a SPIR-V element. diff --git a/src/front/spv/mod.rs b/src/front/spv/mod.rs index 3c99fc2afb..b59fd239b4 100644 --- a/src/front/spv/mod.rs +++ b/src/front/spv/mod.rs @@ -1131,11 +1131,10 @@ impl> Parser { let value_lexp = self.lookup_expression.lookup(value_id)?; let ty_lookup = self.lookup_type.lookup(result_type_id)?; - let kind = match type_arena[ty_lookup.handle].inner { - crate::TypeInner::Scalar { kind, .. } - | crate::TypeInner::Vector { kind, .. } => kind, - _ => return Err(Error::InvalidAsType(ty_lookup.handle)), - }; + let kind = type_arena[ty_lookup.handle] + .inner + .scalar_kind() + .ok_or(Error::InvalidAsType(ty_lookup.handle))?; let expr = crate::Expression::As { expr: value_lexp.handle, @@ -2071,10 +2070,10 @@ impl> Parser { let format = self.next()?; let base_handle = self.lookup_type.lookup(sample_type_id)?.handle; - let kind = match module.types[base_handle].inner { - crate::TypeInner::Scalar { kind, .. } | crate::TypeInner::Vector { kind, .. } => kind, - _ => return Err(Error::InvalidImageBaseType(base_handle)), - }; + let kind = module.types[base_handle] + .inner + .scalar_kind() + .ok_or(Error::InvalidImageBaseType(base_handle))?; let class = if format != 0 { crate::ImageClass::Storage(map_image_format(format)?) diff --git a/src/proc/mod.rs b/src/proc/mod.rs index cb21c042c1..65ee3a6be5 100644 --- a/src/proc/mod.rs +++ b/src/proc/mod.rs @@ -47,3 +47,15 @@ impl From for super::ScalarKind { } } } + +impl crate::TypeInner { + pub fn scalar_kind(&self) -> Option { + match *self { + super::TypeInner::Scalar { kind, .. } | super::TypeInner::Vector { kind, .. } => { + Some(kind) + } + super::TypeInner::Matrix { .. } => Some(super::ScalarKind::Float), + _ => None, + } + } +} diff --git a/src/proc/validator.rs b/src/proc/validator.rs index bfb2858c61..de37ec5769 100644 --- a/src/proc/validator.rs +++ b/src/proc/validator.rs @@ -263,17 +263,12 @@ impl Validator { match (stage, var.class) { (crate::ShaderStage::Vertex, crate::StorageClass::Output) | (crate::ShaderStage::Fragment, crate::StorageClass::Input) => { - match module.types[var.ty].inner { - crate::TypeInner::Scalar { kind, .. } - | crate::TypeInner::Vector { kind, .. } => { - if kind != crate::ScalarKind::Float - && var.interpolation != Some(crate::Interpolation::Flat) - { - return Err(EntryPointError::InvalidIntegerInterpolation); - } + match module.types[var.ty].inner.scalar_kind() { + Some(crate::ScalarKind::Float) => {} + Some(_) if var.interpolation != Some(crate::Interpolation::Flat) => { + return Err(EntryPointError::InvalidIntegerInterpolation); } - crate::TypeInner::Matrix { .. } => {} - _ => unreachable!(), + _ => {} } } _ => {}