Skip to content

Commit 2ed8718

Browse files
Merge pull request #7582 from dotty-staging/fix-#6999
Fix #6999: Emit an error when using type splice in quoted val pattern
2 parents 366ae80 + 1bec039 commit 2ed8718

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1743,7 +1743,8 @@ object desugar {
17431743
new TreeTraverser {
17441744
def traverse(tree: untpd.Tree)(implicit ctx: Context): Unit = tree match {
17451745
case Splice(expr) => collect(expr)
1746-
case TypSplice(expr) => collect(expr)
1746+
case TypSplice(expr) =>
1747+
ctx.error("Type splices cannot be used in val patterns. Consider using `match` instead.", tree.sourcePos)
17471748
case _ => traverseChildren(tree)
17481749
}
17491750
}.traverse(expr)

tests/neg/i6997b.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package playground
2+
3+
import scala.quoted.{_, given}, scala.quoted.matching._
4+
5+
inline def mcr(x: => Any): Any = ${mcrImpl('x)}
6+
7+
def mcrImpl(body: Expr[Any])(given ctx: QuoteContext): Expr[Any] = {
8+
val '{$x: $t} = body // error
9+
'{
10+
val tmp: $t = $x.asInstanceOf[$t] // error // error
11+
println(tmp)
12+
tmp
13+
}
14+
}

tests/pos/i6997b.scala renamed to tests/pos/i6997c.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import scala.quoted.{_, given}, scala.quoted.matching._
55
inline def mcr(x: => Any): Any = ${mcrImpl('x)}
66

77
def mcrImpl(body: Expr[Any])(given ctx: QuoteContext): Expr[Any] = {
8-
val '{$x: $t} = body
8+
body match case '{$x: $t} =>
99
'{
10-
val tmp: $t = $x.asInstanceOf[$t]
10+
val tmp: $t = $x
1111
println(tmp)
1212
tmp
1313
}
14-
}
14+
}

tests/run-staging/quote-type-matcher.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ object Test {
77
def main(args: Array[String]): Unit = withQuoteContext {
88
val '[List[Int]] = '[List[Int]]
99

10-
val '[List[$int]] = '[List[Int]]
11-
println(int.show)
12-
println()
10+
'[List[Int]] match
11+
case '[List[$int]] =>
12+
println(int.show)
13+
println()
1314

14-
{
15-
val '[Function1[$t1, $r]] = '[Int => Double]
16-
println(t1.show)
17-
println(r.show)
18-
println()
19-
}
15+
'[Int => Double] match
16+
case '[Function1[$t1, $r]] =>
17+
println(t1.show)
18+
println(r.show)
19+
println()
20+
21+
'[(Int => Short) => Double] match
22+
case '[Function1[Function1[$t1, $r0], $r]] =>
23+
println(t1.show)
24+
println(r0.show)
25+
println(r.show)
2026

21-
{
22-
val '[Function1[Function1[$t1, $r0], $r]] = '[(Int => Short) => Double]
23-
println(t1.show)
24-
println(r0.show)
25-
println(r.show)
26-
}
2727
}
2828
}

0 commit comments

Comments
 (0)