Skip to content
This repository was archived by the owner on Jan 29, 2025. It is now read-only.

Commit f72489b

Browse files
authored
validate variable initializer for address spaces (#2513)
1 parent a17a93e commit f72489b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/valid/interface.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum GlobalVariableError {
3333
),
3434
#[error("Initializer doesn't match the variable type")]
3535
InitializerType,
36+
#[error("Initializer can't be used with address space {0:?}")]
37+
InitializerNotAllowed(crate::AddressSpace),
3638
#[error("Storage address space doesn't support write-only access")]
3739
StorageAddressSpaceWriteOnlyNotSupported,
3840
}
@@ -572,6 +574,13 @@ impl super::Validator {
572574
}
573575

574576
if let Some(init) = var.init {
577+
match var.space {
578+
crate::AddressSpace::Private | crate::AddressSpace::Function => {}
579+
_ => {
580+
return Err(GlobalVariableError::InitializerNotAllowed(var.space));
581+
}
582+
}
583+
575584
let decl_ty = &gctx.types[var.ty].inner;
576585
let init_ty = mod_info[init].inner_with(gctx.types);
577586
if !decl_ty.equivalent(init_ty, gctx.types) {

tests/wgsl-errors.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,21 @@ fn host_shareable_types() {
16061606
}
16071607
}
16081608

1609+
#[test]
1610+
fn var_init() {
1611+
check_validation! {
1612+
"
1613+
var<workgroup> initialized: u32 = 0u;
1614+
":
1615+
Err(
1616+
naga::valid::ValidationError::GlobalVariable {
1617+
source: naga::valid::GlobalVariableError::InitializerNotAllowed(naga::AddressSpace::WorkGroup),
1618+
..
1619+
},
1620+
)
1621+
}
1622+
}
1623+
16091624
#[test]
16101625
fn misplaced_break_if() {
16111626
check(

0 commit comments

Comments
 (0)