Skip to content
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

Detect non Expr[..] splice patterns #19944

Merged
merged 2 commits into from
Mar 19, 2024
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
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/quoted/QuotePatterns.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ object QuotePatterns:
/** Check for restricted patterns */
def checkPattern(quotePattern: QuotePattern)(using Context): Unit = new tpd.TreeTraverser {
def traverse(tree: Tree)(using Context): Unit = tree match {
case _: SplicePattern =>
case tree: SplicePattern =>
if !tree.body.typeOpt.derivesFrom(defn.QuotedExprClass) then
report.error(i"Splice pattern must match an Expr[...]", tree.body.srcPos)
case tdef: TypeDef if tdef.symbol.isClass =>
val kind = if tdef.symbol.is(Module) then "objects" else "classes"
report.error(em"Implementation restriction: cannot match $kind", tree.srcPos)
Expand Down
4 changes: 4 additions & 0 deletions tests/neg-macros/i19941.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg-macros/i19941.scala:7:14 ---------------------------------------------------------------------------
7 | case '{ ${hd *: tl} : *:[Int, EmptyTuple] } => '{ ??? } // error
| ^^^^^^^^
| Splice pattern must match an Expr[...]
7 changes: 7 additions & 0 deletions tests/neg-macros/i19941.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.*

inline def expandMacro(inline from: Tuple): Any = ${ expandMacroImpl }

def expandMacroImpl(using Quotes): Expr[?] =
'{ 1 *: EmptyTuple } match
case '{ ${hd *: tl} : *:[Int, EmptyTuple] } => '{ ??? } // error
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ object Test {
def impl(receiver: Expr[StringContext])(using qctx: scala.quoted.Quotes) = {
import quotes.reflect.Repeated
receiver match {
case '{ StringContext(${Repeated(parts, tpt)}*) } => // now OK
case '{ StringContext(${Repeated(parts, tpt)}*) } => // error: Repeated is not an Expr pattern
}
}
}