Skip to content

Commit 94342e1

Browse files
Merge pull request #12190 from dotty-staging/fix-12188
Fix #12188: Use ascribed type for bindings
2 parents 3984c67 + 237aaad commit 94342e1

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ class SpaceEngine(using Context) extends SpaceLogic {
402402
case Typed(pat @ UnApply(_, _, _), _) =>
403403
project(pat)
404404

405-
case Typed(expr, _) =>
406-
Typ(erase(expr.tpe.stripAnnots), true)
405+
case Typed(_, tpt) =>
406+
Typ(erase(tpt.tpe.stripAnnots), true)
407407

408408
case This(_) =>
409409
Typ(pat.tpe.stripAnnots, false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted.*
2+
3+
object MatchTest {
4+
inline def test[T](inline obj: T): Unit = ${testImpl('obj)}
5+
6+
def testImpl[T](objExpr: Expr[T])(using qctx: Quotes, t: Type[T]): Expr[Unit] = {
7+
import qctx.reflect.*
8+
9+
val obj = objExpr.asTerm
10+
11+
val cases = obj.tpe.typeSymbol.children.map { child =>
12+
val subtype = TypeIdent(child)
13+
val bind = Symbol.newBind(Symbol.spliceOwner, "c", Flags.EmptyFlags, subtype.tpe)
14+
CaseDef(Bind(bind, Typed(Ref(bind), subtype)), None, '{()}.asTerm)
15+
}
16+
val result = Match(obj, cases)
17+
println(result.show(using Printer.TreeAnsiCode))
18+
result.asExprOf[Unit]
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sealed trait P
2+
case class PC1(a: String) extends P
3+
case class PC2(b: Int) extends P
4+
5+
def Test = MatchTest.test(PC2(10): P)
6+
7+
def foo(x: P): Unit =
8+
x match // error
9+
case _: PC1 =>

0 commit comments

Comments
 (0)