Skip to content

Commit 13c5455

Browse files
authored
Merge pull request #3400 from maseev/iss1589-underscore-only-for-functions
Add an error message class for a function underscore case
2 parents 31c94c3 + 3ce3433 commit 13c5455

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public enum ErrorMessageID {
105105
ExpectedTypeBoundOrEqualsID,
106106
ClassAndCompanionNameClashID,
107107
TailrecNotApplicableID,
108-
FailureToEliminateExistentialID
108+
FailureToEliminateExistentialID,
109+
OnlyFunctionsCanBeFollowedByUnderscoreID
109110
;
110111

111112
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,4 +1838,13 @@ object messages {
18381838
|type used instead: $tp2"""
18391839
}
18401840
}
1841+
1842+
case class OnlyFunctionsCanBeFollowedByUnderscore(pt: Type)(implicit ctx: Context)
1843+
extends Message(OnlyFunctionsCanBeFollowedByUnderscoreID) {
1844+
val kind = "Syntax"
1845+
val msg = hl"Not a function: $pt: cannot be followed by ${"_"}"
1846+
val explanation =
1847+
hl"""The syntax ${"x _"} is no longer supported if ${"x"} is not a function.
1848+
|To convert to a function value, you need to explicitly write ${"() => x"}"""
1849+
}
18411850
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
15501550
val pt1 = if (defn.isFunctionType(pt)) pt else AnyFunctionProto
15511551
var res = typed(qual, pt1)
15521552
if (pt1.eq(AnyFunctionProto) && !defn.isFunctionClass(res.tpe.classSymbol)) {
1553-
ctx.errorOrMigrationWarning(i"not a function: ${res.tpe}; cannot be followed by `_'", tree.pos)
1553+
ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore(res.tpe), tree.pos)
15541554
if (ctx.scala2Mode) {
15551555
// Under -rewrite, patch `x _` to `(() => x)`
15561556
patch(Position(tree.pos.start), "(() => ")

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,4 +1051,22 @@ class ErrorMessagesTests extends ErrorMessagesTest {
10511051
assertEquals("trait G", other.show)
10521052

10531053
}
1054+
1055+
@Test def onlyFunctionsCanBeFollowedByUnderscore =
1056+
checkMessagesAfter("frontend") {
1057+
"""
1058+
|class T {
1059+
| def main(args: Array[String]): Unit = {
1060+
| val n = "T"
1061+
| val func = n _
1062+
| }
1063+
|}
1064+
""".stripMargin
1065+
}.expect { (ictx, messages) =>
1066+
implicit val ctx: Context = ictx
1067+
1068+
assertMessageCount(1, messages)
1069+
val OnlyFunctionsCanBeFollowedByUnderscore(pt) :: Nil = messages
1070+
assertEquals("String(n)", pt.show)
1071+
}
10541072
}

0 commit comments

Comments
 (0)