From 38234dbf1ccedca651e4a34a653f04a5fc4e70e9 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Fri, 15 Sep 2023 09:01:31 -0700 Subject: [PATCH] Make entire `valid::handles` module dependent on "validate" feature. 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]`. --- src/valid/handles.rs | 39 +-------------------------------------- src/valid/mod.rs | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/valid/handles.rs b/src/valid/handles.rs index da95f60842..49be9616fe 100644 --- a/src/valid/handles.rs +++ b/src/valid/handles.rs @@ -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: /// @@ -547,52 +540,24 @@ impl super::Validator { } } -#[cfg(feature = "validate")] impl From for ValidationError { fn from(source: BadHandle) -> Self { Self::InvalidHandle(source.into()) } } -#[cfg(feature = "validate")] impl From for ValidationError { fn from(source: FwdDepError) -> Self { Self::InvalidHandle(source.into()) } } -#[cfg(feature = "validate")] impl From 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 Handle { /// Check that `self` is valid within `arena` using [`Arena::check_contains_handle`]. pub(self) fn check_valid_for(self, arena: &Arena) -> Result<(), InvalidHandleError> { @@ -656,7 +621,6 @@ impl Handle { } } -#[cfg(feature = "validate")] impl crate::arena::Range { pub(self) fn check_valid_for(&self, arena: &Arena) -> Result<(), BadRangeError> { arena.check_contains_range(self) @@ -664,7 +628,6 @@ impl crate::arena::Range { } #[test] -#[cfg(feature = "validate")] fn constant_deps() { use crate::{Constant, Expression, Literal, Span, Type, TypeInner}; diff --git a/src/valid/mod.rs b/src/valid/mod.rs index 5351287725..fb7fb16f2f 100644 --- a/src/valid/mod.rs +++ b/src/valid/mod.rs @@ -6,6 +6,7 @@ mod analyzer; mod compose; mod expression; mod function; +#[cfg(feature = "validate")] mod handles; mod interface; mod r#type; @@ -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.