Skip to content

Fixing bug in global initialization checker when accessing global object #20424

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

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ object BestEffortTastyWriter:
unit.pickled.foreach { (clz, binary) =>
val parts = clz.fullName.mangledString.split('.')
val outPath = outputPath(parts.toList, dir)
val outTastyFile = new File(outPath)
val outstream = new DataOutputStream(new PlainFile(outTastyFile).bufferedOutput)
val file = new File(outPath)
val outTastyFile = new PlainFile(file)
val outstream = new DataOutputStream(outTastyFile.bufferedOutput)
try outstream.write(binary())
catch case ex: ClosedByInterruptException =>
try
outTastyFile.delete() // don't leave an empty or half-written tastyfile around after an interrupt
file.delete() // don't leave an empty or half-written tastyfile around after an interrupt
catch
case _: Throwable =>
throw ex
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/init/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ class Objects(using Context @constructorOnly):
Bottom

case Bottom =>
if field.isStaticObject then ObjectRef(field.moduleClass.asClass)
if field.isStaticObject then accessObject(field.moduleClass.asClass)
else Bottom

case ValueSet(values) =>
Expand Down
9 changes: 9 additions & 0 deletions tests/init-global/warn/cyclic-object.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cyclicObject

object O1 { // warn
val o = cyclicObject.O2
}

object O2 {
val o = cyclicObject.O1
}
26 changes: 26 additions & 0 deletions tests/init-global/warn/deadlock.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// example of cyclic initialization causing deadlock

package pkg

object Main extends App {
val createPredef = new Runnable { def run = { val _ = Predef } }
val createSeq = new Runnable { def run = { val _ = Seq } }
new Thread(createPredef).start()
new Thread(createSeq).start()
Thread.sleep(100)
val seq = Predef.seq
val predef = Seq.predef
println("done")
}

object Predef { // warn
Thread.sleep(10)
val seq = Seq
println("done Predef")
}

object Seq {
Thread.sleep(10)
val predef = Predef
println("done Seq")
}
Loading