Skip to content

Detect quoted pattern variables in alternatives #15073

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
merged 2 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import dotty.tools.dotc.typer.Implicits._
import dotty.tools.dotc.typer.Inferencing._
import dotty.tools.dotc.util.Spans._
import dotty.tools.dotc.util.Stats.record

import dotty.tools.dotc.reporting.IllegalVariableInPatternAlternative
import scala.collection.mutable


Expand Down Expand Up @@ -243,6 +243,17 @@ trait QuotesAndSplices {
res
}

def checkAlternativeBinds(pat0: Tree): Unit =
def rec(pat: Tree): Unit =
pat match
case Typed(pat, _) => rec(pat)
case UnApply(_, _, pats) => pats.foreach(rec)
case pat: Bind =>
report.error(IllegalVariableInPatternAlternative(pat.symbol.name), pat.withSpan(pat.nameSpan))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come we need to check this here, isn't typedBind going to run on this tree later and produce the same error via https://github.com/lampepfl/dotty/blob/400427db21410b366e4d9b890d73cec15416b632/compiler/src/dotty/tools/dotc/typer/Typer.scala#L2141-L2142 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context at that point lost the mode InPatternAlternative. Now I found out where that is happening and will push a better fix.

rec(pat.body)
case _ =>
if ctx.mode.is(Mode.InPatternAlternative) then rec(pat0)

val patBuf = new mutable.ListBuffer[Tree]
val freshTypePatBuf = new mutable.ListBuffer[Tree]
val freshTypeBindingsBuff = new mutable.ListBuffer[Tree]
Expand All @@ -254,6 +265,7 @@ trait QuotesAndSplices {
val newSplice = ref(defn.QuotedRuntime_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.QuotedRuntimePatterns_patternHigherOrderHole =>
checkAlternativeBinds(pat)
args match // TODO support these patterns. Possibly using scala.quoted.util.Var
case SeqLiteral(args, _) =>
for arg <- args; if arg.symbol.is(Mutable) do
Expand All @@ -266,6 +278,7 @@ trait QuotesAndSplices {
patBuf += pat1
}
case Apply(fn, pat :: Nil) if fn.symbol.isExprSplice =>
checkAlternativeBinds(pat)
try ref(defn.QuotedRuntimePatterns_patternHole.termRef).appliedToType(tree.tpe).withSpan(tree.span)
finally {
val patType = pat.tpe.widen
Expand Down Expand Up @@ -321,7 +334,9 @@ trait QuotesAndSplices {
}

private def transformTypeBindingTypeDef(nameOfSyntheticGiven: TermName, tdef: TypeDef, buff: mutable.Builder[Tree, List[Tree]])(using Context): Tree = {
if (variance == -1)
if ctx.mode.is(Mode.InPatternAlternative) then
report.error(IllegalVariableInPatternAlternative(tdef.symbol.name), tdef.srcPos)
if variance == -1 then
tdef.symbol.addAnnotation(Annotation(New(ref(defn.QuotedRuntimePatterns_fromAboveAnnot.typeRef)).withSpan(tdef.span)))
val bindingType = getBinding(tdef.symbol).symbol.typeRef
val bindingTypeTpe = AppliedType(defn.QuotedTypeClass.typeRef, bindingType :: Nil)
Expand Down
27 changes: 27 additions & 0 deletions tests/neg-macros/i14696.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import scala.quoted.*

def test(a: Expr[Any])(using Quotes): Unit = a match
case '{ ${_}: String } | '{ ${_}: Int } =>
case '{ $x1: String } | '{ ${_}: Int } => // error
case '{ $x: String } | '{ $x: Int } => // error // error
case '{ $x1: String } | '{ ${x2: Expr[Int]} } => // error // error
case '{ ${Str(x)}: String } | '{ ${y @ Str(z)}: Int } => // error // error // error
case '{ val x: Int = ???; ${_}(x): String } | '{ '{ val x: String = ???; ${_}(x): String }} =>
case '{ val x: Int = ???; $f(x): String } | '{ val x: String = ???; ${_}(x): String } => // error
case '{ val x: Int = ???; $f(x): String } | '{ val x: String = ???; $f(x): String } => // error // error
case '{ val x: Int = ???; $f(x): String } | '{ val x: String = ???; $g(x): String } => // error // error
case '{ varargs(${_}*) } | '{ varargs(${_}*) } =>
case '{ varargs($args*) } | '{ varargs(${_}*) } => // error
case '{ varargs($args*) } | '{ varargs($args*) } => // error // error
case '{ varargs($args1*) } | '{ varargs($args2*) } => // error // error
case '{ ${_}: t } | '{ ${_}: Int } => // error
case '{ ${_}: t } | '{ ${_}: t } => // error // error
case '{ ${_}: t } | '{ ${_}: u } => // error // error
case '{ type t; () } | '{ 1 } => // error
case '{ type t; () } | '{ type t; () } => // error // error
case '{ type t; () } | '{ type u; () } => // error // error

def varargs(x: Any*): Unit = ()

object Str:
def unapply(x: Any): Option[String] = ???