Skip to content

Commit 9846b5f

Browse files
committed
Look at the most precise type for inline matches
Fixes #11291 Fixes #6781
1 parent 369a465 commit 9846b5f

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,13 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
12911291
if (!tree.isInline || ctx.owner.isInlineMethod) // don't reduce match of nested inline method yet
12921292
super.typedMatchFinish(tree, sel, wideSelType, cases, pt)
12931293
else {
1294-
val selType = if (sel.isEmpty) wideSelType else sel.tpe
1294+
def selTyped(sel: Tree): Type = sel match {
1295+
case Typed(sel2, _) => selTyped(sel2)
1296+
case Block(Nil, sel2) => selTyped(sel2)
1297+
case Inlined(_, Nil, sel2) => selTyped(sel2)
1298+
case _ => sel.tpe
1299+
}
1300+
val selType = if (sel.isEmpty) wideSelType else selTyped(sel)
12951301
reduceInlineMatch(sel, selType, cases.asInstanceOf[List[CaseDef]], this) match {
12961302
case Some((caseBindings, rhs0)) =>
12971303
// drop type ascriptions/casts hiding pattern-bound types (which are now aliases after reducing the match)
@@ -1509,4 +1515,5 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
15091515
case _ => None
15101516
}
15111517

1518+
15121519
}

tests/pos/i11291.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
inline def meth =
2+
val x1 = inline ("a": Any) match
3+
case _: String => "ok"
4+
val x2 = inline { "a": Any } match
5+
case _: String => "ok"
6+
inline s match
7+
case _: String => "ok"
8+
9+
inline def s = "a": Any
10+
11+
def test = meth

tests/pos/i6781.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
enum Nat {
2+
case Zero
3+
case Succ[N <: Nat](n: N)
4+
}
5+
import Nat._
6+
7+
inline def toInt(inline n: Nat): Int = inline n match {
8+
case Zero => 0
9+
case Succ(n1) => toInt(n1) + 1
10+
}
11+
12+
val natTwoA = toInt(Succ[Succ[Zero.type]](Succ(Zero)))
13+
val natTwoB = toInt(Succ(Succ(Zero)): Succ[Succ[Zero.type]])

0 commit comments

Comments
 (0)