Skip to content

Commit 7550c7b

Browse files
committed
Fix #9570: Warn on use of _ in quoted pattern
1 parent 64a239f commit 7550c7b

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ trait QuotesAndSplices {
278278
}
279279
cpy.AppliedTypeTree(tree)(transform(tpt), args1)
280280
case tree: NamedDefTree =>
281+
if tree.name.is(NameKinds.WildcardParamName) then
282+
report.warning(
283+
"Use of `_` for lambda in quoted pattern. Use explicit lambda instead or use `$_` to match any term.",
284+
tree.srcPos)
281285
if tree.name.isTermName && !tree.nameSpan.isSynthetic && tree.name.startsWith("$") then
282286
report.error("Names cannot start with $ quote pattern ", tree.namePos)
283287
super.transform(tree)

compiler/test/dotty/tools/dotc/BootstrappedOnlyCompilationTests.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ class BootstrappedOnlyCompilationTests extends ParallelTesting {
104104

105105
@Test def negMacros: Unit = {
106106
implicit val testGroup: TestGroup = TestGroup("compileNegWithCompiler")
107-
compileFilesInDir("tests/neg-macros", defaultOptions).checkExpectedErrors()
107+
aggregateTests(
108+
compileFilesInDir("tests/neg-macros", defaultOptions),
109+
compileFile("tests/pos-macros/i9570.scala", defaultOptions.and("-Xfatal-warnings")),
110+
).checkExpectedErrors()
108111
}
109112

110113
@Test def negWithCompiler: Unit = {

tests/pos-macros/i9570.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import scala.quoted._
2+
3+
object Macros {
4+
5+
object HList {
6+
sealed trait HList
7+
case class HCons[+HD, TL <: HList](hd: HD, tl: TL) extends HList
8+
case object HNil extends HList
9+
10+
private def sizeImpl(e: Expr[HList], n:Int)(using qctx:QuoteContext): Expr[Int] = {
11+
import qctx.tasty._
12+
e match {
13+
case '{HCons(_,$t)} => // error if run with fatal warinings in BootstrappedOnlyCompilationTests
14+
sizeImpl(t,n+1)
15+
case '{HNil} => Expr(n)
16+
}
17+
}
18+
19+
inline def size(inline expr: HList ): Int = {
20+
${sizeImpl('expr,0)}
21+
}
22+
23+
}
24+
}

tests/run-macros/i9570/Macro_1.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.quoted._
2+
3+
object Macros {
4+
5+
object HList {
6+
sealed trait HList
7+
case class HCons[+HD, TL <: HList](hd: HD, tl: TL) extends HList
8+
case object HNil extends HList
9+
10+
private def sizeImpl(e: Expr[HList], n:Int)(using qctx:QuoteContext): Expr[Int] = {
11+
import qctx.tasty._
12+
e match {
13+
case '{HCons($_,$t)} =>
14+
//case '{HCons($a,$t)} =>
15+
sizeImpl(t,n+1)
16+
case '{HNil} => Expr(n)
17+
}
18+
}
19+
20+
inline def size(inline expr: HList ): Int = {
21+
${sizeImpl('expr,0)}
22+
}
23+
24+
}
25+
}

tests/run-macros/i9570/Test_2.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Test {
2+
3+
def main(args: Array[String]): Unit = {
4+
import Macros.HList._
5+
6+
val hl0n = size( HCons(("1",1), HCons(("2",2), HCons(("3",3),HNil))) )
7+
println(s"size(?) = $hl0n")
8+
}
9+
}

0 commit comments

Comments
 (0)