reader/wgsl: Error for unconsumed decorations
When applied to valid module-scope declarations.
Fixed: chromium:1244349
Change-Id: Icb19200cae751ac70974481693ecbcf48fd627f0
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/63160
Auto-Submit: Ben Clayton <bclayton@google.com>
Commit-Queue: James Price <jrprice@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc
index 18ae656..637375d 100644
--- a/src/reader/wgsl/parser_impl.cc
+++ b/src/reader/wgsl/parser_impl.cc
@@ -427,27 +427,32 @@
return Failure::kNoMatch;
});
- if (decl.errored)
+ if (decl.errored) {
errored = true;
- if (decl.matched)
- return true;
+ }
+ if (decl.matched) {
+ return expect_decorations_consumed(decos.value);
+ }
auto func = function_decl(decos.value);
- if (func.errored)
+ if (func.errored) {
errored = true;
+ }
if (func.matched) {
builder_.AST().AddFunction(func.value);
return true;
}
- if (errored)
+ if (errored) {
return Failure::kErrored;
+ }
// Invalid syntax found - try and determine the best error message
// We have decorations parsed, but nothing to consume them?
- if (decos.value.size() > 0)
+ if (decos.value.size() > 0) {
return add_error(next(), "expected declaration after decorations");
+ }
// We have a statement outside of a function?
auto t = peek();
diff --git a/src/reader/wgsl/parser_impl_error_msg_test.cc b/src/reader/wgsl/parser_impl_error_msg_test.cc
index 65e8eaf..07007b4 100644
--- a/src/reader/wgsl/parser_impl_error_msg_test.cc
+++ b/src/reader/wgsl/parser_impl_error_msg_test.cc
@@ -51,6 +51,13 @@
" ^\n");
}
+TEST_F(ParserImplErrorTest, AliasDeclInvalidDeco) {
+ EXPECT("[[block]]type e=u32;",
+ "test.wgsl:1:3 error: unexpected decorations\n"
+ "[[block]]type e=u32;\n"
+ " ^^^^^\n");
+}
+
TEST_F(ParserImplErrorTest, ArrayIndexExprInvalidExpr) {
EXPECT("fn f() { x = y[^]; }",
"test.wgsl:1:16 error: unable to parse expression inside []\n"
@@ -275,13 +282,6 @@
" ^\n");
}
-TEST_F(ParserImplErrorTest, FunctionDeclInvalid) {
- EXPECT("[[stage(vertex)]] x;",
- "test.wgsl:1:19 error: expected declaration after decorations\n"
- "[[stage(vertex)]] x;\n"
- " ^\n");
-}
-
TEST_F(ParserImplErrorTest, FunctionDeclDecoMissingEnd) {
EXPECT("[[stage(vertex) fn f() {}",
"test.wgsl:1:17 error: expected ']]' for decoration list\n"
@@ -551,6 +551,13 @@
" ^\n");
}
+TEST_F(ParserImplErrorTest, GlobalDeclInvalidDeco) {
+ EXPECT("[[stage(vertex)]] x;",
+ "test.wgsl:1:19 error: expected declaration after decorations\n"
+ "[[stage(vertex)]] x;\n"
+ " ^\n");
+}
+
TEST_F(ParserImplErrorTest, GlobalDeclSampledTextureMissingLessThan) {
EXPECT("var x : texture_1d;",
"test.wgsl:1:19 error: expected '<' for sampled texture type\n"