Skip to content

Commit bc07eec

Browse files
authored
Merge pull request #7441 from bishabosha/fix_separators
Prevent numeric separator after leading zero
2 parents 0fb7efc + e818ce3 commit bc07eec

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ object Scanners {
695695
* to let it be base-10 because of history. Should it be an error? Is
696696
* there a realistic situation where one would need it?
697697
*/
698-
if (isDigit(ch))
698+
if (isDigit(ch) || (isNumberSeparator(ch) && isDigit(lookaheadChar())))
699699
error("Non-zero numbers may not have a leading zero.")
700700
base = 10
701701
}

docs/docs/internals/syntax.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,15 @@ idrest ::= {letter | digit} [‘_’ op]
4747
quoteId ::= ‘'’ alphaid
4848
4949
integerLiteral ::= (decimalNumeral | hexNumeral) [‘L’ | ‘l’]
50-
decimalNumeral ::= ‘0’ | nonZeroDigit {digit}
51-
hexNumeral ::= ‘0’ (‘x’ | ‘X’) hexDigit {hexDigit}
52-
digit ::= ‘0’ | nonZeroDigit
50+
decimalNumeral ::= ‘0’ | nonZeroDigit [{digit | ‘_’} digit]
51+
hexNumeral ::= ‘0’ (‘x’ | ‘X’) hexDigit [{hexDigit | ‘_’} hexDigit]
5352
nonZeroDigit ::= ‘1’ | … | ‘9’
5453
5554
floatingPointLiteral
56-
::= digit {digit} ‘.’ {digit} [exponentPart] [floatType]
57-
| ‘.’ digit {digit} [exponentPart] [floatType]
58-
| digit {digit} exponentPart [floatType]
59-
| digit {digit} [exponentPart] floatType
60-
exponentPart ::= (‘E’ | ‘e’) [‘+’ | ‘-’] digit {digit}
55+
::= [decimalNumeral] ‘.’ digit [{digit | ‘_’} digit] [exponentPart] [floatType]
56+
| decimalNumeral exponentPart [floatType]
57+
| decimalNumeral floatType
58+
exponentPart ::= (‘E’ | ‘e’) [‘+’ | ‘-’] digit [{digit | ‘_’} digit]
6159
floatType ::= ‘F’ | ‘f’ | ‘D’ | ‘d’
6260
6361
booleanLiteral ::= ‘true’ | ‘false’

tests/neg/t6124.check

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@
5454
26 | val x8 = 0x52_ // error
5555
| ^
5656
| trailing separator is not allowed
57+
-- Error: tests/neg/t6124.scala:27:11 ----------------------------------------------------------------------------------
58+
27 | val x9 = 0_52 // error
59+
| ^
60+
| Non-zero numbers may not have a leading zero.
61+
-- Error: tests/neg/t6124.scala:28:12 ----------------------------------------------------------------------------------
62+
28 | val x10 = 052 // error
63+
| ^
64+
| Non-zero numbers may not have a leading zero.
65+
-- Error: tests/neg/t6124.scala:29:12 ----------------------------------------------------------------------------------
66+
29 | val x11 = 0_0.52 // error
67+
| ^
68+
| Non-zero numbers may not have a leading zero.
69+
-- Error: tests/neg/t6124.scala:30:12 ----------------------------------------------------------------------------------
70+
30 | val x12 = 00.52 // error
71+
| ^
72+
| Non-zero numbers may not have a leading zero.
5773
-- Error: tests/neg/t6124.scala:12:17 ----------------------------------------------------------------------------------
5874
12 | def tooSmall = 1.0E-325 // error
5975
| ^^^^^^^^

tests/neg/t6124.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ trait T {
2424
val x5 = 0_x52 // error
2525
val x6 = 0x_52 // error
2626
val x8 = 0x52_ // error
27+
val x9 = 0_52 // error
28+
val x10 = 052 // error
29+
val x11 = 0_0.52 // error
30+
val x12 = 00.52 // error
2731

2832
def z = 0
29-
}
33+
}

0 commit comments

Comments
 (0)