Skip to content

Fix #1503: anonymous application of FunctionX #1508

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

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 10 additions & 10 deletions src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -601,16 +601,16 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
case _ =>
tryEither {
implicit ctx => simpleApply(fun1, proto)
} {
(failedVal, failedState) =>
def fail = { failedState.commit(); failedVal }
// Try once with original prototype and once (if different) with tupled one.
// The reason we need to try both is that the decision whether to use tupled
// or not was already taken but might have to be revised when an implicit
// is inserted on the qualifier.
tryWithImplicitOnQualifier(fun1, originalProto).getOrElse(
if (proto eq originalProto) fail
else tryWithImplicitOnQualifier(fun1, proto).getOrElse(fail))
} { (failedVal, failedState) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style changes should be in separate commits from functional changes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

def fail = { failedState.commit(); failedVal }
// Try once with original prototype and once (if different) with tupled one.
// The reason we need to try both is that the decision whether to use tupled
// or not was already taken but might have to be revised when an implicit
// is inserted on the qualifier.
tryWithImplicitOnQualifier(fun1, originalProto) getOrElse {
if (proto eq originalProto) fail
else tryWithImplicitOnQualifier(fun1, proto).getOrElse(fail)
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,16 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def typedBlock(tree: untpd.Block, pt: Type)(implicit ctx: Context) = track("typedBlock") {
val exprCtx = index(tree.stats)
val stats1 = typedStats(tree.stats, ctx.owner)
val expr1 = typedExpr(tree.expr, pt)(exprCtx)

// A block might appear in function position after desugaring (see
// i1503.scala), so adaptation may insert a `.apply` at the end of expr1.
// When this happens we simply remove the select to make expr1 a valid
// block expression. The `.apply` will be added back around the block when
// the block itself is adapted later.
val expr1 = typedExpr(tree.expr, pt)(exprCtx) match {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But what if the .apply was added explicitly by the user? Then it should not be removed. It seems we need another solution.

case Select(x, nme.apply) => x
case x => x
}
ensureNoLocalRefs(
assignType(cpy.Block(tree)(stats1, expr1), stats1, expr1), pt, localSyms(stats1))
}
Expand Down
1 change: 1 addition & 0 deletions tests/run/i1503.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
working
7 changes: 7 additions & 0 deletions tests/run/i1503.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Test {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put the run test in the same commit where the problem is fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, was planning on cleaning up this PR this morning - thanks for having a look already :)

def main(args: Array[String]): Unit = {
(new Function0[Unit] {
def apply() = println("working")
})()
}
}