Skip to content

Commit 95a6b44

Browse files
Merge pull request #9696 from dotty-staging/dissalow-vars-in-higher-order-patterns
Fix #9692 and fix #9693: Emit error when var is used in HO pattern
2 parents de273a2 + 33aa62e commit 95a6b44

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ trait QuotesAndSplices {
237237
val newSplice = ref(defn.InternalQuoted_exprSplice).appliedToType(tpt1.tpe).appliedTo(Typed(pat, exprTpt))
238238
transform(newSplice)
239239
case Apply(TypeApply(fn, targs), Apply(sp, pat :: Nil) :: args :: Nil) if fn.symbol == defn.InternalQuotedMatcher_patternHigherOrderHole =>
240+
args match // TODO support these patterns. Possibly using scala.quoted.util.Var
241+
case SeqLiteral(args, _) =>
242+
for arg <- args; if arg.symbol.is(Mutable) do
243+
report.error("References to `var`s cannot be used in higher-order pattern", arg.srcPos)
240244
try ref(defn.InternalQuotedMatcher_higherOrderHole.termRef).appliedToTypeTrees(targs).appliedTo(args).withSpan(tree.span)
241245
finally {
242246
val patType = pat.tpe.widen

tests/neg-staging/i9692.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted._
2+
import scala.quoted.staging._
3+
4+
object Test extends App {
5+
6+
// make available the necessary toolbox for runtime code generation
7+
given Toolbox = Toolbox.make(getClass.getClassLoader)
8+
9+
run {
10+
val expr: Expr[Int] = '{ var x = 1; x = 2; 42 }
11+
12+
expr match {
13+
case '{ var x: Int = $binding; $body(x): Int } => // error
14+
val res = Expr.betaReduce('{ $body(4) })
15+
println(res.show)
16+
res
17+
case _ => println(expr.show); '{0}
18+
}
19+
}
20+
}

tests/neg-staging/i9693.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted._
2+
import scala.quoted.staging._
3+
4+
object Test extends App {
5+
6+
// make available the necessary toolbox for runtime code generation
7+
given Toolbox = Toolbox.make(getClass.getClassLoader)
8+
9+
run {
10+
val expr: Expr[Int] = '{ var x = 1; x = 2; 42 }
11+
12+
expr match {
13+
case '{ var x: Int = $binding; $body(x): Int } => // error
14+
val res = '{ var y = $binding; ${ Expr.betaReduce('{ $body(y) })}}
15+
println(res.show)
16+
res
17+
case _ => println(expr.show); '{0}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)