From d92bf6e18816a825c0ff3b7bf5e16d69b22d7e89 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 8 Apr 2020 13:28:30 +0200 Subject: [PATCH] =?UTF-8?q?Improve=20type=20inference=C2=A0of=20quote=20pa?= =?UTF-8?q?ttern=20splices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Infer higher bound for type splices. This means that when type information is not given the pattern should match all term that could fit in this hole. --- .../dotty/tools/dotc/typer/QuotesAndSplices.scala | 2 +- .../quote-matcher-inference/Macro_1.scala | 15 +++++++++++++++ .../quote-matcher-inference/Test_2.scala | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/run-macros/quote-matcher-inference/Macro_1.scala create mode 100644 tests/run-macros/quote-matcher-inference/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index 7a57b67d7f6a..007d49116757 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -73,7 +73,7 @@ trait QuotesAndSplices { case _ => } if (ctx.mode.is(Mode.QuotedPattern)) - if (isFullyDefined(pt, ForceDegree.all)) { + if (isFullyDefined(pt, ForceDegree.flipBottom)) { def spliceOwner(ctx: Context): Symbol = if (ctx.mode.is(Mode.QuotedPattern)) spliceOwner(ctx.outer) else ctx.owner val pat = typedPattern(tree.expr, defn.QuotedExprClass.typeRef.appliedTo(pt))( diff --git a/tests/run-macros/quote-matcher-inference/Macro_1.scala b/tests/run-macros/quote-matcher-inference/Macro_1.scala new file mode 100644 index 000000000000..743e6b9cd452 --- /dev/null +++ b/tests/run-macros/quote-matcher-inference/Macro_1.scala @@ -0,0 +1,15 @@ +import scala.quoted._ + + +object Macros { + + inline def g(inline x: Unit): Unit = ${impl('x)} + + private def impl(x: Expr[Any])(using QuoteContext): Expr[Any] = { + x match + case '{ println(f($y)) } => y + } + +} + +def f[T](x: T): T = x diff --git a/tests/run-macros/quote-matcher-inference/Test_2.scala b/tests/run-macros/quote-matcher-inference/Test_2.scala new file mode 100644 index 000000000000..327112a64868 --- /dev/null +++ b/tests/run-macros/quote-matcher-inference/Test_2.scala @@ -0,0 +1,10 @@ +import Macros._ + + +object Test { + + def main(args: Array[String]): Unit = { + g(println(f((5)))) + } + +}