Skip to content

Commit 74791e0

Browse files
Deprecation warning for trailing _ to force eta expansion (#18926)
This is part of #18867
2 parents 0420aaf + 98df6b5 commit 74791e0

File tree

10 files changed

+61
-23
lines changed

10 files changed

+61
-23
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,26 +2993,28 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
29932993
}
29942994
}
29952995
nestedCtx.typerState.commit()
2996-
if sourceVersion.isAtLeast(future) then
2997-
lazy val (prefix, suffix) = res match {
2998-
case Block(mdef @ DefDef(_, vparams :: Nil, _, _) :: Nil, _: Closure) =>
2999-
val arity = vparams.length
3000-
if (arity > 0) ("", "") else ("(() => ", "())")
3001-
case _ =>
3002-
("(() => ", ")")
3003-
}
3004-
def remedy =
3005-
if ((prefix ++ suffix).isEmpty) "simply leave out the trailing ` _`"
3006-
else s"use `$prefix<function>$suffix` instead"
3007-
report.errorOrMigrationWarning(
3008-
em"""The syntax `<function> _` is no longer supported;
3009-
|you can $remedy""",
3010-
tree.srcPos,
3011-
from = future)
3012-
if sourceVersion.isMigrating then
3013-
patch(Span(tree.span.start), prefix)
3014-
patch(Span(qual.span.end, tree.span.end), suffix)
3015-
end if
2996+
2997+
lazy val (prefix, suffix) = res match {
2998+
case Block(mdef @ DefDef(_, vparams :: Nil, _, _) :: Nil, _: Closure) =>
2999+
val arity = vparams.length
3000+
if (arity > 0) ("", "") else ("(() => ", "())")
3001+
case _ =>
3002+
("(() => ", ")")
3003+
}
3004+
def remedy =
3005+
if ((prefix ++ suffix).isEmpty) "simply leave out the trailing ` _`"
3006+
else s"use `$prefix<function>$suffix` instead"
3007+
def rewrite = Message.rewriteNotice("This construct", `3.4-migration`)
3008+
report.gradualErrorOrMigrationWarning(
3009+
em"""The syntax `<function> _` is no longer supported;
3010+
|you can $remedy$rewrite""",
3011+
tree.srcPos,
3012+
warnFrom = `3.4`,
3013+
errorFrom = future)
3014+
if sourceVersion.isMigrating && sourceVersion.isAtLeast(`3.4-migration`) then
3015+
patch(Span(tree.span.start), prefix)
3016+
patch(Span(qual.span.end, tree.span.end), suffix)
3017+
30163018
res
30173019
}
30183020

language-server/src/dotty/tools/languageserver/DottyLanguageServer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ object DottyLanguageServer {
792792
* not a worksheet, no mapper is necessary. Otherwise, return `toUnwrappedPosition`.
793793
*/
794794
private def positionMapperFor(sourcefile: SourceFile): Option[SourcePosition => SourcePosition] = {
795-
if (isWorksheet(sourcefile)) Some(toUnwrappedPosition _)
795+
if (isWorksheet(sourcefile)) Some(toUnwrappedPosition)
796796
else None
797797
}
798798

tests/neg/i18867-3.4.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//> using options -Werror
2+
3+
import language.`3.4`
4+
5+
def foo(x: Int) = x
6+
7+
def test = foo _ // error

tests/neg/i18867.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Error: tests/neg/i18867.scala:5:15 ----------------------------------------------------------------------------------
2+
5 |def test = foo _ // error
3+
| ^^^^^
4+
| The syntax `<function> _` is no longer supported;
5+
| you can simply leave out the trailing ` _`
6+
| This construct can be rewritten automatically under -rewrite -source 3.4-migration.

tests/neg/i18867.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//> using options -Werror
2+
3+
def foo(x: Int) = x
4+
5+
def test = foo _ // error

tests/patmat/dotty.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object IntEqualityTestTreeMaker {
55
class Test {
66
def isBelow(n: Int, s: String): Boolean = false
77

8-
def foo(xs: List[(Int, String)]): Unit = (xs filter (isBelow _).tupled) match {
8+
def foo(xs: List[(Int, String)]): Unit = xs.filter(isBelow.tupled) match {
99
case Nil =>
1010
case matches =>
1111
}

tests/pos/i18867-3.3.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//> using options -Werror
2+
3+
import language.`3.3`
4+
5+
def foo(x: Int) = x
6+
7+
def test = foo _

tests/pos/i18867-3.4.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import language.`3.4`
2+
3+
def foo(x: Int) = x
4+
5+
def test = foo _ // warn

tests/rewrites/rewrites3x.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ def test =
88
def g = { implicit (x: Int) =>
99
x + 1
1010
}
11+
12+
def foo(x: Int) = x
13+
def testTrailingUnderscoreEtaExpansion = foo

tests/rewrites/rewrites3x.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ def test =
77

88
def g = { implicit x: Int =>
99
x + 1
10-
}
10+
}
11+
12+
def foo(x: Int) = x
13+
def testTrailingUnderscoreEtaExpansion = foo _

0 commit comments

Comments
 (0)