diff --git a/src/reader/wgsl/lexer.cc b/src/reader/wgsl/lexer.cc index 00c0f8d79d..ada6580d84 100644 --- a/src/reader/wgsl/lexer.cc +++ b/src/reader/wgsl/lexer.cc @@ -681,6 +681,10 @@ Token Lexer::try_hex_integer() { "...) has too many digits"}; } } + if (first == end) { + return {Token::Type::kError, source, + "integer or float hex literal has no significant digits"}; + } pos_ = end; location_.column += (end - start); diff --git a/src/reader/wgsl/lexer_test.cc b/src/reader/wgsl/lexer_test.cc index 24faeb397f..fbfea7ad3f 100644 --- a/src/reader/wgsl/lexer_test.cc +++ b/src/reader/wgsl/lexer_test.cc @@ -400,6 +400,28 @@ INSTANTIATE_TEST_SUITE_P( HexSignedIntData{"-0x80000000", std::numeric_limits::min()}, HexSignedIntData{"0x7FFFFFFF", std::numeric_limits::max()})); +TEST_F(LexerTest, HexPrefixOnly_IsError) { + // Could be the start of a hex integer or hex float, but is neither. + Source::FileContent content("0x"); + Lexer l("test.wgsl", &content); + + auto t = l.next(); + ASSERT_TRUE(t.Is(Token::Type::kError)); + EXPECT_EQ(t.to_str(), + "integer or float hex literal has no significant digits"); +} + +TEST_F(LexerTest, NegativeHexPrefixOnly_IsError) { + // Could be the start of a hex integer or hex float, but is neither. + Source::FileContent content("-0x"); + Lexer l("test.wgsl", &content); + + auto t = l.next(); + ASSERT_TRUE(t.Is(Token::Type::kError)); + EXPECT_EQ(t.to_str(), + "integer or float hex literal has no significant digits"); +} + TEST_F(LexerTest, IntegerTest_HexSignedTooLarge) { Source::FileContent content("0x80000000"); Lexer l("test.wgsl", &content);