Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Disallow trailing dot not followed by number #4172

Merged
merged 1 commit into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Breaking Changes:
* Commandline interface: Require ``-`` if standard input is used as source.
* General: ``continue`` in a ``do...while`` loop jumps to the condition (it used to jump to the loop body). Warning: this may silently change the semantics of existing code.
* Type Checker: Disallow arithmetic operations for Boolean variables.
* Disallow trailing dots that are not followed by a number.

Language Features:
* General: Allow appending ``calldata`` keyword to types, to explicitly specify data location for arguments of external functions.
Expand Down
8 changes: 7 additions & 1 deletion libsolidity/parsing/Scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,8 +768,14 @@ Token::Value Scanner::scanNumber(char _charSeen)
scanDecimalDigits(); // optional
if (m_char == '.')
{
// A '.' has to be followed by a number.
if (m_source.isPastEndOfInput() || !isDecimalDigit(m_source.get(1)))
{
literal.complete();
return Token::Number;
}
addLiteralCharAndAdvance();
scanDecimalDigits(); // optional
scanDecimalDigits();
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions test/libsolidity/SolidityScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,26 @@ BOOST_AUTO_TEST_CASE(scientific_notation)
BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
}

BOOST_AUTO_TEST_CASE(trailing_dot)
{
Scanner scanner(CharStream("2.5"));
BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
scanner.reset(CharStream("2.5e10"), "");
BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
scanner.reset(CharStream(".5"), "");
BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
scanner.reset(CharStream(".5e10"), "");
BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
scanner.reset(CharStream("2."), "");
BOOST_CHECK_EQUAL(scanner.currentToken(), Token::Number);
BOOST_CHECK_EQUAL(scanner.next(), Token::Period);
BOOST_CHECK_EQUAL(scanner.next(), Token::EOS);
}

BOOST_AUTO_TEST_CASE(negative_numbers)
{
Scanner scanner(CharStream("var x = -.2 + -0x78 + -7.3 + 8.9 + 2e-2;"));
Expand Down
7 changes: 7 additions & 0 deletions test/libsolidity/syntaxTests/parsing/trailing_dot1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract test {
uint256 a = 2.2e10;
uint256 b = .5E10;
uint256 c = 4.e-2;
}
// ----
// TypeError: (70-73): Member "e" not found or not visible after argument-dependent lookup in int_const 4
7 changes: 7 additions & 0 deletions test/libsolidity/syntaxTests/parsing/trailing_dot2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract test {
uint256 a = 2.2e10;
uint256 b = .5E10;
uint256 c = 2 + 2.;
}
// ----
// ParserError: (76-77): Expected identifier but got ';'
4 changes: 4 additions & 0 deletions test/libsolidity/syntaxTests/parsing/trailing_dot3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
contract test {
uint a = 2.
// ----
// ParserError: (29-29): Expected identifier but got end of source