diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index 5c573218da2c..1dc6a71fb842 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -278,6 +278,10 @@ trait QuotesAndSplices { } cpy.AppliedTypeTree(tree)(transform(tpt), args1) case tree: NamedDefTree => + if tree.name.is(NameKinds.WildcardParamName) then + report.warning( + "Use of `_` for lambda in quoted pattern. Use explicit lambda instead or use `$_` to match any term.", + tree.srcPos) if tree.name.isTermName && !tree.nameSpan.isSynthetic && tree.name.startsWith("$") then report.error("Names cannot start with $ quote pattern ", tree.namePos) super.transform(tree) diff --git a/compiler/test/dotty/tools/dotc/BootstrappedOnlyCompilationTests.scala b/compiler/test/dotty/tools/dotc/BootstrappedOnlyCompilationTests.scala index 93b9fa6b68b5..7577ca411a7a 100644 --- a/compiler/test/dotty/tools/dotc/BootstrappedOnlyCompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/BootstrappedOnlyCompilationTests.scala @@ -104,7 +104,10 @@ class BootstrappedOnlyCompilationTests extends ParallelTesting { @Test def negMacros: Unit = { implicit val testGroup: TestGroup = TestGroup("compileNegWithCompiler") - compileFilesInDir("tests/neg-macros", defaultOptions).checkExpectedErrors() + aggregateTests( + compileFilesInDir("tests/neg-macros", defaultOptions), + compileFile("tests/pos-macros/i9570.scala", defaultOptions.and("-Xfatal-warnings")), + ).checkExpectedErrors() } @Test def negWithCompiler: Unit = { diff --git a/tests/pos-macros/i9570.scala b/tests/pos-macros/i9570.scala new file mode 100644 index 000000000000..64aadaf16882 --- /dev/null +++ b/tests/pos-macros/i9570.scala @@ -0,0 +1,24 @@ +import scala.quoted._ + +object Macros { + + object HList { + sealed trait HList + case class HCons[+HD, TL <: HList](hd: HD, tl: TL) extends HList + case object HNil extends HList + + private def sizeImpl(e: Expr[HList], n:Int)(using qctx:QuoteContext): Expr[Int] = { + import qctx.tasty._ + e match { + case '{HCons(_,$t)} => // error if run with fatal warinings in BootstrappedOnlyCompilationTests + sizeImpl(t,n+1) + case '{HNil} => Expr(n) + } + } + + inline def size(inline expr: HList ): Int = { + ${sizeImpl('expr,0)} + } + + } +} diff --git a/tests/run-macros/i9570/Macro_1.scala b/tests/run-macros/i9570/Macro_1.scala new file mode 100644 index 000000000000..668f91a9deff --- /dev/null +++ b/tests/run-macros/i9570/Macro_1.scala @@ -0,0 +1,25 @@ +import scala.quoted._ + +object Macros { + + object HList { + sealed trait HList + case class HCons[+HD, TL <: HList](hd: HD, tl: TL) extends HList + case object HNil extends HList + + private def sizeImpl(e: Expr[HList], n:Int)(using qctx:QuoteContext): Expr[Int] = { + import qctx.tasty._ + e match { + case '{HCons($_,$t)} => + //case '{HCons($a,$t)} => + sizeImpl(t,n+1) + case '{HNil} => Expr(n) + } + } + + inline def size(inline expr: HList ): Int = { + ${sizeImpl('expr,0)} + } + + } +} diff --git a/tests/run-macros/i9570/Test_2.scala b/tests/run-macros/i9570/Test_2.scala new file mode 100644 index 000000000000..e8ddad50d187 --- /dev/null +++ b/tests/run-macros/i9570/Test_2.scala @@ -0,0 +1,9 @@ +object Test { + + def main(args: Array[String]): Unit = { + import Macros.HList._ + + val hl0n = size( HCons(("1",1), HCons(("2",2), HCons(("3",3),HNil))) ) + println(s"size(?) = $hl0n") + } +} \ No newline at end of file