From f72489be00b64f831f6f7fba3b06dae3cc37f8eb Mon Sep 17 00:00:00 2001 From: Teodor Tanasoaia <28601907+teoxoy@users.noreply.github.com> Date: Thu, 28 Sep 2023 20:32:46 +0200 Subject: [PATCH] validate variable initializer for address spaces (#2513) --- src/valid/interface.rs | 9 +++++++++ tests/wgsl-errors.rs | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/valid/interface.rs b/src/valid/interface.rs index 2444d29d9..ef88a46a8 100644 --- a/src/valid/interface.rs +++ b/src/valid/interface.rs @@ -33,6 +33,8 @@ pub enum GlobalVariableError { ), #[error("Initializer doesn't match the variable type")] InitializerType, + #[error("Initializer can't be used with address space {0:?}")] + InitializerNotAllowed(crate::AddressSpace), #[error("Storage address space doesn't support write-only access")] StorageAddressSpaceWriteOnlyNotSupported, } @@ -572,6 +574,13 @@ impl super::Validator { } if let Some(init) = var.init { + match var.space { + crate::AddressSpace::Private | crate::AddressSpace::Function => {} + _ => { + return Err(GlobalVariableError::InitializerNotAllowed(var.space)); + } + } + let decl_ty = &gctx.types[var.ty].inner; let init_ty = mod_info[init].inner_with(gctx.types); if !decl_ty.equivalent(init_ty, gctx.types) { diff --git a/tests/wgsl-errors.rs b/tests/wgsl-errors.rs index 48318f280..b164f6955 100644 --- a/tests/wgsl-errors.rs +++ b/tests/wgsl-errors.rs @@ -1606,6 +1606,21 @@ fn host_shareable_types() { } } +#[test] +fn var_init() { + check_validation! { + " + var initialized: u32 = 0u; + ": + Err( + naga::valid::ValidationError::GlobalVariable { + source: naga::valid::GlobalVariableError::InitializerNotAllowed(naga::AddressSpace::WorkGroup), + .. + }, + ) + } +} + #[test] fn misplaced_break_if() { check(