Skip to content

Commit 8bdd645

Browse files
committed
Refine trace and error message for cycles
1 parent ec5c7ba commit 8bdd645

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ class CycleChecker(cache: Cache) {
146146
val obj = dep.symbol
147147
if state.path.contains(obj) then
148148
val cycle = state.path.dropWhile(_ != obj)
149-
val trace = state.trace.map(_.source) :+ dep.source
149+
val trace = state.trace.dropWhile(_.symbol != obj).map(_.source) :+ dep.source
150150
if cycle.size > 1 then
151-
CyclicObjectInit(obj, trace) :: Nil
151+
CyclicObjectInit(cycle, trace) :: Nil
152152
else
153153
ObjectLeakDuringInit(obj, trace) :: Nil
154154
else
155155
val constr = obj.moduleClass.primaryConstructor
156156
state.visitObject(dep) {
157-
check(StaticCall(constr.owner.asClass, constr)(constr.defTree))
157+
check(StaticCall(constr.owner.asClass, constr)(obj.moduleClass.defTree))
158158
}
159159
}
160160

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ object Errors {
6969
report.warning(show + stacktrace, field.srcPos)
7070
}
7171

72-
case class CyclicObjectInit(obj: Symbol, trace: Seq[Tree]) extends Error {
72+
case class CyclicObjectInit(objs: Seq[Symbol], trace: Seq[Tree]) extends Error {
7373
def source: Tree = trace.last
7474
def show(using Context): String =
75-
"Cyclic object initialization of " + obj.show + "."
75+
"Cyclic object initialization for " + objs.map(_.show).mkString(", ") + "."
7676

7777
override def issue(using Context): Unit =
78-
report.warning(show + stacktrace, obj.srcPos)
78+
report.warning(show + stacktrace, objs.head.srcPos)
7979
}
8080

8181
case class ObjectLeakDuringInit(obj: Symbol, trace: Seq[Tree]) extends Error {

0 commit comments

Comments
 (0)