Skip to content

Commit 162b543

Browse files
authored
Suppressing repetitive warnings in the global initialization checker (#19898)
This PR suppresses repetitive warnings in the global initialization checker to make the warnings more manageable to analyze.
2 parents a9bb881 + ac40aa6 commit 162b543

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Diff for: compiler/src/dotty/tools/dotc/transform/init/Checker.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ class Checker extends Phase:
5050
Semantic.checkClasses(classes)(using checkCtx)
5151

5252
if ctx.settings.YcheckInitGlobal.value then
53-
Objects.checkClasses(classes)(using checkCtx)
53+
val obj = new Objects
54+
obj.checkClasses(classes)(using checkCtx)
5455
}
5556

5657
units0

Diff for: compiler/src/dotty/tools/dotc/transform/init/Objects.scala

+13-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ import dotty.tools.dotc.core.Flags.AbstractOrTrait
6666
* whole-program analysis. However, the check is not modular in terms of project boundaries.
6767
*
6868
*/
69-
object Objects:
69+
class Objects:
7070

7171
// ----------------------------- abstract domain -----------------------------
7272

@@ -1757,16 +1757,20 @@ object Objects:
17571757
if cls.isAllOf(Flags.JavaInterface) then Bottom
17581758
else evalType(tref.prefix, thisV, klass, elideObjectAccess = cls.isStatic)
17591759

1760+
val mutateErrorSet: mutable.Set[(ClassSymbol, ClassSymbol)] = mutable.Set.empty
17601761
def errorMutateOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) =
1761-
val msg =
1762-
s"Mutating ${otherObj.show} during initialization of ${currentObj.show}.\n" +
1763-
"Mutating other static objects during the initialization of one static object is forbidden. " + Trace.show
1762+
if mutateErrorSet.add((currentObj, otherObj)) then
1763+
val msg =
1764+
s"Mutating ${otherObj.show} during initialization of ${currentObj.show}.\n" +
1765+
"Mutating other static objects during the initialization of one static object is forbidden. " + Trace.show
17641766

1765-
report.warning(msg, Trace.position)
1767+
report.warning(msg, Trace.position)
17661768

1769+
val readErrorSet: mutable.Set[(ClassSymbol, ClassSymbol)] = mutable.Set.empty
17671770
def errorReadOtherStaticObject(currentObj: ClassSymbol, otherObj: ClassSymbol)(using Trace, Context) =
1768-
val msg =
1769-
"Reading mutable state of " + otherObj.show + " during initialization of " + currentObj.show + ".\n" +
1770-
"Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " + Trace.show
1771+
if readErrorSet.add((currentObj, otherObj)) then
1772+
val msg =
1773+
"Reading mutable state of " + otherObj.show + " during initialization of " + currentObj.show + ".\n" +
1774+
"Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. " + Trace.show
17711775

1772-
report.warning(msg, Trace.position)
1776+
report.warning(msg, Trace.position)

0 commit comments

Comments
 (0)