Skip to content

Commit 512115e

Browse files
committed
Fix #2334: Require at least one digit after '.' in floating point literals
1 parent 16a28ac commit 512115e

File tree

3 files changed

+18
-33
lines changed

3 files changed

+18
-33
lines changed

compiler/src/dotty/tools/dotc/parsing/CharArrayReader.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ abstract class CharArrayReader { self =>
120120
def isAtEnd = charOffset >= buf.length
121121

122122
/** A new reader that takes off at the current character position */
123-
def lookaheadReader = new CharArrayLookaheadReader
123+
def lookaheadReader() = new CharArrayLookaheadReader
124124

125125
class CharArrayLookaheadReader extends CharArrayReader {
126126
val buf = self.buf

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ object Scanners {
179179
private[this] var docstringMap: SortedMap[Int, Comment] = SortedMap.empty
180180

181181
private[this] def addComment(comment: Comment): Unit = {
182-
val lookahead = lookaheadReader
182+
val lookahead = lookaheadReader()
183183
def nextPos: Int = (lookahead.getc(): @switch) match {
184184
case ' ' | '\t' => nextPos
185185
case CR | LF | FF =>
@@ -861,7 +861,7 @@ object Scanners {
861861
nextChar()
862862
}
863863
if (ch == 'e' || ch == 'E') {
864-
val lookahead = lookaheadReader
864+
val lookahead = lookaheadReader()
865865
lookahead.nextChar()
866866
if (lookahead.ch == '+' || lookahead.ch == '-') {
867867
lookahead.nextChar()
@@ -905,36 +905,10 @@ object Scanners {
905905
}
906906
token = INTLIT
907907
if (base == 10 && ch == '.') {
908-
val isDefinitelyNumber = {
909-
val lookahead = lookaheadReader
910-
val c = lookahead.getc()
911-
(c: @switch) match {
912-
/** Another digit is a giveaway. */
913-
case '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' =>
914-
true
915-
916-
/** Backquoted idents like 22.`foo`. */
917-
case '`' =>
918-
false
919-
920-
/** These letters may be part of a literal, or a method invocation on an Int.
921-
*/
922-
case 'd' | 'D' | 'f' | 'F' =>
923-
!isIdentifierPart(lookahead.getc())
924-
925-
/** A little more special handling for e.g. 5e7 */
926-
case 'e' | 'E' =>
927-
val ch = lookahead.getc()
928-
!isIdentifierPart(ch) || (isDigit(ch) || ch == '+' || ch == '-')
929-
930-
case x =>
931-
!isIdentifierStart(x)
932-
}
933-
}
934-
if (isDefinitelyNumber) {
935-
putChar(ch)
936-
nextChar()
937-
getFraction()
908+
val lookahead = lookaheadReader()
909+
lookahead.nextChar()
910+
if ('0' <= lookahead.ch && lookahead.ch <= '9') {
911+
putChar('.'); nextChar(); getFraction()
938912
}
939913
} else (ch: @switch) match {
940914
case 'e' | 'E' | 'f' | 'F' | 'd' | 'D' =>

tests/neg/floatlits.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Test {
2+
3+
val y = .2 // ok
4+
5+
val z = 3.2 // ok
6+
7+
val a = 1.0e2
8+
val b = 1e-3
9+
10+
val x = 2. // error: identifier expected
11+
}

0 commit comments

Comments
 (0)