Skip to content

Add an error message class for a function underscore case #3400

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

Merged
merged 2 commits into from
Oct 30, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public enum ErrorMessageID {
ExpectedTypeBoundOrEqualsID,
ClassAndCompanionNameClashID,
TailrecNotApplicableID,
FailureToEliminateExistentialID
FailureToEliminateExistentialID,
OnlyFunctionsCanBeFollowedByUnderscoreID
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1838,4 +1838,13 @@ object messages {
|type used instead: $tp2"""
}
}

case class OnlyFunctionsCanBeFollowedByUnderscore(pt: Type)(implicit ctx: Context)
extends Message(OnlyFunctionsCanBeFollowedByUnderscoreID) {
val kind = "Syntax"
val msg = hl"Not a function: $pt: cannot be followed by ${"_"}"
val explanation =
hl"""The syntax ${"x _"} is no longer supported if ${"x"} is not a function.
|To convert to a function value, you need to explicitly write ${"() => x"}"""
}
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
val pt1 = if (defn.isFunctionType(pt)) pt else AnyFunctionProto
var res = typed(qual, pt1)
if (pt1.eq(AnyFunctionProto) && !defn.isFunctionClass(res.tpe.classSymbol)) {
ctx.errorOrMigrationWarning(i"not a function: ${res.tpe}; cannot be followed by `_'", tree.pos)
ctx.errorOrMigrationWarning(OnlyFunctionsCanBeFollowedByUnderscore(res.tpe), tree.pos)
if (ctx.scala2Mode) {
// Under -rewrite, patch `x _` to `(() => x)`
patch(Position(tree.pos.start), "(() => ")
Expand Down
18 changes: 18 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1051,4 +1051,22 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals("trait G", other.show)

}

@Test def onlyFunctionsCanBeFollowedByUnderscore =
checkMessagesAfter("frontend") {
"""
|class T {
| def main(args: Array[String]): Unit = {
| val n = "T"
| val func = n _
| }
|}
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

assertMessageCount(1, messages)
val OnlyFunctionsCanBeFollowedByUnderscore(pt) :: Nil = messages
assertEquals("String(n)", pt.show)
}
}