Skip to content

Commit 18300d0

Browse files
committed
Add context to safe-init warnings for inlined methods
Closes #14467 - Adds the calling context of the inlined method to the stack trace for safe-init warnings For source code: ```Scala class InlineError { Assertion.failAssert(this) val v = 2; } object Assertion: transparent inline def failAssert(inline message: => Any): Unit = scala.runtime.Scala3RunTime.assertFailed(message) ``` Old warning: ``` [error] -- Error: /*******/dotty/compiler/src/dotty/tools/dotc/InlineError.scala:10:45 [error] 10 | scala.runtime.Scala3RunTime.assertFailed(message) [error] | ^^^^^^^ [error] |Cannot prove that the value is fully initialized. Only initialized values may be used as arguments. ``` New warning: ``` [error] -- Error: /*******/dotty/compiler/src/dotty/tools/dotc/InlineError.scala:10:45 [error] 10 | scala.runtime.Scala3RunTime.assertFailed(message) [error] | ^^^^^^^ [error] |Cannot prove that the value is fully initialized. Only initialized values may be used as arguments. Calling trace: [error] | -> Assertion.failAssert(this) [ InlineError.scala:4 ] ``` Review by @liufengyun
1 parent 808c669 commit 18300d0

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,8 +1239,9 @@ object Semantic {
12391239
Result(Hot, ress.flatMap(_.errors))
12401240

12411241
case Inlined(call, bindings, expansion) =>
1242+
val trace1 = trace.add(expr)
12421243
val ress = eval(bindings, thisV, klass)
1243-
eval(expansion, thisV, klass) ++ ress.flatMap(_.errors)
1244+
withTrace(trace1)(eval(expansion, thisV, klass)) ++ ress.flatMap(_.errors)
12441245

12451246
case Thicket(List()) =>
12461247
// possible in try/catch/finally, see tests/crash/i6914.scala

tests/init/neg/inlined-method.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Error: tests/init/neg/inlined-method.scala:8:45 ---------------------------------------------------------------------
2+
8 | scala.runtime.Scala3RunTime.assertFailed(message) // error
3+
| ^^^^^^^
4+
|Cannot prove that the value is fully initialized. Only initialized values may be used as arguments. Calling trace:
5+
| -> Assertion.failAssert(this) [ inlined-method.scala:2 ]

tests/init/neg/inlined-method.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class InlineError {
2+
Assertion.failAssert(this)
3+
val v = 2;
4+
}
5+
6+
object Assertion:
7+
transparent inline def failAssert(inline message: => Any): Unit =
8+
scala.runtime.Scala3RunTime.assertFailed(message) // error

0 commit comments

Comments
 (0)