Skip to content

Commit 8dd0770

Browse files
authored
Enable UnrollDefinitions phase in REPL frontend phases (#23433)
Fixes #23408 by enabling `UnrollDefinitions` phase in ReplCompiler.
2 parents 9d191c8 + 7e5b443 commit 8dd0770

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

compiler/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import dotty.tools.dotc.core.Phases.Phase
1212
import dotty.tools.dotc.core.StdNames.*
1313
import dotty.tools.dotc.core.Symbols.*
1414
import dotty.tools.dotc.reporting.Diagnostic
15-
import dotty.tools.dotc.transform.{CheckUnused, CheckShadowing, PostTyper}
15+
import dotty.tools.dotc.transform.{CheckUnused, CheckShadowing, PostTyper, UnrollDefinitions}
1616
import dotty.tools.dotc.typer.ImportInfo.{withRootImports, RootRef}
1717
import dotty.tools.dotc.typer.TyperPhase
1818
import dotty.tools.dotc.util.Spans.*
@@ -40,6 +40,7 @@ class ReplCompiler extends Compiler:
4040
List(CheckUnused.PostTyper(), CheckShadowing()),
4141
List(CollectTopLevelImports()),
4242
List(PostTyper()),
43+
List(UnrollDefinitions()),
4344
)
4445

4546
def newRun(initCtx: Context, state: State): Run =

compiler/test/dotty/tools/repl/ReplCompilerTests.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,34 @@ class ReplHighlightTests extends ReplTest(ReplTest.defaultOptions.filterNot(_.st
582582
case class Tree(left: Tree, right: Tree)
583583
def deepTree(depth: Int): Tree
584584
deepTree(300)""")
585+
586+
class ReplUnrollTests extends ReplTest(ReplTest.defaultOptions ++ Seq("-experimental", "-Xprint:pickler")):
587+
override val redirectOutput = true
588+
@Test def i23408: Unit = initially:
589+
run("""
590+
import scala.annotation.unroll
591+
case class Foo(x: Int, @unroll y: Option[String] = None)"""
592+
)
593+
val expected = List(
594+
"def copy(x: Int, y: Option[String]): Foo = new Foo(x, y)",
595+
"def copy(x: Int): Foo = this.copy(x, this.copy$default$2)",
596+
"def copy$default$1: Int @uncheckedVariance = Foo.this.x",
597+
"def copy$default$2: Option[String] @uncheckedVariance = Foo.this.y",
598+
"def apply(x: Int, y: Option[String]): Foo = new Foo(x, y)",
599+
"def apply(x: Int): Foo = this.apply(x, Foo.$lessinit$greater$default$2)",
600+
"""def fromProduct(x$0: Product): Foo.MirroredMonoType = {
601+
val arity: Int = x$0.productArity
602+
val x$1: Int = x$0.productElement(0).$asInstanceOf[Int]
603+
val y$1: Option[String] = (if arity > 1 then x$0.productElement(1) else Foo.$lessinit$greater$default$2).$asInstanceOf[Option[String]]
604+
new Foo(x$1, y$1)
605+
}"""
606+
)
607+
def trimWhitespaces(input: String): String = input.replaceAll("\\s+", " ")
608+
val output = storedOutput()
609+
val normalizedOutput = trimWhitespaces(output)
610+
expected.foreach: defn =>
611+
val normalizedDefn = trimWhitespaces(defn)
612+
assertTrue(
613+
s"Output: '$output' did not contain expected definition: ${defn}",
614+
normalizedOutput.contains(normalizedDefn)
615+
)

0 commit comments

Comments
 (0)