diff --git a/compiler/src/dotty/tools/dotc/transform/init/Checker.scala b/compiler/src/dotty/tools/dotc/transform/init/Checker.scala index 7cf028c95064..692b3177786d 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Checker.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Checker.scala @@ -50,7 +50,8 @@ class Checker extends Phase: Semantic.checkClasses(classes)(using checkCtx) if ctx.settings.YcheckInitGlobal.value then - Objects.checkClasses(classes)(using checkCtx) + val obj = new Objects + obj.checkClasses(classes)(using checkCtx) } units0 diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index 19570f13c519..4b56a7c81ca4 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -66,7 +66,7 @@ import dotty.tools.dotc.core.Flags.AbstractOrTrait * whole-program analysis. However, the check is not modular in terms of project boundaries. * */ -object Objects: +class Objects: // ----------------------------- abstract domain ----------------------------- @@ -1757,16 +1757,20 @@ object Objects: if cls.isAllOf(Flags.JavaInterface) then Bottom else evalType(tref.prefix, thisV, klass, elideObjectAccess = cls.isStatic) + val mutateErrorSet: mutable.Set[(ClassSymbol, ClassSymbol)] = mutable.Set.empty def errorMutateOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) = - val msg = - s"Mutating ${otherObj.show} during initialization of ${currentObj.show}.\n" + - "Mutating other static objects during the initialization of one static object is forbidden. " + Trace.show + if mutateErrorSet.add((currentObj, otherObj)) then + val msg = + s"Mutating ${otherObj.show} during initialization of ${currentObj.show}.\n" + + "Mutating other static objects during the initialization of one static object is forbidden. " + Trace.show - report.warning(msg, Trace.position) + report.warning(msg, Trace.position) + val readErrorSet: mutable.Set[(ClassSymbol, ClassSymbol)] = mutable.Set.empty def errorReadOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) = - val msg = - "Reading mutable state of " + otherObj.show + " during initialization of " + currentObj.show + ".\n" + - "Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " + Trace.show + if readErrorSet.add((currentObj, otherObj)) then + val msg = + "Reading mutable state of " + otherObj.show + " during initialization of " + currentObj.show + ".\n" + + "Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " + Trace.show - report.warning(msg, Trace.position) + report.warning(msg, Trace.position)