@@ -15,6 +15,7 @@ import dotty.tools.dotc.core.NameKinds.PatMatGivenVarName
15
15
import dotty .tools .dotc .core .Names .*
16
16
import dotty .tools .dotc .core .StdNames .*
17
17
import dotty .tools .dotc .core .Symbols .*
18
+ import dotty .tools .dotc .core .TypeOps .*
18
19
import dotty .tools .dotc .core .Types .*
19
20
import dotty .tools .dotc .reporting .IllegalVariableInPatternAlternative
20
21
import dotty .tools .dotc .transform .SymUtils ._
@@ -24,6 +25,33 @@ import scala.collection.mutable
24
25
object QuotePatterns :
25
26
import tpd ._
26
27
28
+ /** Check for restricted patterns */
29
+ def checkPattern (quotePattern : QuotePattern )(using Context ): Unit = new tpd.TreeTraverser {
30
+ def traverse (tree : Tree )(using Context ): Unit = tree match {
31
+ case _ : SplicePattern =>
32
+ case tdef : TypeDef if tdef.symbol.isClass =>
33
+ val kind = if tdef.symbol.is(Module ) then " objects" else " classes"
34
+ report.error(em " Implementation restriction: cannot match $kind" , tree.srcPos)
35
+ case tree : NamedDefTree =>
36
+ if tree.name.is(NameKinds .WildcardParamName ) then
37
+ report.warning(
38
+ " Use of `_` for lambda in quoted pattern. Use explicit lambda instead or use `$_` to match any term." ,
39
+ tree.srcPos)
40
+ if tree.name.isTermName && ! tree.nameSpan.isSynthetic && tree.name != nme.ANON_FUN && tree.name.startsWith(" $" ) then
41
+ report.error(" Names cannot start with $ quote pattern" , tree.namePos)
42
+ traverseChildren(tree)
43
+ case _ : Match =>
44
+ report.error(" Implementation restriction: cannot match `match` expressions" , tree.srcPos)
45
+ case _ : Try =>
46
+ report.error(" Implementation restriction: cannot match `try` expressions" , tree.srcPos)
47
+ case _ : Return =>
48
+ report.error(" Implementation restriction: cannot match `return` statements" , tree.srcPos)
49
+ case _ =>
50
+ traverseChildren(tree)
51
+ }
52
+
53
+ }.traverse(quotePattern.body)
54
+
27
55
/** Encode the quote pattern into an `unapply` that the pattern matcher can handle.
28
56
*
29
57
* A quote pattern
0 commit comments