From ebf5af13231e09b05f0c6668e361cdba44bbc8d8 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 1 Sep 2020 15:47:41 +0200 Subject: [PATCH 1/3] Fix #9692 and fix #9693: Emit error when var is used in HO pattern --- .../tools/dotc/typer/QuotesAndSplices.scala | 4 ++++ tests/neg-staging/i9692.scala | 20 +++++++++++++++++++ tests/neg-staging/i9693.scala | 20 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tests/neg-staging/i9692.scala create mode 100644 tests/neg-staging/i9693.scala diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index 1dc6a71fb842..d8b5253833ee 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -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: refercences to `var`s cannot be used in higher-horder pattern", arg.srcPos) try ref(defn.InternalQuotedMatcher_higherOrderHole.termRef).appliedToTypeTrees(targs).appliedTo(args).withSpan(tree.span) finally { val patType = pat.tpe.widen diff --git a/tests/neg-staging/i9692.scala b/tests/neg-staging/i9692.scala new file mode 100644 index 000000000000..fdb4e51bd6bb --- /dev/null +++ b/tests/neg-staging/i9692.scala @@ -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} + } + } +} diff --git a/tests/neg-staging/i9693.scala b/tests/neg-staging/i9693.scala new file mode 100644 index 000000000000..9705bc012c41 --- /dev/null +++ b/tests/neg-staging/i9693.scala @@ -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} + } + } +} From 0a54504d7cebbba866db10edcf2c17078e9a0882 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 1 Sep 2020 17:31:43 +0200 Subject: [PATCH 2/3] Update compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala Co-authored-by: Guillaume Martres --- compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index d8b5253833ee..b4f14b8e068f 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -240,7 +240,7 @@ trait QuotesAndSplices { 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: refercences to `var`s cannot be used in higher-horder pattern", arg.srcPos) + 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 From 33aa62e06fcd5e782bd479006e605492bd178296 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 2 Sep 2020 14:19:18 +0200 Subject: [PATCH 3/3] Update compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala Co-authored-by: Fengyun Liu --- compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index b4f14b8e068f..9850076f626e 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -240,7 +240,7 @@ trait QuotesAndSplices { 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) + report.error("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