diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ece886a0..050305c89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cts_runner/fail.lst b/cts_runner/fail.lst index a46978c7b..fea9e119e 100644 --- a/cts_runner/fail.lst +++ b/cts_runner/fail.lst @@ -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 diff --git a/cts_runner/test.lst b/cts_runner/test.lst index 834473651..031ced0b6 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -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:* diff --git a/naga/src/front/wgsl/error.rs b/naga/src/front/wgsl/error.rs index 538215c1e..59d226833 100644 --- a/naga/src/front/wgsl/error.rs +++ b/naga/src/front/wgsl/error.rs @@ -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())], diff --git a/naga/src/front/wgsl/parse/mod.rs b/naga/src/front/wgsl/parse/mod.rs index 45c5514ae..5f503ef22 100644 --- a/naga/src/front/wgsl/parse/mod.rs +++ b/naga/src/front/wgsl/parse/mod.rs @@ -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 }, diff --git a/naga/tests/naga/wgsl_errors.rs b/naga/tests/naga/wgsl_errors.rs index ec98311f3..97bed72b7 100644 --- a/naga/tests/naga/wgsl_errors.rs +++ b/naga/tests/naga/wgsl_errors.rs @@ -3158,6 +3158,27 @@ struct S { ) } +#[test] +fn global_var_must_use() { + check( + r#" +@must_use +@group(0) +@binding(0) +var x : array; +"#, + 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(