reader/wsgl: Improve error message for missing 'var'

Fixes: tint:295
Change-Id: Id01ad61fa24f14a1d86ca945d941fd27ee1e8f82
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33400
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc
index 0d6f4d5..8773fd0 100644
--- a/src/reader/wgsl/parser_impl.cc
+++ b/src/reader/wgsl/parser_impl.cc
@@ -2577,6 +2577,14 @@
   auto t = peek();
   auto source = t.source();
 
+  // tint:295 - Test for `ident COLON` - this is invalid grammar, and without
+  // special casing will error as "missing = for assignment", which is less
+  // helpful than this error message:
+  if (peek(0).IsIdentifier() && peek(1).IsColon()) {
+    return add_error(peek(0).source(),
+                     "expected 'var' for variable declaration");
+  }
+
   auto lhs = unary_expression();
   if (lhs.errored)
     return Failure::kErrored;
diff --git a/src/reader/wgsl/parser_impl_error_msg_test.cc b/src/reader/wgsl/parser_impl_error_msg_test.cc
index 8a5c15d..cf5906b 100644
--- a/src/reader/wgsl/parser_impl_error_msg_test.cc
+++ b/src/reader/wgsl/parser_impl_error_msg_test.cc
@@ -70,9 +70,9 @@
 
 TEST_F(ParserImplErrorTest, AssignmentStmtMissingAssignment2) {
   EXPECT("fn f() -> void { a : i32; }",
-         "test.wgsl:1:20 error: expected '=' for assignment\n"
+         "test.wgsl:1:18 error: expected 'var' for variable declaration\n"
          "fn f() -> void { a : i32; }\n"
-         "                   ^\n");
+         "                 ^\n");
 }
 
 TEST_F(ParserImplErrorTest, AssignmentStmtMissingSemicolon) {
@@ -216,6 +216,13 @@
          "                                      ^\n");
 }
 
+TEST_F(ParserImplErrorTest, ForLoopInitializerMissingVar) {
+  EXPECT("fn f() -> void { for (i : i32 = 0; i < 8; i=i+1) {} }",
+         "test.wgsl:1:23 error: expected 'var' for variable declaration\n"
+         "fn f() -> void { for (i : i32 = 0; i < 8; i=i+1) {} }\n"
+         "                      ^\n");
+}
+
 TEST_F(ParserImplErrorTest, ForLoopConditionMissingSemicolon) {
   EXPECT("fn f() -> void { for (var i : i32 = 0; i < 8 i=i+1) {} }",
          "test.wgsl:1:46 error: expected ';' for condition in for loop\n"