Skip to content

Commit 12259ba

Browse files
naga: Accept var<function> syntax for local variables (#8710)
1 parent a35e5a2 commit 12259ba

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ By @cwfitzgerald in [#8609](https://github.com/gfx-rs/wgpu/pull/8609).
206206

207207
- Prevent UB with invalid ray query calls on spirv. By @Vecvec in [#8390](https://github.com/gfx-rs/wgpu/pull/8390).
208208
- Update the set of binding_array capabilities. In most cases, they are set automatically from `wgpu` features, and this change should not be user-visible. By @andyleiserson in [#8671](https://github.com/gfx-rs/wgpu/pull/8671).
209+
- Naga now accepts the `var<function>` syntax for declaring local variables. By @andyleiserson in [#8710](https://github.com/gfx-rs/wgpu/pull/8710).
209210

210211
### Bug Fixes
211212

cts_runner/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ webgpu:shader,execution,flow_control,return:*
175175
// Many other vertex_buffer_access subtests also passing, but there are too many to enumerate.
176176
// Fails on Metal in CI only, not when running locally.
177177
fails-if(metal) webgpu:shader,execution,robust_access_vertex:vertex_buffer_access:indexed=true;indirect=false;drawCallTestParameter="baseVertex";type="float32x4";additionalBuffers=4;partialLastNumber=false;offsetVertexBuffer=true
178+
webgpu:shader,validation,expression,call,builtin,all:arguments:test="ptr_deref"
178179
webgpu:shader,validation,expression,call,builtin,max:values:*
179180
webgpu:shader,validation,statement,statement_behavior:invalid_statements:body="break"
180181
webgpu:shader,validation,statement,statement_behavior:invalid_statements:body="break_if"

naga/src/front/wgsl/error.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ pub(crate) enum Error<'a> {
199199
InvalidIdentifierUnderscore(Span),
200200
ReservedIdentifierPrefix(Span),
201201
UnknownAddressSpace(Span),
202+
InvalidLocalVariableAddressSpace(Span),
202203
RepeatedAttribute(Span),
203204
UnknownAttribute(Span),
204205
UnknownBuiltin(Span),
@@ -660,6 +661,11 @@ impl<'a> Error<'a> {
660661
labels: vec![(bad_span, "unknown address space".into())],
661662
notes: vec![],
662663
},
664+
Error::InvalidLocalVariableAddressSpace(bad_span) => ParseError {
665+
message: format!("invalid address space for local variable: `{}`", &source[bad_span]),
666+
labels: vec![(bad_span, "local variables can only use 'function' address space".into())],
667+
notes: vec![],
668+
},
663669
Error::RepeatedAttribute(bad_span) => ParseError {
664670
message: format!("repeated attribute: `{}`", &source[bad_span]),
665671
labels: vec![(bad_span, "repeated attribute".into())],

naga/src/front/wgsl/parse/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,16 @@ impl Parser {
22732273
"var" => {
22742274
let _ = lexer.next();
22752275

2276+
if lexer.skip(Token::Paren('<')) {
2277+
let (class_str, span) = lexer.next_ident_with_span()?;
2278+
if class_str != "function" {
2279+
return Err(Box::new(Error::InvalidLocalVariableAddressSpace(
2280+
span,
2281+
)));
2282+
}
2283+
lexer.expect(Token::Paren('>'))?;
2284+
}
2285+
22762286
let name = lexer.next_ident()?;
22772287
let ty = if lexer.skip(Token::Separator(':')) {
22782288
let ty = this.type_decl(lexer, ctx)?;

naga/src/front/wgsl/tests.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,41 @@ fn parse_assignment_statements() {
456456
);
457457
}
458458

459+
#[test]
460+
fn parse_local_var_address_space() {
461+
parse_str(
462+
"
463+
fn foo() {
464+
var<function> a = true;
465+
var<function> b: i32 = 5;
466+
var c = 10;
467+
}",
468+
)
469+
.unwrap();
470+
471+
let error = parse_str(
472+
"fn foo() {
473+
var<private> x: i32 = 5;
474+
}",
475+
)
476+
.unwrap_err();
477+
assert_eq!(
478+
error.message(),
479+
"invalid address space for local variable: `private`",
480+
);
481+
482+
let error = parse_str(
483+
"fn foo() {
484+
var<storage> x: i32 = 5;
485+
}",
486+
)
487+
.unwrap_err();
488+
assert_eq!(
489+
error.message(),
490+
"invalid address space for local variable: `storage`",
491+
);
492+
}
493+
459494
#[test]
460495
fn binary_expression_mixed_scalar_and_vector_operands() {
461496
for (operand, expect_splat) in [

0 commit comments

Comments
 (0)