Skip to content

Commit 3f5c9b4

Browse files
committed
Count references correctly when dropping unused defs
When dropping unused definitions during inlining we must also count references to terms which are reachable via the types of RefTrees. Fixes #5572.
1 parent c444f1a commit 3f5c9b4

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -954,13 +954,18 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
954954
override def traverse(t: Tree)(implicit ctx: Context) = {
955955
def updateRefCount(sym: Symbol, inc: Int) =
956956
for (x <- refCount.get(sym)) refCount(sym) = x + inc
957+
def updateTermRefCounts(t: Tree) =
958+
t.typeOpt.foreachPart {
959+
case ref: TermRef => updateRefCount(ref.symbol, 2) // can't be inlined, so make sure refCount is at least 2
960+
case _ =>
961+
}
962+
957963
t match {
958-
case t: RefTree => updateRefCount(t.symbol, 1)
964+
case t: RefTree =>
965+
updateRefCount(t.symbol, 1)
966+
updateTermRefCounts(t)
959967
case _: New | _: TypeTree =>
960-
t.typeOpt.foreachPart {
961-
case ref: TermRef => updateRefCount(ref.symbol, 2) // can't be inlined, so make sure refCount is at least 2
962-
case _ =>
963-
}
968+
updateTermRefCounts(t)
964969
case _ =>
965970
}
966971
traverseChildren(t)

tests/pos/i5572.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
trait Foo {
2+
type Id[t] = t
3+
inline def foo[T](t: T) <: Id[T] =
4+
inline t match {
5+
case i: Int => (i+1).asInstanceOf[Id[T]]
6+
case _ => t
7+
}
8+
}
9+
10+
object Bar extends Foo
11+
12+
object Test {
13+
Bar.foo(23)
14+
}

0 commit comments

Comments
 (0)