Skip to content

Commit f90da9e

Browse files
Merge pull request #3840 from dotty-staging/fix-#3478
Fix #3478: Optimize away unused defs and lazy vals
2 parents 4e4cddb + 2b7479f commit f90da9e

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

compiler/src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ object Symbols {
743743
def keysIterator: Iterator[Symbol] = value.keySet().asScala.iterator
744744

745745
def toMap: Map[Symbol, T] = value.asScala.toMap
746+
747+
override def toString: String = value.asScala.toString()
746748
}
747749

748750
@inline def newMutableSymbolMap[T]: MutableSymbolMap[T] =

compiler/src/dotty/tools/dotc/transform/localopt/Devalify.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ class Devalify extends Optimisation {
159159
if (replacements.contains(t.symbol))
160160
deepReplacer.transform(replacements(t.symbol)).ensureConforms(t.tpe.widen)
161161
else t
162+
case t: DefDef if !t.symbol.owner.isClass =>
163+
if (timesUsed.getOrElse(t.symbol, 0) + timesUsedAsType.getOrElse(t.symbol, 0) != 0) t
164+
else {
165+
simplify.println(s"Dropping definition of ${t.symbol.showFullName} as not used")
166+
EmptyTree
167+
}
162168
case tree => tree
163169
}
164170

compiler/test/dotty/tools/dotc/SimplifyTests.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,35 @@ abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
164164
|print(8)
165165
""")
166166

167+
@Test def localDefinitionElimination =
168+
check(
169+
"""
170+
|lazy val foo = 1
171+
|def bar = 2
172+
|val baz = 3
173+
""",
174+
"""
175+
""")
176+
177+
@Test def localDefinitionNoElimination =
178+
check(
179+
"""
180+
|val j = 0 // dummy
181+
|class Foo {
182+
| lazy val foo = 1
183+
| def bar = 2
184+
| val baz = 3
185+
|}
186+
""",
187+
"""
188+
|class Foo {
189+
| lazy val foo = 1
190+
| def bar = 2
191+
| val baz = 3
192+
|}
193+
""")
194+
195+
167196
// @Test def listPatmapExample =
168197
// check(
169198
// """

0 commit comments

Comments
 (0)