Skip to content

Fix #3246: Refine handling of postfix _ for non-functions #3300

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
24 changes: 14 additions & 10 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1548,17 +1548,21 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def typedAsFunction(tree: untpd.PostfixOp, pt: Type)(implicit ctx: Context): Tree = {
val untpd.PostfixOp(qual, Ident(nme.WILDCARD)) = tree
val pt1 = if (defn.isFunctionType(pt)) pt else AnyFunctionProto
var res = typed(qual, pt1)
if (pt1.eq(AnyFunctionProto) && !defn.isFunctionClass(res.tpe.classSymbol)) {
ctx.errorOrMigrationWarning(i"not a function: ${res.tpe}; cannot be followed by `_'", tree.pos)
if (ctx.scala2Mode) {
// Under -rewrite, patch `x _` to `(() => x)`
patch(Position(tree.pos.start), "(() => ")
patch(Position(qual.pos.end, tree.pos.end), ")")
res = typed(untpd.Function(Nil, untpd.TypedSplice(res)))
}
val nestedCtx = ctx.fresh.setNewTyperState()
val res = typed(qual, pt1)(nestedCtx)
res match {
case res @ closure(_, _, _) =>
case _ =>
ctx.errorOrMigrationWarning(i"not a function: $qual; cannot be followed by `_'", tree.pos)
if (ctx.scala2Mode) {
// Under -rewrite, patch `x _` to `(() => x)`
patch(Position(tree.pos.start), "(() => ")
patch(Position(qual.pos.end, tree.pos.end), ")")
return typed(untpd.Function(Nil, qual), pt)
}
}
else if (ctx.settings.strict.value) {
nestedCtx.typerState.commit()
if (ctx.settings.strict.value) {
lazy val (prefix, suffix) = res match {
case Block(mdef @ DefDef(_, _, vparams :: Nil, _, _) :: Nil, _: Closure) =>
val arity = vparams.length
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i3246.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Test {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should also compile it under scala2 mode, since it only crashed in this mode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK. Can you make the change and merge the PR when done? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. Will do

def foo(x: Int) = 1
val bar: () => Int = foo _
}
7 changes: 7 additions & 0 deletions tests/pos-scala2/i3246.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Test {
def foo(x: => Int) = bar(x _)
def bar(x: () => Int) = ???
def baz = 1
def bam: () => Int = baz _
def ban: () => Int = 1 _
}