Skip to content

Commit

Permalink
Merge pull request #3601 from kitbellew/sbt299_15
Browse files Browse the repository at this point in the history
Lit.{Int,Long}: handle `0b` exactly like `0x`
  • Loading branch information
kitbellew committed Feb 29, 2024
2 parents daf7713 + b0806be commit 48a0221
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1322,25 +1322,25 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>

def literal(isNegated: Boolean = false): Lit = {
val startPos = if (isNegated) prevTokenPos else tokenPos
def isHex = {
val syntax = token.syntax
syntax.startsWith("0x") || syntax.startsWith("0X")
def getBigInt(tok: NumericConstant[BigInt], dec: BigInt, hex: BigInt, typ: String) = {
// decimal never starts with `0` as octal was removed in 2.11; "hex" includes `0x` or `0b`
// non-decimal literals allow signed overflow within unsigned range
val max = if (tok.text(0) != '0') dec else hex
// token value is always positive as it doesn't take into account a sign
val value = tok.value
if (isNegated) {
if (value > max) syntaxError(s"integer number too small for $typ", at = token)
-value
} else {
if (value >= max) syntaxError(s"integer number too large for $typ", at = token)
value
}
}
val res = token match {
case Constant.Int(rawValue) =>
val value = if (isNegated) -rawValue else rawValue
val min = if (isHex) BigInt(Int.MinValue) * 2 + 1 else BigInt(Int.MinValue)
val max = if (isHex) BigInt(Int.MaxValue) * 2 + 1 else BigInt(Int.MaxValue)
if (value > max) syntaxError("integer number too large", at = token)
else if (value < min) syntaxError("integer number too small", at = token)
Lit.Int(value.toInt)
case Constant.Long(rawValue) =>
val value = if (isNegated) -rawValue else rawValue
val min = if (isHex) BigInt(Long.MinValue) * 2 + 1 else BigInt(Long.MinValue)
val max = if (isHex) BigInt(Long.MaxValue) * 2 + 1 else BigInt(Long.MaxValue)
if (value > max) syntaxError("integer number too large", at = token)
else if (value < min) syntaxError("integer number too small", at = token)
Lit.Long(value.toLong)
case tok: Constant.Int =>
Lit.Int(getBigInt(tok, bigIntMaxInt, bigIntMaxUInt, "Int").intValue)
case tok: Constant.Long =>
Lit.Long(getBigInt(tok, bigIntMaxLong, bigIntMaxULong, "Long").longValue)
case Constant.Float(rawValue) =>
Lit.Float(if (isNegated) -rawValue else rawValue)
case Constant.Double(rawValue) =>
Expand Down Expand Up @@ -4843,6 +4843,12 @@ object ScalametaParser {
case v => f(v)
}

private val bigIntMaxInt = BigInt(Int.MaxValue) + 1
private val bigIntMaxUInt = bigIntMaxInt << 1

private val bigIntMaxLong = BigInt(Long.MaxValue) + 1
private val bigIntMaxULong = bigIntMaxLong << 1

}

class Location private (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scala.meta.tests
package parsers

import scala.meta._
import scala.meta.dialects.Scala211
import scala.meta.dialects.Scala213

class LitSuite extends ParseSuite {

Expand All @@ -19,7 +19,11 @@ class LitSuite extends ParseSuite {
}

test("2147483648") {
intercept[ParseException] { term("2147483648") }
interceptMessage[ParseException](
"""|<input>:1: error: integer number too large for Int
|2147483648
|^""".stripMargin.replace("\n", EOL)
) { term("2147483648") }
}

test("2147483647") {
Expand All @@ -31,7 +35,11 @@ class LitSuite extends ParseSuite {
}

test("-2147483649") {
intercept[ParseException] { term("-2147483649") }
interceptMessage[ParseException](
"""|<input>:1: error: integer number too small for Int
|-2147483649
| ^""".stripMargin.replace("\n", EOL)
) { term("-2147483649") }
}

test("42L") {
Expand All @@ -43,7 +51,11 @@ class LitSuite extends ParseSuite {
}

test("9223372036854775808L") {
intercept[ParseException] { term("9223372036854775808L") }
interceptMessage[ParseException](
"""|<input>:1: error: integer number too large for Long
|9223372036854775808L
|^""".stripMargin.replace("\n", EOL)
) { term("9223372036854775808L") }
}

test("9223372036854775807L") {
Expand All @@ -55,7 +67,11 @@ class LitSuite extends ParseSuite {
}

test("-9223372036854775809L") {
intercept[ParseException] { term("-9223372036854775809L") }
interceptMessage[ParseException](
"""|<input>:1: error: integer number too small for Long
|-9223372036854775809L
| ^""".stripMargin.replace("\n", EOL)
) { term("-9223372036854775809L") }
}

test("42.42") {
Expand Down Expand Up @@ -189,7 +205,6 @@ class LitSuite extends ParseSuite {
}

test("numeric literals with separators") {
implicit val Scala211 = scala.meta.dialects.Scala213
runTestAssert[Stat]("1_000_000_000.0", "1000000000.0d")(dbl("1000000000"))
runTestAssert[Stat]("1_000_000_000d", "1000000000d")(dbl("1000000000"))
runTestAssert[Stat]("1_000_000_000D", "1000000000d")(dbl("1000000000"))
Expand All @@ -199,4 +214,60 @@ class LitSuite extends ParseSuite {
runTestAssert[Stat]("1_000_000_000L", "1000000000L")(lit(1000000000L))
}

// non-decimal, signed overflow within unsigned range

test("0xffffffff") {
assertTree(term("0xffffffff"))(Lit.Int(-1))
assertTree(term("-0xffffffff"))(Lit.Int(1))
}

test("0xffffffff0") {
interceptMessage[ParseException](
"""|<input>:1: error: integer number too large for Int
|0xffffffff0
|^""".stripMargin.replace("\n", EOL)
)(term("0xffffffff0"))
interceptMessage[ParseException](
"""|<input>:1: error: integer number too small for Int
|-0xffffffff0
| ^""".stripMargin.replace("\n", EOL)
)(term("-0xffffffff0"))
}

test("0b11111111111111111111111111111111") { // 32
assertTree(term("0b11111111111111111111111111111111"))(Lit.Int(-1))
assertTree(term("-0b11111111111111111111111111111111"))(Lit.Int(1))
}

test("0b111111111111111111111111111111110") { // 33
interceptMessage[ParseException](
"""|<input>:1: error: integer number too large for Int
|0b111111111111111111111111111111110
|^""".stripMargin.replace("\n", EOL)
)(term("0b111111111111111111111111111111110"))
interceptMessage[ParseException](
"""|<input>:1: error: integer number too small for Int
|-0b111111111111111111111111111111110
| ^""".stripMargin.replace("\n", EOL)
)(term("-0b111111111111111111111111111111110"))
}

test("0xffffffffffffffffL") {
assertTree(term("0xffffffffffffffffL"))(Lit.Long(-1L))
assertTree(term("-0xffffffffffffffffL"))(Lit.Long(1L))
}

test("0xffffffffffffffff0L") {
interceptMessage[ParseException](
"""|<input>:1: error: integer number too large for Long
|0xffffffffffffffff0L
|^""".stripMargin.replace("\n", EOL)
)(term("0xffffffffffffffff0L"))
interceptMessage[ParseException](
"""|<input>:1: error: integer number too small for Long
|-0xffffffffffffffff0L
| ^""".stripMargin.replace("\n", EOL)
)(term("-0xffffffffffffffff0L"))
}

}

0 comments on commit 48a0221

Please sign in to comment.