@@ -15,6 +15,7 @@ import dotty.tools.dotc.core.NameKinds.PatMatGivenVarName
1515import dotty .tools .dotc .core .Names .*
1616import dotty .tools .dotc .core .StdNames .*
1717import dotty .tools .dotc .core .Symbols .*
18+ import dotty .tools .dotc .core .TypeOps .*
1819import dotty .tools .dotc .core .Types .*
1920import dotty .tools .dotc .reporting .IllegalVariableInPatternAlternative
2021import dotty .tools .dotc .transform .SymUtils ._
@@ -24,6 +25,33 @@ import scala.collection.mutable
2425object QuotePatterns :
2526 import tpd ._
2627
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+
2755 /** Encode the quote pattern into an `unapply` that the pattern matcher can handle.
2856 *
2957 * A quote pattern
0 commit comments