From fecf10664a7e955ed4d86a77c5c3445b580a9518 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 23 Nov 2020 20:07:15 +0000 Subject: [PATCH] Improve error message when suffixing float with f Fixes: tint:277 Change-Id: I7ec3bb758c7d80c95c6ecd51eebf4144b696bec5 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/33668 Commit-Queue: Ben Clayton Reviewed-by: David Neto --- src/reader/wgsl/parser_impl.cc | 5 +++++ src/reader/wgsl/parser_impl_error_msg_test.cc | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/reader/wgsl/parser_impl.cc b/src/reader/wgsl/parser_impl.cc index f914bd4ee1..8e5c0cf672 100644 --- a/src/reader/wgsl/parser_impl.cc +++ b/src/reader/wgsl/parser_impl.cc @@ -2622,6 +2622,11 @@ Maybe ParserImpl::const_literal() { return create(type, t.to_u32()); } if (match(Token::Type::kFloatLiteral)) { + auto p = peek(); + if (p.IsIdentifier() && p.to_str() == "f") { + next(); // Consume 'f' + add_error(p.source(), "float literals must not be suffixed with 'f'"); + } auto* type = module_.create(); return create(type, t.to_f32()); } diff --git a/src/reader/wgsl/parser_impl_error_msg_test.cc b/src/reader/wgsl/parser_impl_error_msg_test.cc index cf5906bd31..8a533358b4 100644 --- a/src/reader/wgsl/parser_impl_error_msg_test.cc +++ b/src/reader/wgsl/parser_impl_error_msg_test.cc @@ -209,6 +209,13 @@ TEST_F(ParserImplErrorTest, EqualityInvalidExpr) { " ^\n"); } +TEST_F(ParserImplErrorTest, FloatLiteralSuffixedWithF) { + EXPECT("var f : f32 = 1.23f;", + "test.wgsl:1:19 error: float literals must not be suffixed with 'f'\n" + "var f : f32 = 1.23f;\n" + " ^\n"); +} + TEST_F(ParserImplErrorTest, ForLoopInitializerMissingSemicolon) { EXPECT("fn f() -> void { for (var i : i32 = 0 i < 8; i=i+1) {} }", "test.wgsl:1:39 error: expected ';' for initializer in for loop\n"