Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Detect quoted pattern variables in alternatives #15073

Merged
merged 2 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Mode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object Mode {
/** Don't suppress exceptions thrown during show */
val PrintShowExceptions: Mode = newMode(18, "PrintShowExceptions")

val PatternOrTypeBits: Mode = Pattern | Type | InPatternAlternative
val PatternOrTypeBits: Mode = Pattern | Type

/** We are elaborating the fully qualified name of a package clause.
* In this case, identifiers should never be imported.
Expand Down
6 changes: 4 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 @@ -321,7 +321,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
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] = ???