Skip to content
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

Deprecation warning for trailing _ to force eta expansion #18926

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 _