Skip to content

Fix #8881: Lift lets over applications #8918

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 5 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Compiler {
new FunctionXXLForwarders, // Add forwarders for FunctionXXL apply method
new ParamForwarding, // Add forwarders for aliases of superclass parameters
new TupleOptimizations, // Optimize generic operations on tuples
new LetOverApply, // Lift blocks from receivers of applications
new ArrayConstructors) :: // Intercept creation of (non-generic) arrays and intrinsify.
List(new Erasure) :: // Rewrite types to JVM model, erasing all type parameters, abstract types and refinements.
List(new ElimErasedValueType, // Expand erased value types to their underlying implmementation types
Expand Down
37 changes: 37 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/LetOverApply.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dotty.tools
package dotc
package transform

import core._
import MegaPhase._
import Contexts.Context
import Symbols._, Decorators._, Types._
import ast.Trees._
import dotty.tools.dotc.ast.tpd


import scala.collection.immutable.::


/** Rewrite `{ stats; expr}.f(args) }` to `{ stats; expr.f(args) }` before
* proceeding, but leave closures alone. This is necessary to be able to
* collapse applies of IFTs.
*/
class LetOverApply extends MiniPhase:
import ast.tpd._

override def phaseName: String = "letOverApply"

override def transformApply(tree: tpd.Apply)(using Context): tpd.Tree =
tree.fun match
case Select(blk @ Block(stats, expr), name) if !expr.isInstanceOf[Closure] =>
cpy.Block(blk)(stats,
cpy.Apply(tree)(
cpy.Select(tree.fun)(expr, name), tree.args))
case Block(stats, expr) =>
cpy.Block(tree.fun)(stats,
cpy.Apply(tree)(expr, tree.args))
case _ =>
tree

end LetOverApply
5 changes: 5 additions & 0 deletions tests/pos/i8881.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Crash {
def f(a: String, b: String, c: Int = 0): Int ?=> String = ""
given Int = ???
f(b = "b", a = "a")
}