fix(naga): validate @must_use usage on non-function declarations (#9367)

---------

Co-authored-by: dongsong <dongsong@pkpm.com.cn>
This commit is contained in:
dnsn021anddongsong authored and GitHub committed 2026-04-03 17:20:04 +00:00
1 parent 41bb9cbd25
commit 3a8b7eaf05
6 files changed
+37 -1

No files matched your search

+1
View File
@@ -90,6 +90,7 @@ Bottom level categories:
- Naga no longer allows derivative operations on `f16`. WGSL does not currently allow this, although [it may be added in the future](https://github.com/gpuweb/gpuweb/issues/5482). By @andyleiserson in [#9154](https://github.com/gfx-rs/wgpu/pull/9154).
- Disallow direct access to atomic variables in WGSL front-end (e.g. `let x = myAtomic;`). By @ecoricemon in [#9262](https://github.com/gfx-rs/wgpu/pull/9262).
- Fixed handling of unterminated block comments. By @BKDaugherty in [#9356](https://github.com/gfx-rs/wgpu/pull/9356).
- Enforce that `@must_use` appear only on function declarations. By @dnsn021 in [#9367](https://github.com/gfx-rs/wgpu/pull/9367).
#### dx12
-1
View File
@@ -125,7 +125,6 @@ webgpu:shader,validation,parse,blankspace:* // 95%, null in comments, https://gi
webgpu:shader,validation,parse,comments:* // 93%, unterminated block comments, https://github.com/gfx-rs/wgpu/issues/8877
webgpu:shader,validation,parse,diagnostic:* // 50%, https://github.com/gfx-rs/wgpu/issues/6458
webgpu:shader,validation,parse,literal:* // 96%, https://github.com/gfx-rs/wgpu/issues/7046
webgpu:shader,validation,parse,must_use:* // 97%, https://github.com/gfx-rs/wgpu/issues/8876
webgpu:shader,validation,parse,shadow_builtins:* // 83%, function param shadowing parser issue; determinant const-eval; texture_external capability missing
webgpu:shader,validation,shader_io,align:* // 98%, @align(2^31) accepted but exceeds i32::MAX per WGSL spec
webgpu:shader,validation,shader_io,id:* // 97%, @id on const
+1
View File
@@ -450,6 +450,7 @@ webgpu:shader,validation,parse,blankspace:null_characters:contains_null=true;pla
webgpu:shader,validation,parse,comments:unterminated_block_comment:terminated=false
webgpu:shader,validation,parse,enable:*
webgpu:shader,validation,parse,identifiers:*
webgpu:shader,validation,parse,must_use:*
webgpu:shader,validation,parse,requires:*
webgpu:shader,validation,parse,semicolon:*
webgpu:shader,validation,parse,shadow_builtins:function_param:*
+8
View File
@@ -362,6 +362,7 @@ pub(crate) enum Error<'a> {
FunctionReturnsVoid(Span),
FunctionMustUseUnused(Span),
FunctionMustUseReturnsVoid(Span, Span),
FunctionMustUseOnNonFunction(Span),
InvalidWorkGroupUniformLoad(Span),
Internal(&'static str),
ExpectedConstExprConcreteIntegerScalar(Span),
@@ -1077,6 +1078,13 @@ impl<'a> Error<'a> {
"declare a return type or remove the attribute".into(),
],
},
Error::FunctionMustUseOnNonFunction(attr) => ParseError {
message: "attribute `@must_use` is only valid on function declarations".into(),
labels: vec![(attr, "".into())],
notes: vec![
"place `@must_use` on a function declaration with a return type".into(),
],
},
Error::InvalidWorkGroupUniformLoad(span) => ParseError {
message: "incorrect type passed to workgroupUniformLoad".into(),
labels: vec![(span, "".into())],
+6
View File
@@ -2203,6 +2203,12 @@ impl Parser {
}
};
if let Some(must_use_span) = must_use.value {
if !matches!(kind.as_ref(), Some(ast::GlobalDeclKind::Fn(_))) {
return Err(Box::new(Error::FunctionMustUseOnNonFunction(must_use_span)));
}
}
if let Some(kind) = kind {
out.decls.append(
ast::GlobalDecl { kind, dependencies },
+21
View File
@@ -3158,6 +3158,27 @@ struct S {
)
}
#[test]
fn global_var_must_use() {
check(
r#"
@must_use
@group(0)
@binding(0)
var<storage> x : array<u32>;
"#,
r#"error: attribute `@must_use` is only valid on function declarations
┌─ wgsl:2:2
│
2 │ @must_use
│ ^^^^^^^^
│
= note: place `@must_use` on a function declaration with a return type
"#,
)
}
#[test]
fn function_param_redefinition_as_param() {
check(