Skip to content

Disallow quoted patterns that are not supported #8986

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
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
20 changes: 18 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,15 @@ trait QuotesAndSplices {
TypeTree(tree.tpe.dealias).withSpan(tree.span)
else
tree
case tdef: TypeDef if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternTypeAnnot) =>
transformTypeBindingTypeDef(tdef, typePatBuf)
case tdef: TypeDef =>
if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternTypeAnnot) then
transformTypeBindingTypeDef(tdef, typePatBuf)
else if tdef.symbol.isClass then
val kind = if tdef.symbol.is(Module) then "objects" else "classes"
ctx.error("Implementation restriction: cannot match " + kind, tree.sourcePos)
EmptyTree
else
super.transform(tree)
case tree @ AppliedTypeTree(tpt, args) =>
val args1: List[Tree] = args.zipWithConserve(tpt.tpe.typeParams.map(_.paramVarianceSign)) { (arg, v) =>
arg.tpe match {
Expand All @@ -254,6 +261,15 @@ trait QuotesAndSplices {
if tree.name.isTermName && !tree.nameSpan.isSynthetic && tree.name.startsWith("$") then
ctx.error("Names cannot start with $ quote pattern ", tree.namePos)
super.transform(tree)
case _: Match =>
ctx.error("Implementation restriction: cannot match `match` expressions", tree.sourcePos)
EmptyTree
case _: Try =>
ctx.error("Implementation restriction: cannot match `try` expressions", tree.sourcePos)
EmptyTree
case _: Return =>
ctx.error("Implementation restriction: cannot match `return` statements", tree.sourcePos)
EmptyTree
case _ =>
super.transform(tree)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/neg-macros/quote-pattern-implemnetation-restrictions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.quoted._

def f(x: Expr[Any])(using QuoteContext) =
x match {
case '{ class Foo; () } => // error
case '{ object Foo; () } => // error
case '{ 1 match { case _ => () } } => // error
case '{ try 1 finally () } => // error
case '{ try 1 catch { case _ => 4 } } => // error
case '{ Nil.map({ case x: Int => () }) } => // error
case '{ def f: Int = return 2 } => // error
}
16 changes: 0 additions & 16 deletions tests/pos/quoted-pattern-type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,6 @@ object Lib {
z: Expr[T & Int]
e

case e @ '{ ($x: Boolean) match { case _ => $y: Int } } =>
e: Expr[T & Int]
y: Expr[T & Int]
e

case e @ '{ ($x: Boolean) match { case _ => $y } } =>
e: Expr[T]
y: Expr[T]
e

case e @ '{ try ($x: Boolean) catch { case _ => $y: Int } } =>
e: Expr[T & (Boolean | Int)]
x: Expr[T & Boolean]
y: Expr[T & Int]
e

}
}
}