Skip to content

Commit 6540ad9

Browse files
Merge pull request #15073 from dotty-staging/fix-14696
Detect quoted pattern variables in alternatives
2 parents b0ebb70 + b61181f commit 6540ad9

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

compiler/src/dotty/tools/dotc/core/Mode.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ object Mode {
8989
/** Don't suppress exceptions thrown during show */
9090
val PrintShowExceptions: Mode = newMode(18, "PrintShowExceptions")
9191

92-
val PatternOrTypeBits: Mode = Pattern | Type | InPatternAlternative
92+
val PatternOrTypeBits: Mode = Pattern | Type
9393

9494
/** We are elaborating the fully qualified name of a package clause.
9595
* In this case, identifiers should never be imported.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import dotty.tools.dotc.typer.Implicits._
2020
import dotty.tools.dotc.typer.Inferencing._
2121
import dotty.tools.dotc.util.Spans._
2222
import dotty.tools.dotc.util.Stats.record
23-
23+
import dotty.tools.dotc.reporting.IllegalVariableInPatternAlternative
2424
import scala.collection.mutable
2525

2626

@@ -321,7 +321,9 @@ trait QuotesAndSplices {
321321
}
322322

323323
private def transformTypeBindingTypeDef(nameOfSyntheticGiven: TermName, tdef: TypeDef, buff: mutable.Builder[Tree, List[Tree]])(using Context): Tree = {
324-
if (variance == -1)
324+
if ctx.mode.is(Mode.InPatternAlternative) then
325+
report.error(IllegalVariableInPatternAlternative(tdef.symbol.name), tdef.srcPos)
326+
if variance == -1 then
325327
tdef.symbol.addAnnotation(Annotation(New(ref(defn.QuotedRuntimePatterns_fromAboveAnnot.typeRef)).withSpan(tdef.span)))
326328
val bindingType = getBinding(tdef.symbol).symbol.typeRef
327329
val bindingTypeTpe = AppliedType(defn.QuotedTypeClass.typeRef, bindingType :: Nil)

tests/neg-macros/i14696.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import scala.quoted.*
2+
3+
def test(a: Expr[Any])(using Quotes): Unit = a match
4+
case '{ ${_}: String } | '{ ${_}: Int } =>
5+
case '{ $x1: String } | '{ ${_}: Int } => // error
6+
case '{ $x: String } | '{ $x: Int } => // error // error
7+
case '{ $x1: String } | '{ ${x2: Expr[Int]} } => // error // error
8+
case '{ ${Str(x)}: String } | '{ ${y @ Str(z)}: Int } => // error // error
9+
case '{ val x: Int = ???; ${_}(x): String } | '{ '{ val x: String = ???; ${_}(x): String }} =>
10+
case '{ val x: Int = ???; $f(x): String } | '{ val x: String = ???; ${_}(x): String } => // error
11+
case '{ val x: Int = ???; $f(x): String } | '{ val x: String = ???; $f(x): String } => // error // error
12+
case '{ val x: Int = ???; $f(x): String } | '{ val x: String = ???; $g(x): String } => // error // error
13+
case '{ varargs(${_}*) } | '{ varargs(${_}*) } =>
14+
case '{ varargs($args*) } | '{ varargs(${_}*) } => // error
15+
case '{ varargs($args*) } | '{ varargs($args*) } => // error // error
16+
case '{ varargs($args1*) } | '{ varargs($args2*) } => // error // error
17+
case '{ ${_}: t } | '{ ${_}: Int } => // error
18+
case '{ ${_}: t } | '{ ${_}: t } => // error // error
19+
case '{ ${_}: t } | '{ ${_}: u } => // error // error
20+
case '{ type t; () } | '{ 1 } => // error
21+
case '{ type t; () } | '{ type t; () } => // error // error
22+
case '{ type t; () } | '{ type u; () } => // error // error
23+
24+
def varargs(x: Any*): Unit = ()
25+
26+
object Str:
27+
def unapply(x: Any): Option[String] = ???

0 commit comments

Comments
 (0)