Skip to content

Fix #3478: Optimize away unused defs and lazy vals #3840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ object Symbols {
def keysIterator: Iterator[Symbol] = value.keySet().asScala.iterator

def toMap: Map[Symbol, T] = value.asScala.toMap

override def toString: String = value.asScala.toString()
}

@inline def newMutableSymbolMap[T]: MutableSymbolMap[T] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ class Devalify extends Optimisation {
if (replacements.contains(t.symbol))
deepReplacer.transform(replacements(t.symbol)).ensureConforms(t.tpe.widen)
else t
case t: DefDef if !t.symbol.owner.isClass =>
if (timesUsed.getOrElse(t.symbol, 0) + timesUsedAsType.getOrElse(t.symbol, 0) != 0) t
else {
simplify.println(s"Dropping definition of ${t.symbol.showFullName} as not used")
EmptyTree
}
case tree => tree
}

Expand Down
29 changes: 29 additions & 0 deletions compiler/test/dotty/tools/dotc/SimplifyTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,35 @@ abstract class SimplifyTests(val optimise: Boolean) extends DottyBytecodeTest {
|print(8)
""")

@Test def localDefinitionElimination =
check(
"""
|lazy val foo = 1
|def bar = 2
|val baz = 3
""",
"""
""")

@Test def localDefinitionNoElimination =
check(
"""
|val j = 0 // dummy
|class Foo {
| lazy val foo = 1
| def bar = 2
| val baz = 3
|}
""",
"""
|class Foo {
| lazy val foo = 1
| def bar = 2
| val baz = 3
|}
""")


// @Test def listPatmapExample =
// check(
// """
Expand Down