Skip to content

Commit e8860ee

Browse files
committed
Fix #2334: Require at least one digit after '.' in floating point literals
1 parent f338ffa commit e8860ee

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
@@ -180,7 +180,7 @@ object Scanners {
180180
private[this] var docstringMap: SortedMap[Int, Comment] = SortedMap.empty
181181

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