Skip to content

Commit 93a44dc

Browse files
committed
more friendly error messages for FromDigits summoners
1 parent 1c59175 commit 93a44dc

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

library/src/scala/util/FromDigits.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ object FromDigits {
5050
inline def fromDigits[T](given x: FromDigits[T]): x.type = x
5151

5252
inline def fromRadixDigits[T](given x: FromDigits[T]): x.type =
53-
${summonDigitsImpl[x.type, FromDigits.WithRadix[T]]('x)}
53+
${summonDigitsImpl[x.type, T, FromDigits.WithRadix[T]]('x, "whole numbers with radix other than 10")}
5454

5555
inline def fromDecimalDigits[T](given x: FromDigits[T]): x.type =
56-
${summonDigitsImpl[x.type, FromDigits.Decimal[T]]('x)}
56+
${summonDigitsImpl[x.type, T, FromDigits.Decimal[T]]('x, "numbers with a decimal point")}
5757

5858
inline def fromFloatingDigits[T](given x: FromDigits[T]): x.type =
59-
${summonDigitsImpl[x.type, FromDigits.Floating[T]]('x)}
59+
${summonDigitsImpl[x.type, T, FromDigits.Floating[T]]('x, "floating-point numbers")}
6060

61-
private def summonDigitsImpl[T <: FromDigits[_], U <: FromDigits[_]](x: Expr[T])(given
62-
qctx: QuoteContext, t: Type[T], u: Type[U]): Expr[T] = {
63-
import qctx.tasty.{Type => _, _, given}
64-
def makeError = s"""|FromDigits instance is incompatible with the expected numeric kind.
61+
private def summonDigitsImpl[T <: FromDigits[_], O, U <: FromDigits[O]](x: Expr[T], descriptor: String)(given
62+
qctx: QuoteContext, t: Type[T], o: Type[O], u: Type[U]): Expr[T] = {
63+
import qctx.tasty.{_, given}
64+
def makeError = s"""|Type ${o.show} does not have a FromDigits instance for $descriptor.
6565
| Found: ${t.show}(${x.show})
6666
| Expected: ${u.show}""".stripMargin
6767
if typeOf[T] <:< typeOf[U] then
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
-- Error: tests/neg-macros/generic-num-lits.scala:2:8 ------------------------------------------------------------------
22
2 |val b = 0xcafebabe: BigDecimal // error
33
| ^
4-
| FromDigits instance is incompatible with the expected numeric kind.
4+
| Type scala.math.BigDecimal does not have a FromDigits instance for whole numbers with radix other than 10.
55
| Found: scala.util.FromDigits.BigDecimalFromDigits.type(scala.util.FromDigits.BigDecimalFromDigits)
66
| Expected: scala.util.FromDigits.WithRadix[scala.math.BigDecimal]
77
| This location is in code that was inlined at generic-num-lits.scala:2
88
-- Error: tests/neg-macros/generic-num-lits.scala:3:8 ------------------------------------------------------------------
99
3 |val c = 1.3: BigInt // error
1010
| ^
11-
| FromDigits instance is incompatible with the expected numeric kind.
11+
| Type scala.math.BigInt does not have a FromDigits instance for numbers with a decimal point.
1212
| Found: scala.util.FromDigits.BigIntFromDigits.type(scala.util.FromDigits.BigIntFromDigits)
1313
| Expected: scala.util.FromDigits.Decimal[scala.math.BigInt]
1414
| This location is in code that was inlined at generic-num-lits.scala:3
1515
-- Error: tests/neg-macros/generic-num-lits.scala:4:8 ------------------------------------------------------------------
1616
4 |val d = 2e500: BigInt // error
1717
| ^
18-
| FromDigits instance is incompatible with the expected numeric kind.
18+
| Type scala.math.BigInt does not have a FromDigits instance for floating-point numbers.
1919
| Found: scala.util.FromDigits.BigIntFromDigits.type(scala.util.FromDigits.BigIntFromDigits)
2020
| Expected: scala.util.FromDigits.Floating[scala.math.BigInt]
2121
| This location is in code that was inlined at generic-num-lits.scala:4
2222
-- Error: tests/neg-macros/generic-num-lits.scala:7:7 ------------------------------------------------------------------
2323
7 | case 0xcafebabe: BigDecimal => // error
2424
| ^
25-
| FromDigits instance is incompatible with the expected numeric kind.
25+
| Type scala.math.BigDecimal does not have a FromDigits instance for whole numbers with radix other than 10.
2626
| Found: scala.util.FromDigits.BigDecimalFromDigits.type(scala.util.FromDigits.BigDecimalFromDigits)
2727
| Expected: scala.util.FromDigits.WithRadix[scala.math.BigDecimal]
2828
| This location is in code that was inlined at generic-num-lits.scala:7
2929
-- Error: tests/neg-macros/generic-num-lits.scala:11:7 -----------------------------------------------------------------
3030
11 | case 1.3: BigInt => // error
3131
| ^
32-
| FromDigits instance is incompatible with the expected numeric kind.
32+
| Type scala.math.BigInt does not have a FromDigits instance for numbers with a decimal point.
3333
| Found: scala.util.FromDigits.BigIntFromDigits.type(scala.util.FromDigits.BigIntFromDigits)
3434
| Expected: scala.util.FromDigits.Decimal[scala.math.BigInt]
3535
| This location is in code that was inlined at generic-num-lits.scala:11
3636
-- Error: tests/neg-macros/generic-num-lits.scala:15:7 -----------------------------------------------------------------
3737
15 | case 2e500: BigInt => // error
3838
| ^
39-
| FromDigits instance is incompatible with the expected numeric kind.
39+
| Type scala.math.BigInt does not have a FromDigits instance for floating-point numbers.
4040
| Found: scala.util.FromDigits.BigIntFromDigits.type(scala.util.FromDigits.BigIntFromDigits)
4141
| Expected: scala.util.FromDigits.Floating[scala.math.BigInt]
4242
| This location is in code that was inlined at generic-num-lits.scala:15

0 commit comments

Comments
 (0)