Skip to content

Commit c1c2e3e

Browse files
authored
Merge pull request #8470 from dotty-staging/fix-#7698
Fix #7698: Disallow quoted patterns against non Expr/Type
2 parents a2abd6c + ecee498 commit c1c2e3e

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ trait QuotesAndSplices {
337337
* ```
338338
*/
339339
private def typedQuotePattern(tree: untpd.Quote, pt: Type, qctx: Tree)(implicit ctx: Context): Tree = {
340+
if tree.quoted.isTerm && !pt.derivesFrom(defn.QuotedExprClass) then
341+
ctx.error("Quote pattern can only match scrutinees of type scala.quoted.Expr", tree.sourcePos)
342+
else if tree.quoted.isType && !pt.derivesFrom(defn.QuotedTypeClass) then
343+
ctx.error("Quote pattern can only match scrutinees of type scala.quoted.Type", tree.sourcePos)
344+
340345
val quoted = tree.quoted
341346
val exprPt = pt.baseType(if quoted.isType then defn.QuotedTypeClass else defn.QuotedExprClass)
342347
val quotedPt = exprPt.argInfos.headOption match {

tests/neg/i7698.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.quoted._
2+
import scala.quoted.matching._
3+
4+
trait Show[T] {
5+
def show(x: T): String
6+
}
7+
8+
def showInterpolatorImpl(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext): Expr[String] =
9+
argsExpr.unseal match
10+
case '{ $arg: $t } => // error
11+
case '[ Int ] => // error
12+
???
13+
14+
inline def (sc: => StringContext) show (args: Any*): String = ${ showInterpolatorImpl('sc, 'args) }

tests/pos/i6998.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import scala.quoted._
22

33
def foo(using QuoteContext) : Unit = {
4-
val '{ $f : (Int => Double) } = ???
4+
val '{ $f : (Int => Double) } = ??? : Expr[Any]
55
}

0 commit comments

Comments
 (0)