diff --git a/Changelog.md b/Changelog.md index 82a16fe0d..e45646c91 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,7 @@ Breaking changes: * Constructors should not have visibility. * Deprecated dot syntax for `value` and `gas`. * Deprecated the identifier `now`. + * Disallow `gwei` as identifier. * JSON AST: Removes members with ``null`` value from JSON output. * Parser: NatSpec comments on variables are only allowed for public state variables. * Type Checker: Disallow shifts by signed types. diff --git a/liblangutil/Token.h b/liblangutil/Token.h index d887e3901..cea102f0f 100644 --- a/liblangutil/Token.h +++ b/liblangutil/Token.h @@ -196,6 +196,7 @@ namespace solidity::langutil \ /* Ether subdenominations */ \ K(SubWei, "wei", 0) \ + K(SubGwei, "gwei", 0) \ K(SubEther, "ether", 0) \ K(SubSecond, "seconds", 0) \ K(SubMinute, "minutes", 0) \ @@ -230,7 +231,6 @@ namespace solidity::langutil \ /* Identifiers (not keywords or future reserved words). */ \ T(Identifier, nullptr, 0) \ - T(SubGwei, "gwei", 0) \ \ /* Keywords reserved for future use. */ \ K(After, "after", 0) \ @@ -311,7 +311,7 @@ namespace TokenTraits || op == Token::Pure || op == Token::View || op == Token::Payable; } - constexpr bool isEtherSubdenomination(Token op) { return op == Token::SubWei || op == Token::SubEther; } + constexpr bool isEtherSubdenomination(Token op) { return op >= Token::SubWei && op <= Token::SubEther; } constexpr bool isTimeSubdenomination(Token op) { return op == Token::SubSecond || op == Token::SubMinute || op == Token::SubHour || op == Token::SubDay || op == Token::SubWeek || op == Token::SubYear; } constexpr bool isReservedKeyword(Token op) { return (Token::After <= op && op <= Token::Unchecked); } diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 819d18a79..ab8180dc7 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -1825,16 +1825,11 @@ ASTPointer Parser::parsePrimaryExpression() expression = nodeFactory.createNode(token, getLiteralAndAdvance()); break; case Token::Number: - if ( - (m_scanner->peekNextToken() == Token::Identifier && m_scanner->peekLiteral() == "gwei") || - TokenTraits::isEtherSubdenomination(m_scanner->peekNextToken()) - ) + if (TokenTraits::isEtherSubdenomination(m_scanner->peekNextToken())) { ASTPointer literal = getLiteralAndAdvance(); nodeFactory.markEndPosition(); - Token actualToken = m_scanner->currentToken() == Token::Identifier ? Token::SubGwei : m_scanner->currentToken(); - - Literal::SubDenomination subdenomination = static_cast(actualToken); + Literal::SubDenomination subdenomination = static_cast(m_scanner->currentToken()); m_scanner->next(); expression = nodeFactory.createNode(token, literal, subdenomination); } diff --git a/test/libsolidity/SolidityScanner.cpp b/test/libsolidity/SolidityScanner.cpp index 93e44a5bf..5b83778ba 100644 --- a/test/libsolidity/SolidityScanner.cpp +++ b/test/libsolidity/SolidityScanner.cpp @@ -430,7 +430,7 @@ BOOST_AUTO_TEST_CASE(ether_subdenominations) { Scanner scanner(CharStream("wei gwei ether", "")); BOOST_CHECK_EQUAL(scanner.currentToken(), Token::SubWei); - BOOST_CHECK_EQUAL(scanner.next(), Token::Identifier); + BOOST_CHECK_EQUAL(scanner.next(), Token::SubGwei); BOOST_CHECK_EQUAL(scanner.next(), Token::SubEther); } diff --git a/test/libsolidity/semanticTests/literals/gwei.sol b/test/libsolidity/semanticTests/literals/gwei.sol index 7762021dc..54870040f 100644 --- a/test/libsolidity/semanticTests/literals/gwei.sol +++ b/test/libsolidity/semanticTests/literals/gwei.sol @@ -1,7 +1,7 @@ contract C { - uint constant gwei = 1 gwei; + uint constant x = 1 gwei; - function f() public view returns(uint) { return gwei; } + function f() public view returns(uint) { return x; } } // ==== // compileViaYul: also diff --git a/test/libsolidity/syntaxTests/denominations/gwei.sol b/test/libsolidity/syntaxTests/denominations/gwei.sol deleted file mode 100644 index 5bc5edb3d..000000000 --- a/test/libsolidity/syntaxTests/denominations/gwei.sol +++ /dev/null @@ -1,3 +0,0 @@ -contract C { - uint constant gwei = 1 gwei; -} diff --git a/test/libsolidity/syntaxTests/denominations/gwei_as_identifier.sol b/test/libsolidity/syntaxTests/denominations/gwei_as_identifier.sol new file mode 100644 index 000000000..43fde5ed7 --- /dev/null +++ b/test/libsolidity/syntaxTests/denominations/gwei_as_identifier.sol @@ -0,0 +1,5 @@ +contract C { + uint constant gwei = 1; +} +// ---- +// ParserError 2314: (28-32): Expected identifier but got 'gwei'