Skip to content

Commit 2d76382

Browse files
committed
Allow additional arguments for typelevel.error
1 parent 71fbc15 commit 2d76382

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
447447
}
448448

449449
def issueError() = callValueArgss match {
450-
case (msgArg :: Nil) :: Nil =>
450+
case (msgArg :: rest) :: Nil =>
451451
msgArg.tpe match {
452452
case ConstantType(Constant(msg: String)) =>
453453
// Usually `error` is called from within a rewrite method. In this
@@ -456,7 +456,18 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
456456
// report bad inputs at the point of call instead of revealing its internals.
457457
val callToReport = if (enclosingInlineds.nonEmpty) enclosingInlineds.last else call
458458
val ctxToReport = ctx.outersIterator.dropWhile(enclosingInlineds(_).nonEmpty).next
459-
def issueInCtx(implicit ctx: Context) = ctx.error(msg, callToReport.pos)
459+
def issueInCtx(implicit ctx: Context) = {
460+
def decompose(arg: Tree): String = arg match {
461+
case Typed(arg, _) => decompose(arg)
462+
case SeqLiteral(elems, _) => elems.map(decompose).mkString(", ")
463+
case arg =>
464+
arg.tpe.widenTermRefExpr match {
465+
case ConstantType(Constant(c)) => c.toString
466+
case _ => arg.show
467+
}
468+
}
469+
ctx.error(s"$msg${rest.map(decompose).mkString(", ")}", callToReport.pos)
470+
}
460471
issueInCtx(ctxToReport)
461472
case _ =>
462473
}

library/src-scala3/scala/typelevel/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package object typelevel {
66

77
case class Typed[T](val value: T) { type Type = T }
88

9-
rewrite def error(transparent msg: String): Nothing = ???
9+
rewrite def error(transparent msg: String, objs: Any*): Nothing = ???
1010

1111
rewrite def constValueOpt[T]: Option[T] = ???
1212

tests/neg/tuple-oob1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
object Test {
2-
def elem(xs: (Int, String)) = xs(2) // error: index out of bounds
2+
def elem(xs: (Int, String)) = xs(2) // error: index out of bounds: 2
33
}

0 commit comments

Comments
 (0)