Skip to content

Fix #7698: Disallow quoted patterns against non Expr/Type #8470

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

Merged
merged 1 commit into from
Mar 9, 2020
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
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ trait QuotesAndSplices {
* ```
*/
private def typedQuotePattern(tree: untpd.Quote, pt: Type, qctx: Tree)(implicit ctx: Context): Tree = {
if tree.quoted.isTerm && !pt.derivesFrom(defn.QuotedExprClass) then
ctx.error("Quote pattern can only match scrutinees of type scala.quoted.Expr", tree.sourcePos)
else if tree.quoted.isType && !pt.derivesFrom(defn.QuotedTypeClass) then
ctx.error("Quote pattern can only match scrutinees of type scala.quoted.Type", tree.sourcePos)

val quoted = tree.quoted
val exprPt = pt.baseType(if quoted.isType then defn.QuotedTypeClass else defn.QuotedExprClass)
val quotedPt = exprPt.argInfos.headOption match {
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i7698.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import scala.quoted._
import scala.quoted.matching._

trait Show[T] {
def show(x: T): String
}

def showInterpolatorImpl(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using qctx: QuoteContext): Expr[String] =
argsExpr.unseal match
case '{ $arg: $t } => // error
case '[ Int ] => // error
???

inline def (sc: => StringContext) show (args: Any*): String = ${ showInterpolatorImpl('sc, 'args) }
2 changes: 1 addition & 1 deletion tests/pos/i6998.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import scala.quoted._

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