Skip to content

Commit

Permalink
Deprecation warning for trailing _ to force eta expansion (#18926)
Browse files Browse the repository at this point in the history
This is part of #18867
  • Loading branch information
nicolasstucki committed Nov 16, 2023
2 parents 0420aaf + 98df6b5 commit 74791e0
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 23 deletions.
42 changes: 22 additions & 20 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Expand Up @@ -2993,26 +2993,28 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
}
}
nestedCtx.typerState.commit()
if sourceVersion.isAtLeast(future) then
lazy val (prefix, suffix) = res match {
case Block(mdef @ DefDef(_, vparams :: Nil, _, _) :: Nil, _: Closure) =>
val arity = vparams.length
if (arity > 0) ("", "") else ("(() => ", "())")
case _ =>
("(() => ", ")")
}
def remedy =
if ((prefix ++ suffix).isEmpty) "simply leave out the trailing ` _`"
else s"use `$prefix<function>$suffix` instead"
report.errorOrMigrationWarning(
em"""The syntax `<function> _` is no longer supported;
|you can $remedy""",
tree.srcPos,
from = future)
if sourceVersion.isMigrating then
patch(Span(tree.span.start), prefix)
patch(Span(qual.span.end, tree.span.end), suffix)
end if

lazy val (prefix, suffix) = res match {
case Block(mdef @ DefDef(_, vparams :: Nil, _, _) :: Nil, _: Closure) =>
val arity = vparams.length
if (arity > 0) ("", "") else ("(() => ", "())")
case _ =>
("(() => ", ")")
}
def remedy =
if ((prefix ++ suffix).isEmpty) "simply leave out the trailing ` _`"
else s"use `$prefix<function>$suffix` instead"
def rewrite = Message.rewriteNotice("This construct", `3.4-migration`)
report.gradualErrorOrMigrationWarning(
em"""The syntax `<function> _` is no longer supported;
|you can $remedy$rewrite""",
tree.srcPos,
warnFrom = `3.4`,
errorFrom = future)
if sourceVersion.isMigrating && sourceVersion.isAtLeast(`3.4-migration`) then
patch(Span(tree.span.start), prefix)
patch(Span(qual.span.end, tree.span.end), suffix)

res
}

Expand Down
Expand Up @@ -792,7 +792,7 @@ object DottyLanguageServer {
* not a worksheet, no mapper is necessary. Otherwise, return `toUnwrappedPosition`.
*/
private def positionMapperFor(sourcefile: SourceFile): Option[SourcePosition => SourcePosition] = {
if (isWorksheet(sourcefile)) Some(toUnwrappedPosition _)
if (isWorksheet(sourcefile)) Some(toUnwrappedPosition)
else None
}

Expand Down
7 changes: 7 additions & 0 deletions tests/neg/i18867-3.4.scala
@@ -0,0 +1,7 @@
//> using options -Werror

import language.`3.4`

def foo(x: Int) = x

def test = foo _ // error
6 changes: 6 additions & 0 deletions tests/neg/i18867.check
@@ -0,0 +1,6 @@
-- Error: tests/neg/i18867.scala:5:15 ----------------------------------------------------------------------------------
5 |def test = foo _ // error
| ^^^^^
| The syntax `<function> _` is no longer supported;
| you can simply leave out the trailing ` _`
| This construct can be rewritten automatically under -rewrite -source 3.4-migration.
5 changes: 5 additions & 0 deletions tests/neg/i18867.scala
@@ -0,0 +1,5 @@
//> using options -Werror

def foo(x: Int) = x

def test = foo _ // error
2 changes: 1 addition & 1 deletion tests/patmat/dotty.scala
Expand Up @@ -5,7 +5,7 @@ object IntEqualityTestTreeMaker {
class Test {
def isBelow(n: Int, s: String): Boolean = false

def foo(xs: List[(Int, String)]): Unit = (xs filter (isBelow _).tupled) match {
def foo(xs: List[(Int, String)]): Unit = xs.filter(isBelow.tupled) match {
case Nil =>
case matches =>
}
Expand Down
7 changes: 7 additions & 0 deletions tests/pos/i18867-3.3.scala
@@ -0,0 +1,7 @@
//> using options -Werror

import language.`3.3`

def foo(x: Int) = x

def test = foo _
5 changes: 5 additions & 0 deletions tests/pos/i18867-3.4.scala
@@ -0,0 +1,5 @@
import language.`3.4`

def foo(x: Int) = x

def test = foo _ // warn
3 changes: 3 additions & 0 deletions tests/rewrites/rewrites3x.check
Expand Up @@ -8,3 +8,6 @@ def test =
def g = { implicit (x: Int) =>
x + 1
}

def foo(x: Int) = x
def testTrailingUnderscoreEtaExpansion = foo
5 changes: 4 additions & 1 deletion tests/rewrites/rewrites3x.scala
Expand Up @@ -7,4 +7,7 @@ def test =

def g = { implicit x: Int =>
x + 1
}
}

def foo(x: Int) = x
def testTrailingUnderscoreEtaExpansion = foo _

0 comments on commit 74791e0

Please sign in to comment.