Skip to content

Commit 9b6ecad

Browse files
Merge pull request #8986 from dotty-staging/disallow-quoted-paterns-that-are-not-supported
Disallow quoted patterns that are not supported
2 parents 363dd03 + 7b01a86 commit 9b6ecad

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,15 @@ trait QuotesAndSplices {
240240
TypeTree(tree.tpe.dealias).withSpan(tree.span)
241241
else
242242
tree
243-
case tdef: TypeDef if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternTypeAnnot) =>
244-
transformTypeBindingTypeDef(tdef, typePatBuf)
243+
case tdef: TypeDef =>
244+
if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternTypeAnnot) then
245+
transformTypeBindingTypeDef(tdef, typePatBuf)
246+
else if tdef.symbol.isClass then
247+
val kind = if tdef.symbol.is(Module) then "objects" else "classes"
248+
ctx.error("Implementation restriction: cannot match " + kind, tree.sourcePos)
249+
EmptyTree
250+
else
251+
super.transform(tree)
245252
case tree @ AppliedTypeTree(tpt, args) =>
246253
val args1: List[Tree] = args.zipWithConserve(tpt.tpe.typeParams.map(_.paramVarianceSign)) { (arg, v) =>
247254
arg.tpe match {
@@ -254,6 +261,15 @@ trait QuotesAndSplices {
254261
if tree.name.isTermName && !tree.nameSpan.isSynthetic && tree.name.startsWith("$") then
255262
ctx.error("Names cannot start with $ quote pattern ", tree.namePos)
256263
super.transform(tree)
264+
case _: Match =>
265+
ctx.error("Implementation restriction: cannot match `match` expressions", tree.sourcePos)
266+
EmptyTree
267+
case _: Try =>
268+
ctx.error("Implementation restriction: cannot match `try` expressions", tree.sourcePos)
269+
EmptyTree
270+
case _: Return =>
271+
ctx.error("Implementation restriction: cannot match `return` statements", tree.sourcePos)
272+
EmptyTree
257273
case _ =>
258274
super.transform(tree)
259275
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.quoted._
2+
3+
def f(x: Expr[Any])(using QuoteContext) =
4+
x match {
5+
case '{ class Foo; () } => // error
6+
case '{ object Foo; () } => // error
7+
case '{ 1 match { case _ => () } } => // error
8+
case '{ try 1 finally () } => // error
9+
case '{ try 1 catch { case _ => 4 } } => // error
10+
case '{ Nil.map({ case x: Int => () }) } => // error
11+
case '{ def f: Int = return 2 } => // error
12+
}

tests/pos/quoted-pattern-type.scala

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,6 @@ object Lib {
4242
z: Expr[T & Int]
4343
e
4444

45-
case e @ '{ ($x: Boolean) match { case _ => $y: Int } } =>
46-
e: Expr[T & Int]
47-
y: Expr[T & Int]
48-
e
49-
50-
case e @ '{ ($x: Boolean) match { case _ => $y } } =>
51-
e: Expr[T]
52-
y: Expr[T]
53-
e
54-
55-
case e @ '{ try ($x: Boolean) catch { case _ => $y: Int } } =>
56-
e: Expr[T & (Boolean | Int)]
57-
x: Expr[T & Boolean]
58-
y: Expr[T & Int]
59-
e
60-
6145
}
6246
}
6347
}

0 commit comments

Comments
 (0)