Skip to content

Commit

Permalink
Fix async transform in REPL
Browse files Browse the repository at this point in the history
The async transform only runs on source files with `await` calls.
For REPL lines there are two SourceFile created for a single line.
Make sure the async phase connects these dots.
  • Loading branch information
lrytz committed Oct 23, 2024
1 parent 0b0fa5a commit d64861a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/compiler/scala/tools/nsc/transform/async/AsyncPhase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ package scala.tools.nsc.transform.async

import scala.collection.mutable
import scala.tools.nsc.transform.{Transform, TypingTransformers}
import scala.reflect.internal.util.{SourceFile, NoSourceFile}
import scala.reflect.internal.util.{NoSourceFile, ReplBatchSourceFile, SourceFile}

abstract class AsyncPhase extends Transform with TypingTransformers with AnfTransform with Lifter with LiveVariables {
self =>
Expand Down Expand Up @@ -81,7 +81,11 @@ abstract class AsyncPhase extends Transform with TypingTransformers with AnfTran
override def transformUnit(unit: CompilationUnit): Unit = {
if (settings.async.value) {
// NoSourceFile can happen for, e.g., toolbox compilation; overestimate by always transforming them. See test/async/jvm/toolbox.scala
val shouldTransform = unit.source == NoSourceFile || sourceFilesToTransform.contains(unit.source)
val shouldTransform = unit.source == NoSourceFile || sourceFilesToTransform(unit.source) || (unit.source match {
// scala/bug#13050, see also `PerRunReporting.repSrc`
case r: ReplBatchSourceFile => sourceFilesToTransform(r.parserSource)
case _ => false
})
if (shouldTransform) super.transformUnit(unit)
if (awaits.exists(_.isInitialized)) {
unit.body.foreach {
Expand Down
8 changes: 8 additions & 0 deletions test/files/run/t13050.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

scala> import scala.tools.partest.async.OptionAwait._
import scala.tools.partest.async.OptionAwait._

scala> println(optionally { value(Some(1)) + value(Some(2)) })
Some(3)

scala> :quit
9 changes: 9 additions & 0 deletions test/files/run/t13050.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.tools.nsc._
import scala.tools.partest.ReplTest

object Test extends ReplTest {
override def extraSettings = "-Xasync"
def code =
"""import scala.tools.partest.async.OptionAwait._
|println(optionally { value(Some(1)) + value(Some(2)) })""".stripMargin
}

0 comments on commit d64861a

Please sign in to comment.