From bc3e536e7dda1dc3b347ca470e076fe148f42bd4 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 22 Apr 2020 13:28:04 +0200 Subject: [PATCH] Fix #8746: Fix quote matcher block normalization --- library/src/scala/internal/quoted/Matcher.scala | 5 ++++- tests/run/i8746/Macro_1.scala | 10 ++++++++++ tests/run/i8746/Test_2.scala | 5 +++++ tests/run/i8746b/Macro_1.scala | 12 ++++++++++++ tests/run/i8746b/Test_2.scala | 7 +++++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/run/i8746/Macro_1.scala create mode 100644 tests/run/i8746/Test_2.scala create mode 100644 tests/run/i8746b/Macro_1.scala create mode 100644 tests/run/i8746b/Test_2.scala diff --git a/library/src/scala/internal/quoted/Matcher.scala b/library/src/scala/internal/quoted/Matcher.scala index deb412f60405..f52860bcae9b 100644 --- a/library/src/scala/internal/quoted/Matcher.scala +++ b/library/src/scala/internal/quoted/Matcher.scala @@ -202,7 +202,10 @@ private[quoted] object Matcher { /** Normalize the tree */ def normalize(tree: Tree): Tree = tree match { case Block(Nil, expr) => normalize(expr) - case Block(stats1, Block(stats2, expr)) => normalize(Block(stats1 ::: stats2, expr)) + case Block(stats1, Block(stats2, expr)) => + expr match + case _: Closure => tree + case _ => normalize(Block(stats1 ::: stats2, expr)) case Inlined(_, Nil, expr) => normalize(expr) case _ => tree } diff --git a/tests/run/i8746/Macro_1.scala b/tests/run/i8746/Macro_1.scala new file mode 100644 index 000000000000..66902e3203cf --- /dev/null +++ b/tests/run/i8746/Macro_1.scala @@ -0,0 +1,10 @@ +import scala.quoted._ + +object Macro { + inline def mac(): String = ${ macImpl() } + def macImpl()(using qctx: QuoteContext): Expr[String] = + '{(x: String) => "anything"} match + case '{ (in: String) => ($out: $tpe2) } => Expr(out.toString) + case _ => ??? + +} diff --git a/tests/run/i8746/Test_2.scala b/tests/run/i8746/Test_2.scala new file mode 100644 index 000000000000..9a3dce5970d0 --- /dev/null +++ b/tests/run/i8746/Test_2.scala @@ -0,0 +1,5 @@ +import Macro._ + +@main def Test() = { // hello + mac() +} diff --git a/tests/run/i8746b/Macro_1.scala b/tests/run/i8746b/Macro_1.scala new file mode 100644 index 000000000000..85d52dbcde28 --- /dev/null +++ b/tests/run/i8746b/Macro_1.scala @@ -0,0 +1,12 @@ + +import scala.quoted._ + +object Macro { + inline def mac(inline tree: Any): String = ${ macImpl('tree) } + def macImpl(tree: Expr[Any])(using qctx: QuoteContext): Expr[String] = { + tree match { + case '{ ($in: $tpe1) => ($out: $tpe2) } => Expr(out.toString) + case _ => Expr("not matched") + } + } +} diff --git a/tests/run/i8746b/Test_2.scala b/tests/run/i8746b/Test_2.scala new file mode 100644 index 000000000000..251549d10349 --- /dev/null +++ b/tests/run/i8746b/Test_2.scala @@ -0,0 +1,7 @@ +import Macro._ + +import Macro._ + +@main def Test() = { //hello + mac((x: String) => "anything") +}