From 0e3f745fb203c0df47ca2f587469dc2bc3ed7363 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Fri, 19 Feb 2021 16:40:44 -0500 Subject: [PATCH] [msl] derive thiserror --- src/back/glsl/mod.rs | 2 +- src/back/msl/mod.rs | 47 ++++++++++++++++-------------------------- src/back/msl/writer.rs | 31 +++++++++++++++------------- src/back/spv/writer.rs | 2 +- src/proc/validator.rs | 2 +- 5 files changed, 38 insertions(+), 46 deletions(-) diff --git a/src/back/glsl/mod.rs b/src/back/glsl/mod.rs index ae22cb213e..6a0a74a947 100644 --- a/src/back/glsl/mod.rs +++ b/src/back/glsl/mod.rs @@ -44,7 +44,7 @@ pub use features::Features; use crate::{ - proc::{analyzer::Analysis, NameKey, Namer, ResolveContext, TypifyError, Typifier}, + proc::{analyzer::Analysis, NameKey, Namer, ResolveContext, Typifier, TypifyError}, Arena, ArraySize, BinaryOperator, Binding, BuiltIn, Bytes, ConservativeDepth, Constant, ConstantInner, DerivativeAxis, Expression, FastHashMap, Function, GlobalVariable, Handle, ImageClass, Interpolation, LocalVariable, Module, RelationalFunction, ScalarKind, ScalarValue, diff --git a/src/back/msl/mod.rs b/src/back/msl/mod.rs index 585593ab50..9adeed2c62 100644 --- a/src/back/msl/mod.rs +++ b/src/back/msl/mod.rs @@ -56,43 +56,30 @@ enum ResolvedBinding { // Note: some of these should be removed in favor of proper IR validation. -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum Error { - IO(IoError), - Utf8(FromUtf8Error), - Type(TypifyError), + #[error(transparent)] + IO(#[from] IoError), + #[error(transparent)] + Utf8(#[from] FromUtf8Error), + #[error(transparent)] + Type(#[from] TypifyError), + #[error("bind source for {0:?} is missing from the map")] MissingBindTarget(BindSource), - InvalidImageAccess(crate::StorageAccess), - BadName(String), + #[error("bind target {0:?} is empty")] UnimplementedBindTarget(BindTarget), + #[error("composing of {0:?} is not implemented yet")] UnsupportedCompose(Handle), + #[error("operation {0:?} is not implemented yet")] UnsupportedBinaryOp(crate::BinaryOperator), - UnexpectedSampleLevel(crate::SampleLevel), + #[error("standard function '{0}' is not implemented yet")] UnsupportedCall(String), - UnsupportedDynamicArrayLength, + #[error("feature '{0}' is not implemented yet")] FeatureNotImplemented(String), - /// The source IR is not valid. + #[error("module is not valid")] Validation, } -impl From for Error { - fn from(e: IoError) -> Self { - Error::IO(e) - } -} - -impl From for Error { - fn from(e: FromUtf8Error) -> Self { - Error::Utf8(e) - } -} - -impl From for Error { - fn from(e: TypifyError) -> Self { - Error::Type(e) - } -} - #[derive(Clone, Copy, Debug)] enum LocationMode { VertexInput, @@ -155,8 +142,10 @@ impl Options { group, binding, }; - ResolvedBinding::Resource( - self.binding_map.get(&source).cloned(). + self.binding_map + .get(&source) + .cloned() + .map(ResolvedBinding::Resource) .ok_or(Error::MissingBindTarget(source)) } None => { diff --git a/src/back/msl/writer.rs b/src/back/msl/writer.rs index 99bc0974cd..69086c260e 100644 --- a/src/back/msl/writer.rs +++ b/src/back/msl/writer.rs @@ -680,20 +680,23 @@ impl Writer { } => { self.put_local_call(function, arguments, context)?; } - crate::Expression::ArrayLength(expr) => match *self - .typifier - .get(expr, &context.module.types) - { - crate::TypeInner::Array { - size: crate::ArraySize::Constant(const_handle), - .. - } => { - let size_str = &self.names[&NameKey::Constant(const_handle)]; - write!(self.out, "{}", size_str)?; + crate::Expression::ArrayLength(expr) => { + match *self.typifier.get(expr, &context.module.types) { + crate::TypeInner::Array { + size: crate::ArraySize::Constant(const_handle), + .. + } => { + let size_str = &self.names[&NameKey::Constant(const_handle)]; + write!(self.out, "{}", size_str)?; + } + crate::TypeInner::Array { .. } => { + return Err(Error::FeatureNotImplemented( + "dynamic array size".to_string(), + )) + } + _ => return Err(Error::Validation), } - crate::TypeInner::Array { .. } => return Err(Error::UnsupportedDynamicArrayLength), - _ => return Err(Error::Validation), - }, + } } Ok(()) } @@ -1025,7 +1028,7 @@ impl Writer { } else if global.storage_access.contains(crate::StorageAccess::LOAD) { "read" } else { - return Err(Error::InvalidImageAccess(global.storage_access)); + return Err(Error::Validation); }; ("texture", "", format.into(), access) } diff --git a/src/back/spv/writer.rs b/src/back/spv/writer.rs index a6c279358d..3476b480c2 100644 --- a/src/back/spv/writer.rs +++ b/src/back/spv/writer.rs @@ -4,7 +4,7 @@ use crate::{ arena::{Arena, Handle}, proc::{ analyzer::{Analysis, FunctionInfo}, - Layouter, ResolveContext, TypifyError, Typifier, + Layouter, ResolveContext, Typifier, TypifyError, }, }; use spirv::Word; diff --git a/src/proc/validator.rs b/src/proc/validator.rs index 55bc960235..4188c8ba36 100644 --- a/src/proc/validator.rs +++ b/src/proc/validator.rs @@ -1,6 +1,6 @@ use super::{ analyzer::{Analysis, AnalysisError, FunctionInfo, GlobalUse}, - typifier::{TypifyError, ResolveContext, Typifier}, + typifier::{ResolveContext, Typifier, TypifyError}, }; use crate::arena::{Arena, Handle}; use bit_set::BitSet;