Closed
Description
When using the staging API one can extract a mutable variable into a HOAS function and then substitute another bound mutable variable. This generates an intermediate immutable binding and assigns to that one, which is wrong.
Probably related to #9692
Minimized code
import scala.quoted._
import scala.quoted.staging._
object Test extends App {
// make available the necessary toolbox for runtime code generation
given Toolbox = Toolbox.make(getClass.getClassLoader)
run {
val expr: Expr[Int] = '{ var x = 1; x = 2; 42 }
expr match {
case '{ var x: Int = $binding; $body(x): Int } =>
val res = '{ var y = $binding; ${ Expr.betaReduce('{ $body(y) })}}
println(res.show)
res
case _ => println(expr.show); '{0}
}
}
}
Output (click arrow to expand)
[warn] -- Warning: /Users/jonathan/playground/staging-test/src/main/scala/Main.scala:14:4
[warn] 14 | val res = '{ var y = $binding; ${ Expr.betaReduce('{ $body(y) })}}
[warn] | ^
[warn] | Line is indented too far to the left, or a `}` is missing
[warn] -- Warning: /Users/jonathan/playground/staging-test/src/main/scala/Main.scala:15:4
[warn] 15 | println(res.show)
[warn] | ^
[warn] | Line is indented too far to the left, or a `}` is missing
[warn] -- Warning: /Users/jonathan/playground/staging-test/src/main/scala/Main.scala:16:4
[warn] 16 | res
[warn] | ^
[warn] | Line is indented too far to the left, or a `}` is missing
[warn] three warnings found
[info] running Test
{
var y: scala.Int = 1
val x: scala.Int = y
x = 2
42
}
exception while typing x = 2 of class class dotty.tools.dotc.ast.Trees$Assign # 1083
exception while typing {
val x: Int = y
x = 2
42
} of class class dotty.tools.dotc.ast.Trees$Block # 1091
exception while typing {
{
val x: Int = y
x = 2
42
}
} of class class dotty.tools.dotc.ast.Trees$Inlined # 1092
exception while typing {
var y: Int =
{
1
}
{
{
val x: Int = y
x = 2
42
}
}
} of class class dotty.tools.dotc.ast.Trees$Block # 1093
exception while typing {
{
var y: Int =
{
1
}
{
{
val x: Int = y
x = 2
42
}
}
}
} of class class dotty.tools.dotc.ast.Trees$Inlined # 1094
exception while typing def apply: Any =
{
{
var y: Int =
{
1
}
{
{
val x: Int = y
x = 2
42
}
}
}
} of class class dotty.tools.dotc.ast.Trees$DefDef # 31
exception while typing class Generated$Code$From$Quoted() extends Object() {
def apply: Any =
{
{
var y: Int =
{
1
}
{
{
val x: Int = y
x = 2
42
}
}
}
}
} of class class dotty.tools.dotc.ast.Trees$TypeDef # 33
exception while typing package _root_ {
class Generated$Code$From$Quoted() extends Object() {
def apply: Any =
{
{
var y: Int =
{
1
}
{
{
val x: Int = y
x = 2
42
}
}
}
}
}
} of class class dotty.tools.dotc.ast.Trees$PackageDef # 34
[error] (run-main-4) java.lang.ExceptionInInitializerError
[error] java.lang.ExceptionInInitializerError
[error] at Test.main(Main.scala)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[error] Caused by: class dotty.tools.dotc.reporting.Diagnostic$Error at src/main/scala/Main.scala:<255..260>: Reassignment to val x
[error] at dotty.tools.dotc.report$.error(report.scala:71)
[error] at dotty.tools.dotc.typer.ErrorReporting$.errorType(ErrorReporting.scala:29)
[error] at dotty.tools.dotc.typer.ErrorReporting$.errorTree(ErrorReporting.scala:20)
[error] at dotty.tools.dotc.typer.ErrorReporting$.errorTree(ErrorReporting.scala:23)
[error] at dotty.tools.dotc.typer.Typer.reassignmentToVal$1(Typer.scala:826)
[error] at dotty.tools.dotc.typer.Typer.typedAssign(Typer.scala:884)
[error] at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2463)
[error] at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2517)
[error] at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:122)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2586)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2590)
[error] at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2638)
[error] at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2661)
[error] at dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:979)
[error] at dotty.tools.dotc.typer.Typer.typedBlockStats(Typer.scala:896)
[error] at dotty.tools.dotc.typer.Typer.typedBlock(Typer.scala:900)
[error] at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2464)
[error] at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2517)
[error] at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:122)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2586)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2590)
[error] at dotty.tools.dotc.typer.Typer.typedInlined(Typer.scala:1629)
[error] at dotty.tools.dotc.transform.Erasure$Typer.typedInlined(Erasure.scala:840)
[error] at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2477)
[error] at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2517)
[error] at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:122)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2586)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2590)
[error] at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2705)
[error] at dotty.tools.dotc.typer.Typer.typedBlock(Typer.scala:902)
[error] at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2464)
[error] at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2517)
[error] at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:122)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2586)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2590)
[error] at dotty.tools.dotc.typer.Typer.typedInlined(Typer.scala:1629)
[error] at dotty.tools.dotc.transform.Erasure$Typer.typedInlined(Erasure.scala:840)
[error] at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2477)
[error] at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2517)
[error] at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:122)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2586)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2583)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2590)
[error] at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2705)
[error] at dotty.tools.dotc.typer.Typer.$anonfun$34(Typer.scala:1981)
[error] at dotty.tools.dotc.typer.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:217)
[error] at dotty.tools.dotc.typer.Typer.typedDefDef(Typer.scala:1981)
[error] at dotty.tools.dotc.transform.Erasure$Typer.typedDefDef(Erasure.scala:892)
[error] at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2433)
[error] at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2516)
[error] at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:122)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2586)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2590)
[error] at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2612)
[error] at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2661)
[error] at dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:979)
[error] at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2118)
[error] at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$2(Typer.scala:2444)
[error] at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2448)
[error] at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2516)
[error] at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:122)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2586)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2590)
[error] at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2612)
[error] at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:2661)
[error] at dotty.tools.dotc.transform.Erasure$Typer.typedStats(Erasure.scala:979)
[error] at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2242)
[error] at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2488)
[error] at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2517)
[error] at dotty.tools.dotc.typer.ReTyper.typedUnadapted(ReTyper.scala:122)
[error] at dotty.tools.dotc.typer.Typer.op$proxy16$1(Typer.scala:2586)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2587)
[error] at dotty.tools.dotc.typer.Typer.typed(Typer.scala:2590)
[error] at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:2705)
[error] at dotty.tools.dotc.transform.Erasure.run(Erasure.scala:123)
[error] at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:296)
[error] at scala.collection.immutable.List.map(List.scala:246)
[error] at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:297)
[error] at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:180)
[error] at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
[error] at dotty.tools.dotc.Run.runPhases$5(Run.scala:190)
[error] at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:198)
[error] at dotty.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error] at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:64)
[error] at dotty.tools.dotc.Run.compileUnits(Run.scala:205)
[error] at dotty.tools.dotc.Run.compileUnits(Run.scala:147)
[error] at scala.quoted.staging.QuoteCompiler$ExprRun.compileExpr(QuoteCompiler.scala:107)
[error] at scala.quoted.staging.QuoteDriver.run(QuoteDriver.scala:39)
[error] at scala.quoted.staging.Toolbox$$anon$1.run(Toolbox.scala:36)
[error] at scala.quoted.staging.package$.run(staging.scala:19)
[error] at Test$.<clinit>(Main.scala:19)
[error] at Test.main(Main.scala)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.base/java.lang.reflect.Method.invoke(Method.java:566)