Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make entire valid::handles module dependent on "validate" feature. #2479

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 1 addition & 38 deletions src/valid/handles.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
//! Implementation of `Validator::validate_module_handles`.

use super::{FwdDepError, InvalidHandleError, ValidationError};
use crate::{
arena::{BadHandle, BadRangeError},
Handle,
};

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

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

#[cfg(feature = "validate")]
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