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 {
105
105
ExpectedTypeBoundOrEqualsID ,
106
106
ClassAndCompanionNameClashID ,
107
107
TailrecNotApplicableID ,
108
- FailureToEliminateExistentialID
108
+ FailureToEliminateExistentialID ,
109
+ OnlyFunctionsCanBeFollowedByUnderscoreID
109
110
;
110
111
111
112
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -1838,4 +1838,13 @@ object messages {
1838
1838
|type used instead: $tp2"""
1839
1839
}
1840
1840
}
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
+ }
1841
1850
}
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
1550
1550
val pt1 = if (defn.isFunctionType(pt)) pt else AnyFunctionProto
1551
1551
var res = typed(qual, pt1)
1552
1552
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)
1554
1554
if (ctx.scala2Mode) {
1555
1555
// Under -rewrite, patch `x _` to `(() => x)`
1556
1556
patch(Position (tree.pos.start), " (() => " )
Original file line number Diff line number Diff line change @@ -1051,4 +1051,22 @@ class ErrorMessagesTests extends ErrorMessagesTest {
1051
1051
assertEquals(" trait G" , other.show)
1052
1052
1053
1053
}
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
+ }
1054
1072
}
You can’t perform that action at this time.
0 commit comments