Skip to content

WIP Avoid mutation during iteration bug in package object lazy open #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.13.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/Analyzer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
package scala.tools.nsc
package typechecker

import scala.collection.mutable
import scala.collection.mutable.ArrayDeque
import scala.reflect.internal.util.JavaClearable

/** Defines the sub-components for the namer, packageobjects, and typer phases.
*/
Expand Down Expand Up @@ -55,7 +57,13 @@ trait Analyzer extends AnyRef
object packageObjects extends {
val global: Analyzer.this.global.type = Analyzer.this.global
} with SubComponent {
val deferredOpen = perRunCaches.newSet[Symbol]()
val deferredOpen: mutable.Set[Symbol] = {
import scala.jdk.CollectionConverters._
// This will throw a ConcurrentModificationException if we mutate during iteration
val javaSet = new java.util.LinkedHashSet[Symbol]()
perRunCaches.recordCache(JavaClearable.forCollection(javaSet))
javaSet.asScala
}
val phaseName = "packageobjects"
val runsAfter = List[String]()
val runsRightAfter= Some("namer")
Expand All @@ -80,8 +88,18 @@ trait Analyzer extends AnyRef

def apply(unit: CompilationUnit): Unit = {
openPackageObjectsTraverser(unit.body)
deferredOpen.foreach(openPackageModule(_))
deferredOpen.clear()
}

override def run(): Unit = {
super.run()

for (sym <- deferredOpen.toVector) {
if (deferredOpen.remove(sym)) {
// this can remove entries from `deferredOpen`, hence the copy to a vector
// and the check of `remove` return value
openPackageModule(sym)
}
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/files/pos/package-object-deferred-load-bug/A_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package p1 {
package x_01 { object zappo {}}

package x_64 {
object zappo {}
}
}
10 changes: 10 additions & 0 deletions test/files/pos/package-object-deferred-load-bug/A_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package p1 {
import x_64._
package x_01 {
object blerg extends AnyRef {}
}

package x_64 {
object blerg {}
}
}
10 changes: 10 additions & 0 deletions test/files/pos/package-object-deferred-load-bug/B_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package p1 {
import x_64._
package x_01 {
object `package` extends AnyRef { def m = "m "}
}

package x_64 {
object `package` { def m = "m" }
}
}