From 6d09a0cda43734cd0eed84eafe35b386d5954d1e Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 16 Mar 2024 09:33:34 -0700 Subject: [PATCH 1/3] TokenizerSuite: add precise float/double boundary --- .../tests/tokenizers/TokenizerSuite.scala | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/tests/shared/src/test/scala/scala/meta/tests/tokenizers/TokenizerSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/tokenizers/TokenizerSuite.scala index 48e56d9e24..e0d3e2cb1d 100644 --- a/tests/shared/src/test/scala/scala/meta/tests/tokenizers/TokenizerSuite.scala +++ b/tests/shared/src/test/scala/scala/meta/tests/tokenizers/TokenizerSuite.scala @@ -1508,18 +1508,18 @@ class TokenizerSuite extends BaseTokenizerSuite { |""".stripMargin ), ( - "1.79e+308d", + "1.7976931348623157e+308d", """|BOF [0..0) - |Constant.Double(1.79e+308d) [0..10) - |EOF [10..10) + |Constant.Double(1.7976931348623157e+308d) [0..24) + |EOF [24..24) |""".stripMargin ), ( - "-1.79e+308d", + "-1.7976931348623157e+308d", """|BOF [0..0) |Ident(-) [0..1) - |Constant.Double(1.79e+308d) [1..11) - |EOF [11..11) + |Constant.Double(1.7976931348623157e+308d) [1..25) + |EOF [25..25) |""".stripMargin ), ( @@ -1597,28 +1597,41 @@ class TokenizerSuite extends BaseTokenizerSuite { |0b0123 | ^""".stripMargin ), + // these two are actually within range + ( + "3.4028235e38f", + """|:1: error: floating-point value out of range for Float + |3.4028235e38f + |^""".stripMargin + ), + ( + "-3.4028235e38f", + """|:1: error: floating-point value out of range for Float + |-3.4028235e38f + | ^""".stripMargin + ), ( - "3.41e+38f", + "3.4028236e38f", """|:1: error: floating-point value out of range for Float - |3.41e+38f + |3.4028236e38f |^""".stripMargin ), ( - "-3.41e+38f", + "-3.4028236e38f", """|:1: error: floating-point value out of range for Float - |-3.41e+38f + |-3.4028236e38f | ^""".stripMargin ), ( - "1.80e+308d", + "1.7976931348623158e+308d", """|:1: error: floating-point value out of range for Double - |1.80e+308d + |1.7976931348623158e+308d |^""".stripMargin ), ( - "-1.80e+308d", + "-1.7976931348623158e+308d", """|:1: error: floating-point value out of range for Double - |-1.80e+308d + |-1.7976931348623158e+308d | ^""".stripMargin ) ).foreach { case (code, error) => From 28856ff414fdf15f9914cd84097baf0023ef722d Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 16 Mar 2024 10:35:56 -0700 Subject: [PATCH 2/3] LitSuite: mirror literal tests from TokenizerSuite --- .../scala/meta/tests/parsers/LitSuite.scala | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/LitSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/LitSuite.scala index e19adbc7c3..f158c3774b 100644 --- a/tests/shared/src/test/scala/scala/meta/tests/parsers/LitSuite.scala +++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/LitSuite.scala @@ -374,4 +374,149 @@ class LitSuite extends ParseSuite { runTestAssert[Stat](code)(tree) } + Seq( + ("0d", 0d), + ("0.0", 0d), + ("00e0", 0d), + ("00e0f", 0f), + ("0xe1", 225), + ("0xd", 13), + ("0f", 0f), + ("0.0f", 0f), + ("0xf", 15), + ("0", 0), + ("0l", 0L), + ("0L", 0L), + ("0x80000000", -2147483648), + ("0x8000000000000000L", -9223372036854775808L), + ("3.40e+38f", 3.40e+38f), + ("-3.40e+38f", -3.40e+38f), + ("1.7976931348623157e+308d", Double.MaxValue), + ("-1.7976931348623157e+308d", Double.MinValue), + ("0b00101010", 42), + ("0B_0010_1010", 42), + ("0b_0010_1010L", 42L) + ).foreach { case (code, expected) => + test(s"numeric literal ok scala213: $code") { + parseStat(code, Scala213) match { + case lit: Lit => assertEquals(lit.value, expected) + } + } + } + + Seq( + ( + "0.f", + Term.Select(lit(0), tname("f")) + ), + ( + "1.0 + 2.0f", + Term.ApplyInfix(lit(1d), tname("+"), Nil, List(lit(2f))) + ), + ( + "1d + 2f", + Term.ApplyInfix(lit(1d), tname("+"), Nil, List(lit(2f))) + ), + ( + "0b01.toString", + Term.Select(lit(1), tname("toString")) + ) + ).foreach { case (code, tree: Tree) => + test(s"expr with numeric literal ok scala213: $code") { + runTestAssert[Stat](code, None)(tree) + } + } + + Seq( + ( + "00", + """|:1: error: Non-zero integral values may not have a leading zero. + |00 + |^""".stripMargin + ), + ( + "00l", + """|:1: error: Non-zero integral values may not have a leading zero. + |00l + |^""".stripMargin + ), + ( + "0b01d", + """|:1: error: Invalid literal number, followed by identifier character + |0b01d + | ^""".stripMargin + ), + ( + "0b01f", + """|:1: error: Invalid literal number, followed by identifier character + |0b01f + | ^""".stripMargin + ), + ( + "0b0123", + """|:1: error: Invalid literal number, followed by identifier character + |0b0123 + | ^""".stripMargin + ), + ( + "0b01.2", + """|:1: error: ; expected but double constant found + |0b01.2 + | ^""".stripMargin + ), + ( + "1. + 2.", + """|:1: error: ; expected but integer constant found + |1. + 2. + | ^""".stripMargin + ), + ( + ".f", + """|:1: error: illegal start of definition . + |.f + |^""".stripMargin + ), + // these two are actually within range + ( + "3.4028235e38f", + """|:1: error: floating-point value out of range for Float + |3.4028235e38f + |^""".stripMargin + ), + ( + "-3.4028235e38f", + """|:1: error: floating-point value out of range for Float + |-3.4028235e38f + | ^""".stripMargin + ), + ( + "3.4028236e38f", + """|:1: error: floating-point value out of range for Float + |3.4028236e38f + |^""".stripMargin + ), + ( + "-3.4028236e38f", + """|:1: error: floating-point value out of range for Float + |-3.4028236e38f + | ^""".stripMargin + ), + ( + "1.7976931348623158e+308d", + """|:1: error: floating-point value out of range for Double + |1.7976931348623158e+308d + |^""".stripMargin + ), + ( + "-1.7976931348623158e+308d", + """|:1: error: floating-point value out of range for Double + |-1.7976931348623158e+308d + | ^""".stripMargin + ) + ).foreach { case (code, error) => + test(s"numeric literal fail scala213: $code") { + runTestError[Stat](code, error) + } + } + } From 392138f9bdf0e2fc4ae3ac82f25d020b48d51091 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 16 Mar 2024 10:13:03 -0700 Subject: [PATCH 3/3] LegacyTokenData: relax float check, round up max --- .../internal/tokenizers/LegacyTokenData.scala | 10 ++++++-- .../scala/meta/tests/parsers/LitSuite.scala | 17 ++----------- .../tests/tokenizers/TokenizerSuite.scala | 25 +++++-------------- 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/scalameta/tokenizers/shared/src/main/scala/scala/meta/internal/tokenizers/LegacyTokenData.scala b/scalameta/tokenizers/shared/src/main/scala/scala/meta/internal/tokenizers/LegacyTokenData.scala index ef5905d231..6492c559d5 100644 --- a/scalameta/tokenizers/shared/src/main/scala/scala/meta/internal/tokenizers/LegacyTokenData.scala +++ b/scalameta/tokenizers/shared/src/main/scala/scala/meta/internal/tokenizers/LegacyTokenData.scala @@ -2,6 +2,9 @@ package scala.meta package internal package tokenizers +import java.math.MathContext +import java.math.RoundingMode + import scala.meta.inputs._ trait LegacyTokenData { @@ -114,6 +117,9 @@ trait LegacyTokenData { } object LegacyTokenData { - private val bigDecimalMaxFloat = BigDecimal(Float.MaxValue) - private val bigDecimalMaxDouble = BigDecimal(Double.MaxValue) + // add a bit more, JS doesn't handle it well + private val bigDecimalMaxFloat = + BigDecimal.binary(Float.MaxValue, new MathContext(8, RoundingMode.UP)) + private val bigDecimalMaxDouble = + BigDecimal.binary(Double.MaxValue, new MathContext(32, RoundingMode.UP)) } diff --git a/tests/shared/src/test/scala/scala/meta/tests/parsers/LitSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/parsers/LitSuite.scala index f158c3774b..7831566775 100644 --- a/tests/shared/src/test/scala/scala/meta/tests/parsers/LitSuite.scala +++ b/tests/shared/src/test/scala/scala/meta/tests/parsers/LitSuite.scala @@ -389,8 +389,8 @@ class LitSuite extends ParseSuite { ("0L", 0L), ("0x80000000", -2147483648), ("0x8000000000000000L", -9223372036854775808L), - ("3.40e+38f", 3.40e+38f), - ("-3.40e+38f", -3.40e+38f), + ("3.4028235e38f", Float.MaxValue), + ("-3.4028235e38f", Float.MinValue), ("1.7976931348623157e+308d", Double.MaxValue), ("-1.7976931348623157e+308d", Double.MinValue), ("0b00101010", 42), @@ -476,19 +476,6 @@ class LitSuite extends ParseSuite { |.f |^""".stripMargin ), - // these two are actually within range - ( - "3.4028235e38f", - """|:1: error: floating-point value out of range for Float - |3.4028235e38f - |^""".stripMargin - ), - ( - "-3.4028235e38f", - """|:1: error: floating-point value out of range for Float - |-3.4028235e38f - | ^""".stripMargin - ), ( "3.4028236e38f", """|:1: error: floating-point value out of range for Float diff --git a/tests/shared/src/test/scala/scala/meta/tests/tokenizers/TokenizerSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/tokenizers/TokenizerSuite.scala index e0d3e2cb1d..8d883a9963 100644 --- a/tests/shared/src/test/scala/scala/meta/tests/tokenizers/TokenizerSuite.scala +++ b/tests/shared/src/test/scala/scala/meta/tests/tokenizers/TokenizerSuite.scala @@ -1493,18 +1493,18 @@ class TokenizerSuite extends BaseTokenizerSuite { |""".stripMargin ), ( - "3.40e+38f", + "3.4028235e38f", """|BOF [0..0) - |Constant.Float(3.40e+38f) [0..9) - |EOF [9..9) + |Constant.Float(3.4028235e38f) [0..13) + |EOF [13..13) |""".stripMargin ), ( - "-3.40e+38f", + "-3.4028235e38f", """|BOF [0..0) |Ident(-) [0..1) - |Constant.Float(3.40e+38f) [1..10) - |EOF [10..10) + |Constant.Float(3.4028235e38f) [1..14) + |EOF [14..14) |""".stripMargin ), ( @@ -1597,19 +1597,6 @@ class TokenizerSuite extends BaseTokenizerSuite { |0b0123 | ^""".stripMargin ), - // these two are actually within range - ( - "3.4028235e38f", - """|:1: error: floating-point value out of range for Float - |3.4028235e38f - |^""".stripMargin - ), - ( - "-3.4028235e38f", - """|:1: error: floating-point value out of range for Float - |-3.4028235e38f - | ^""".stripMargin - ), ( "3.4028236e38f", """|:1: error: floating-point value out of range for Float