Skip to content

Commit

Permalink
validate variable initializer for address spaces (#2513)
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy authored Sep 28, 2023
1 parent a17a93e commit f72489b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/valid/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions tests/wgsl-errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,21 @@ fn host_shareable_types() {
}
}

#[test]
fn var_init() {
check_validation! {
"
var<workgroup> initialized: u32 = 0u;
":
Err(
naga::valid::ValidationError::GlobalVariable {
source: naga::valid::GlobalVariableError::InitializerNotAllowed(naga::AddressSpace::WorkGroup),
..
},
)
}
}

#[test]
fn misplaced_break_if() {
check(
Expand Down

0 comments on commit f72489b

Please sign in to comment.