Skip to content

Fix #9692 and fix #9693: Emit error when var is used in HO pattern #9696

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

Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ trait QuotesAndSplices {
val newSplice = ref(defn.InternalQuoted_exprSplice).appliedToType(tpt1.tpe).appliedTo(Typed(pat, exprTpt))
transform(newSplice)
case Apply(TypeApply(fn, targs), Apply(sp, pat :: Nil) :: args :: Nil) if fn.symbol == defn.InternalQuotedMatcher_patternHigherOrderHole =>
args match // TODO support these patterns. Possibly using scala.quoted.util.Var
case SeqLiteral(args, _) =>
for arg <- args; if arg.symbol.is(Mutable) do
report.error("Implementation restriction: references to `var`s cannot be used in higher-order pattern", arg.srcPos)
try ref(defn.InternalQuotedMatcher_higherOrderHole.termRef).appliedToTypeTrees(targs).appliedTo(args).withSpan(tree.span)
finally {
val patType = pat.tpe.widen
Expand Down
20 changes: 20 additions & 0 deletions tests/neg-staging/i9692.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 } => // error
val res = Expr.betaReduce('{ $body(4) })
println(res.show)
res
case _ => println(expr.show); '{0}
}
}
}
20 changes: 20 additions & 0 deletions tests/neg-staging/i9693.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 } => // error
val res = '{ var y = $binding; ${ Expr.betaReduce('{ $body(y) })}}
println(res.show)
res
case _ => println(expr.show); '{0}
}
}
}