From 7f075c2a88c0dfa7e199fbc5a61f56989ada3d15 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 20 Nov 2020 11:03:04 +0000 Subject: [PATCH] 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 Reviewed-by: dan sinclair --- src/reader/wgsl/parser_impl.cc | 8 ++++++++ src/reader/wgsl/parser_impl_error_msg_test.cc | 11 +++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index 0d6f4d58df..8773fd0665 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -2577,6 +2577,14 @@ Maybe ParserImpl::assignment_stmt() { 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 8a5c15d1bb..cf5906bd31 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, AssignmentStmtMissingAssignment) { 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 @@ TEST_F(ParserImplErrorTest, ForLoopInitializerMissingSemicolon) { " ^\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"