File tree Expand file tree Collapse file tree 4 files changed +30
-2
lines changed
test/dotty/tools/dotc/reporting Expand file tree Collapse file tree 4 files changed +30
-2
lines changed Original file line number Diff line number Diff 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 () {
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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), " (() => " )
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments