Skip to content

Fix #6999: Emit an error when using type splice in quoted val pattern #7582

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 2 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1740,7 +1740,8 @@ object desugar {
new TreeTraverser {
def traverse(tree: untpd.Tree)(implicit ctx: Context): Unit = tree match {
case Splice(expr) => collect(expr)
case TypSplice(expr) => collect(expr)
case TypSplice(expr) =>
ctx.error("Type splices cannot ve used in val patterns. Consider using `match` instead.", tree.sourcePos)
case _ => traverseChildren(tree)
}
}.traverse(expr)
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i6997b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package playground

import scala.quoted.{_, given}, scala.quoted.matching._

inline def mcr(x: => Any): Any = ${mcrImpl('x)}

def mcrImpl(body: Expr[Any])(given ctx: QuoteContext): Expr[Any] = {
val '{$x: $t} = body // error
'{
val tmp: $t = $x.asInstanceOf[$t] // error // error
println(tmp)
tmp
}
}
6 changes: 3 additions & 3 deletions tests/pos/i6997b.scala → tests/pos/i6997c.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import scala.quoted.{_, given}, scala.quoted.matching._
inline def mcr(x: => Any): Any = ${mcrImpl('x)}

def mcrImpl(body: Expr[Any])(given ctx: QuoteContext): Expr[Any] = {
val '{$x: $t} = body
body match case '{$x: $t} =>
'{
val tmp: $t = $x.asInstanceOf[$t]
val tmp: $t = $x
println(tmp)
tmp
}
}
}
30 changes: 15 additions & 15 deletions tests/run-staging/quote-type-matcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ object Test {
def main(args: Array[String]): Unit = withQuoteContext {
val '[List[Int]] = '[List[Int]]

val '[List[$int]] = '[List[Int]]
println(int.show)
println()
'[List[Int]] match
case '[List[$int]] =>
println(int.show)
println()

{
val '[Function1[$t1, $r]] = '[Int => Double]
println(t1.show)
println(r.show)
println()
}
'[Int => Double] match
case '[Function1[$t1, $r]] =>
println(t1.show)
println(r.show)
println()

'[(Int => Short) => Double] match
case '[Function1[Function1[$t1, $r0], $r]] =>
println(t1.show)
println(r0.show)
println(r.show)

{
val '[Function1[Function1[$t1, $r0], $r]] = '[(Int => Short) => Double]
println(t1.show)
println(r0.show)
println(r.show)
}
}
}