Skip to content

Commit

Permalink
Give Validator::check_width its own error type, WidthError.
Browse files Browse the repository at this point in the history
Later changes will use `check_width` in new functions. Separating out
`WidthError` will make it easier to incorporate `check_width`'s
results into its new callers'.
  • Loading branch information
jimblandy authored and teoxoy committed Oct 19, 2023
1 parent 5b65f11 commit 860c200
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/valid/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ pub enum Disalignment {
pub enum TypeError {
#[error("Capability {0:?} is required")]
MissingCapability(Capabilities),
#[error("The {0:?} scalar width {1} is not supported")]
InvalidWidth(crate::ScalarKind, crate::Bytes),
#[error("The {0:?} scalar width {1} is not supported for an atomic")]
InvalidAtomicWidth(crate::ScalarKind, crate::Bytes),
#[error("Invalid type for pointer target {0:?}")]
Expand Down Expand Up @@ -126,6 +124,22 @@ pub enum TypeError {
},
#[error("Structure types must have at least one member")]
EmptyStruct,
#[error(transparent)]
WidthError(#[from] WidthError),
}

#[derive(Clone, Debug, thiserror::Error)]
pub enum WidthError {
#[error("The {0:?} scalar width {1} is not supported")]
Invalid(crate::ScalarKind, crate::Bytes),
#[error("Using `{name}` values requires the `naga::valid::Capabilities::{flag}` flag")]
MissingCapability {
name: &'static str,
flag: &'static str,
},

#[error("64-bit integers are not yet supported")]
Unsupported64Bit,
}

// Only makes sense if `flags.contains(HOST_SHAREABLE)`
Expand Down Expand Up @@ -209,16 +223,21 @@ impl super::Validator {
}
}

pub(super) fn check_width(
pub(super) const fn check_width(
&self,
kind: crate::ScalarKind,
width: crate::Bytes,
) -> Result<(), TypeError> {
) -> Result<(), WidthError> {
let good = match kind {
crate::ScalarKind::Bool => width == crate::BOOL_WIDTH,
crate::ScalarKind::Float => {
if width == 8 {
self.require_type_capability(Capabilities::FLOAT64)?;
if !self.capabilities.contains(Capabilities::FLOAT64) {
return Err(WidthError::MissingCapability {
name: "f64",
flag: "FLOAT64",
});
}
true
} else {
width == 4
Expand All @@ -229,7 +248,7 @@ impl super::Validator {
if good {
Ok(())
} else {
Err(TypeError::InvalidWidth(kind, width))
Err(WidthError::Invalid(kind, width))
}
}

Expand Down

0 comments on commit 860c200

Please sign in to comment.