Skip to content

Commit 6457877

Browse files
committed
Only report non-trivial cycles
It is necessary for the following code to pass the check: enum Color(val x: Int): case Violet extends Color(Green.x + 1) case Green extends Color(3)
1 parent f78b1dd commit 6457877

File tree

9 files changed

+3
-4
lines changed

9 files changed

+3
-4
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ object Objects:
159159
def checkCycle(clazz: ClassSymbol)(work: => Unit)(using data: Data, ctx: Context, pendingTrace: Trace) =
160160
val index = data.checkingObjects.indexOf(clazz)
161161

162-
if index != -1 then
162+
if index != -1 && data.checkingObjects.size > 1 then
163163
val joinedTrace = data.pendingTraces.slice(index + 1, data.checkingObjects.size).foldLeft(pendingTrace) { (a, acc) => acc ++ a }
164164
val callTrace = Trace.buildStacktrace(joinedTrace, "Calling trace:\n")
165165
val cycle = data.checkingObjects.slice(index, data.checkingObjects.size)
166166
report.warning("Cyclic initialization: " + cycle.map(_.show).mkString(" -> ") + " -> " + clazz.show + ". " + callTrace, clazz.defTree)
167-
else if data.checkedObjects.indexOf(clazz) == -1 then
167+
else if index == -1 && data.checkedObjects.indexOf(clazz) == -1 then
168168
data.pendingTraces += pendingTrace
169169
data.checkingObjects += clazz
170170
work
@@ -483,7 +483,6 @@ object Objects:
483483
refs.foreach(ref => assign(ref, field, rhs, rhsTyp))
484484

485485
case ref: Ref =>
486-
println("ref = " + ref.show + ", ref.owner = " + ref.owner.show + ", current = " + State.currentObject.show)
487486
if ref.owner != State.currentObject then
488487
errorMutateOtherStaticObject(State.currentObject, ref.owner)
489488
else

tests/init/neg/i12544.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ enum Enum:
55
def g(b: Enum.B): Int = b.foo()
66

77
object Enum:
8-
object nested:
8+
object nested: // error
99
val a: Enum = Case
1010

1111
val b: Enum = f(nested.a)

0 commit comments

Comments
 (0)