Skip to content

Commit

Permalink
Make entire valid::handles module dependent on "validate" feature.
Browse files Browse the repository at this point in the history
Rather than having `#[cfg(feature = "validate")]` attributes on almost
every definition in `src/valid/handles.rs`, move the definitions we
need unconditionally out of the module, and just put the whole module
under a `#[cfg]`.
  • Loading branch information
jimblandy committed Sep 15, 2023
1 parent 1281c11 commit 25f66b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
39 changes: 1 addition & 38 deletions src/valid/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ use crate::{
arena::{BadHandle, BadRangeError},
Handle,
};

#[cfg(feature = "validate")]
use crate::{Arena, UniqueArena};

#[cfg(feature = "validate")]
use super::ValidationError;

#[cfg(feature = "validate")]
use super::{ValidationError, InvalidHandleError, FwdDepError};
use std::{convert::TryInto, hash::Hash, num::NonZeroU32};

#[cfg(feature = "validate")]
impl super::Validator {
/// Validates that all handles within `module` are:
///
Expand Down Expand Up @@ -547,52 +540,24 @@ impl super::Validator {
}
}

#[cfg(feature = "validate")]
impl From<BadHandle> for ValidationError {
fn from(source: BadHandle) -> Self {
Self::InvalidHandle(source.into())
}
}

#[cfg(feature = "validate")]
impl From<FwdDepError> for ValidationError {
fn from(source: FwdDepError) -> Self {
Self::InvalidHandle(source.into())
}
}

#[cfg(feature = "validate")]
impl From<BadRangeError> for ValidationError {
fn from(source: BadRangeError) -> Self {
Self::InvalidHandle(source.into())
}
}

#[derive(Clone, Debug, thiserror::Error)]
pub enum InvalidHandleError {
#[error(transparent)]
BadHandle(#[from] BadHandle),
#[error(transparent)]
ForwardDependency(#[from] FwdDepError),
#[error(transparent)]
BadRange(#[from] BadRangeError),
}

#[derive(Clone, Debug, thiserror::Error)]
#[error(
"{subject:?} of kind {subject_kind:?} depends on {depends_on:?} of kind {depends_on_kind}, \
which has not been processed yet"
)]
pub struct FwdDepError {
// This error is used for many `Handle` types, but there's no point in making this generic, so
// we just flatten them all to `Handle<()>` here.
subject: Handle<()>,
subject_kind: &'static str,
depends_on: Handle<()>,
depends_on_kind: &'static str,
}

#[cfg(feature = "validate")]
impl<T> Handle<T> {
/// Check that `self` is valid within `arena` using [`Arena::check_contains_handle`].
pub(self) fn check_valid_for(self, arena: &Arena<T>) -> Result<(), InvalidHandleError> {
Expand Down Expand Up @@ -656,15 +621,13 @@ impl<T> Handle<T> {
}
}

#[cfg(feature = "validate")]
impl<T> crate::arena::Range<T> {
pub(self) fn check_valid_for(&self, arena: &Arena<T>) -> Result<(), BadRangeError> {
arena.check_contains_range(self)
}
}

#[test]
#[cfg(feature = "validate")]
fn constant_deps() {
use crate::{Constant, Expression, Literal, Span, Type, TypeInner};

Expand Down
25 changes: 24 additions & 1 deletion src/valid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod analyzer;
mod compose;
mod expression;
mod function;
#[cfg(feature = "validate")]
mod handles;
mod interface;
mod r#type;
Expand All @@ -29,7 +30,29 @@ pub use function::{CallError, FunctionError, LocalVariableError};
pub use interface::{EntryPointError, GlobalVariableError, VaryingError};
pub use r#type::{Disalignment, TypeError, TypeFlags};

use self::handles::InvalidHandleError;
#[derive(Clone, Debug, thiserror::Error)]
pub enum InvalidHandleError {
#[error(transparent)]
BadHandle(#[from] crate::arena::BadHandle),
#[error(transparent)]
ForwardDependency(#[from] FwdDepError),
#[error(transparent)]
BadRange(#[from] crate::arena::BadRangeError),
}

#[derive(Clone, Debug, thiserror::Error)]
#[error(
"{subject:?} of kind {subject_kind:?} depends on {depends_on:?} of kind {depends_on_kind}, \
which has not been processed yet"
)]
pub struct FwdDepError {
// This error is used for many `Handle` types, but there's no point in making this generic, so
// we just flatten them all to `Handle<()>` here.
subject: Handle<()>,
subject_kind: &'static str,
depends_on: Handle<()>,
depends_on_kind: &'static str,
}

bitflags::bitflags! {
/// Validation flags.
Expand Down

0 comments on commit 25f66b7

Please sign in to comment.