From a3b235cc9888fe3d3a25a97a11c68ed80a257c7b Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Thu, 22 Apr 2021 13:45:49 +0200 Subject: [PATCH 1/2] Fix #12188: Use ascribed type for bindings --- .../tools/dotc/transform/patmat/Space.scala | 4 ++-- .../fatal-warnings/i12188/Macro.scala | 20 +++++++++++++++++++ .../fatal-warnings/i12188/Test.scala | 5 +++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 tests/neg-custom-args/fatal-warnings/i12188/Macro.scala create mode 100644 tests/neg-custom-args/fatal-warnings/i12188/Test.scala diff --git a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala index 001f8a711913..39a3845dc9ef 100644 --- a/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala +++ b/compiler/src/dotty/tools/dotc/transform/patmat/Space.scala @@ -402,8 +402,8 @@ class SpaceEngine(using Context) extends SpaceLogic { case Typed(pat @ UnApply(_, _, _), _) => project(pat) - case Typed(expr, _) => - Typ(erase(expr.tpe.stripAnnots), true) + case Typed(_, tpt) => + Typ(erase(tpt.tpe.stripAnnots), true) case This(_) => Typ(pat.tpe.stripAnnots, false) diff --git a/tests/neg-custom-args/fatal-warnings/i12188/Macro.scala b/tests/neg-custom-args/fatal-warnings/i12188/Macro.scala new file mode 100644 index 000000000000..230443a54eef --- /dev/null +++ b/tests/neg-custom-args/fatal-warnings/i12188/Macro.scala @@ -0,0 +1,20 @@ +import scala.quoted.* + +object MatchTest { + inline def test[T](inline obj: T): Unit = ${testImpl('obj)} + + def testImpl[T](objExpr: Expr[T])(using qctx: Quotes, t: Type[T]): Expr[Unit] = { + import qctx.reflect.* + + val obj = objExpr.asTerm + + val cases = obj.tpe.typeSymbol.children.map { child => + val subtype = TypeIdent(child) + val bind = Symbol.newBind(Symbol.spliceOwner, "c", Flags.EmptyFlags, subtype.tpe) + CaseDef(Bind(bind, Typed(Ref(bind), subtype)), None, '{()}.asTerm) + } + val result = Match(obj, cases) + println(result.show(using Printer.TreeAnsiCode)) + result.asExprOf[Unit] + } +} diff --git a/tests/neg-custom-args/fatal-warnings/i12188/Test.scala b/tests/neg-custom-args/fatal-warnings/i12188/Test.scala new file mode 100644 index 000000000000..55f4157a4132 --- /dev/null +++ b/tests/neg-custom-args/fatal-warnings/i12188/Test.scala @@ -0,0 +1,5 @@ +sealed trait P +case class PC1(a: String) extends P +case class PC2(b: Int) extends P + +def Test = MatchTest.test(PC2(10): P) From 237aaad4d00b17ad35aa0166cd00e1db06cca2d2 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Thu, 22 Apr 2021 14:40:41 +0200 Subject: [PATCH 2/2] Fix CI: Make test fail --- tests/neg-custom-args/fatal-warnings/i12188/Test.scala | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/neg-custom-args/fatal-warnings/i12188/Test.scala b/tests/neg-custom-args/fatal-warnings/i12188/Test.scala index 55f4157a4132..3bea42ac3032 100644 --- a/tests/neg-custom-args/fatal-warnings/i12188/Test.scala +++ b/tests/neg-custom-args/fatal-warnings/i12188/Test.scala @@ -3,3 +3,7 @@ case class PC1(a: String) extends P case class PC2(b: Int) extends P def Test = MatchTest.test(PC2(10): P) + +def foo(x: P): Unit = + x match // error + case _: PC1 => \ No newline at end of file