Skip to content

Commit 0803aff

Browse files
committed
Fix #8362: Fail compilation if a compile time error can't be inlined because of a non-literal string parameter
1 parent 04d5068 commit 0803aff

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -559,19 +559,19 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
559559

560560
def issueError() = callValueArgss match {
561561
case (msgArg :: Nil) :: Nil =>
562-
msgArg.tpe match {
563-
case ConstantType(Constant(msg: String)) =>
564-
// Usually `error` is called from within a rewrite method. In this
565-
// case we need to report the error at the point of the outermost enclosing inline
566-
// call. This way, a defensively written rewrite methid can always
567-
// report bad inputs at the point of call instead of revealing its internals.
568-
val callToReport = if (enclosingInlineds.nonEmpty) enclosingInlineds.last else call
569-
val ctxToReport = ctx.outersIterator.dropWhile(enclosingInlineds(_).nonEmpty).next
570-
def issueInCtx(implicit ctx: Context) =
571-
ctx.error(msg, callToReport.sourcePos)
572-
issueInCtx(ctxToReport)
573-
case _ =>
562+
val message = msgArg.tpe match {
563+
case ConstantType(Constant(msg: String)) => msg
564+
case _ => s"A literal string is expected as an argument to `compiletime.error`. Got ${msgArg.show}"
574565
}
566+
// Usually `error` is called from within a rewrite method. In this
567+
// case we need to report the error at the point of the outermost enclosing inline
568+
// call. This way, a defensively written rewrite methid can always
569+
// report bad inputs at the point of call instead of revealing its internals.
570+
val callToReport = if (enclosingInlineds.nonEmpty) enclosingInlineds.last else call
571+
val ctxToReport = ctx.outersIterator.dropWhile(enclosingInlineds(_).nonEmpty).next
572+
def issueInCtx(implicit ctx: Context) =
573+
ctx.error(message, callToReport.sourcePos)
574+
issueInCtx(ctxToReport)
575575
case _ =>
576576
}
577577

@@ -1316,4 +1316,3 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
13161316
}
13171317
}.apply(Nil, tree)
13181318
}
1319-

tests/neg/i8362.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
inline def foo: String = scala.compiletime.error(s"")
3+
4+
def main(args: Array[String]): Unit = println(foo) // error
5+
}

0 commit comments

Comments
 (0)