[valid] make sure overrides are not present after evaluation

This commit is contained in:
teoxoy 2024-03-07 16:35:18 +01:00 committed by Teodor Tanasoaia
parent f1706b994b
commit 4ede83929c
3 changed files with 29 additions and 2 deletions

View File

@ -111,7 +111,7 @@ pub(super) fn process_overrides<'a>(
}
let mut validator = Validator::new(ValidationFlags::all(), Capabilities::all());
let module_info = validator.validate(&module)?;
let module_info = validator.validate_no_overrides(&module)?;
Ok((Cow::Owned(module), Cow::Owned(module_info)))
}

View File

@ -213,7 +213,7 @@ impl super::Validator {
crate::TypeInner::Scalar { .. } => {}
_ => return Err(super::ConstExpressionError::InvalidSplatType(value)),
},
_ if global_expr_kind.is_const(handle) => {
_ if global_expr_kind.is_const(handle) || !self.allow_overrides => {
return Err(super::ConstExpressionError::NonFullyEvaluatedConst)
}
// the constant evaluator will report errors about override-expressions

View File

@ -175,6 +175,7 @@ pub struct Validator {
valid_expression_list: Vec<Handle<crate::Expression>>,
valid_expression_set: BitSet,
override_ids: FastHashSet<u16>,
allow_overrides: bool,
}
#[derive(Clone, Debug, thiserror::Error)]
@ -203,6 +204,8 @@ pub enum OverrideError {
NonConstructibleType,
#[error("The type is not a scalar")]
TypeNotScalar,
#[error("Override declarations are not allowed")]
NotAllowed,
}
#[derive(Clone, Debug, thiserror::Error)]
@ -322,6 +325,7 @@ impl Validator {
valid_expression_list: Vec::new(),
valid_expression_set: BitSet::new(),
override_ids: FastHashSet::default(),
allow_overrides: true,
}
}
@ -370,6 +374,10 @@ impl Validator {
gctx: crate::proc::GlobalCtx,
mod_info: &ModuleInfo,
) -> Result<(), OverrideError> {
if !self.allow_overrides {
return Err(OverrideError::NotAllowed);
}
let o = &gctx.overrides[handle];
if o.name.is_none() && o.id.is_none() {
@ -414,6 +422,25 @@ impl Validator {
pub fn validate(
&mut self,
module: &crate::Module,
) -> Result<ModuleInfo, WithSpan<ValidationError>> {
self.allow_overrides = true;
self.validate_impl(module)
}
/// Check the given module to be valid.
///
/// With the additional restriction that overrides are not present.
pub fn validate_no_overrides(
&mut self,
module: &crate::Module,
) -> Result<ModuleInfo, WithSpan<ValidationError>> {
self.allow_overrides = false;
self.validate_impl(module)
}
fn validate_impl(
&mut self,
module: &crate::Module,
) -> Result<ModuleInfo, WithSpan<ValidationError>> {
self.reset();
self.reset_types(module.types.len());