Skip to content

Commit bb1e2e7

Browse files
committed
Show which argument is non-hot
1 parent 615da00 commit bb1e2e7

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,22 @@ object Errors:
101101
error.show
102102

103103
/** Unsafe leaking a non-hot value as constructor arguments */
104-
case class UnsafeLeaking(trace: Seq[Tree], error: Error) extends Error:
104+
case class UnsafeLeaking(trace: Seq[Tree], error: Error, argsIndices: List[Int]) extends Error:
105105
def show(using Context): String =
106-
"Problematic object instantiation with uninitialized arguments." + stacktrace() + "\n" +
106+
"Problematic object instantiation: " + argumentInfo() + stacktrace() + "\n" +
107107
"It leads to the following error during object initialization:\n" +
108108
error.show
109+
110+
private def argumentInfo(): String =
111+
val multiple = argsIndices.size > 1
112+
val part1 =
113+
argsIndices.zipWithIndex.foldLeft("") { case (acc, (pos, i)) =>
114+
val text1 = if pos == 0 then "the outer" else "arg " + pos.toString
115+
val text2 =
116+
if i == argsIndices.size - 2 then text1 + " and "
117+
else if i == argsIndices.size - 1 then text1
118+
else text1 + ", "
119+
acc + text2
120+
}
121+
val part2 = if multiple then " are not fully initialized." else " is not fully initialized."
122+
part1 + part2

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,15 @@ object Semantic:
784784
warm.callConstructor(ctor, argInfos2)
785785
}
786786
if errors.nonEmpty then
787-
val error = UnsafeLeaking(trace.toVector, errors.head)
787+
val indices =
788+
for
789+
(arg, i) <- argValues.zipWithIndex
790+
if arg.isCold
791+
yield
792+
i + 1
793+
794+
val indices2 = if warm.outer.isHot then indices else 0 :: indices
795+
val error = UnsafeLeaking(trace.toVector, errors.head, indices2)
788796
reporter.report(error)
789797
Hot
790798
else

tests/init/neg/cycle-structure.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/init/neg/cycle-structure.scala:9:13 --------------------------------------------------------------------
22
9 | val x = A(this) // error
33
| ^^^^^^^
4-
| Problematic object instantiation with uninitialized arguments. Calling trace:
4+
| Problematic object instantiation: arg 1 is not fully initialized. Calling trace:
55
| -> case class B(a: A) { [ cycle-structure.scala:7 ]
66
| ^
77
| -> val x = A(this) // error [ cycle-structure.scala:9 ]
@@ -16,7 +16,7 @@
1616
-- Error: tests/init/neg/cycle-structure.scala:3:13 --------------------------------------------------------------------
1717
3 | val x = B(this) // error
1818
| ^^^^^^^
19-
| Problematic object instantiation with uninitialized arguments. Calling trace:
19+
| Problematic object instantiation: arg 1 is not fully initialized. Calling trace:
2020
| -> case class A(b: B) { [ cycle-structure.scala:1 ]
2121
| ^
2222
| -> val x = B(this) // error [ cycle-structure.scala:3 ]

tests/init/neg/i15363.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/init/neg/i15363.scala:3:10 -----------------------------------------------------------------------------
22
3 | val b = new B(this) // error
33
| ^^^^^^^^^^^
4-
| Problematic object instantiation with uninitialized arguments. Calling trace:
4+
| Problematic object instantiation: arg 1 is not fully initialized. Calling trace:
55
| -> class A: [ i15363.scala:1 ]
66
| ^
77
| -> val b = new B(this) // error [ i15363.scala:3 ]

tests/init/neg/secondary-ctor4.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-- Error: tests/init/neg/secondary-ctor4.scala:54:14 -------------------------------------------------------------------
22
54 | val c = new C(b, 5) // error
33
| ^^^^^^^^^^^
4-
| Problematic object instantiation with uninitialized arguments. Calling trace:
4+
| Problematic object instantiation: the outer and arg 1 are not fully initialized. Calling trace:
55
| -> class N(d: D) extends M(d) { [ secondary-ctor4.scala:59 ]
66
| ^
77
| -> def this(d: D) = { [ secondary-ctor4.scala:7 ]
@@ -30,7 +30,7 @@
3030
-- Error: tests/init/neg/secondary-ctor4.scala:42:4 --------------------------------------------------------------------
3131
42 | new A(new B(new D)) // error
3232
| ^^^^^^^^^^^^^^^^^^^
33-
| Problematic object instantiation with uninitialized arguments. Calling trace:
33+
| Problematic object instantiation: the outer and arg 1 are not fully initialized. Calling trace:
3434
| -> class N(d: D) extends M(d) { [ secondary-ctor4.scala:59 ]
3535
| ^
3636
| -> def this(d: D) = { [ secondary-ctor4.scala:7 ]

0 commit comments

Comments
 (0)