Skip to content

Commit 51388eb

Browse files
TheReturningVoidallanrenucci
authored andcommitted
Convert "method has return statement; needs result type" error to error message (#3094)
Convert "method has return statement; needs result type" to new error format with MissingReturnTypeWithReturnStatement
1 parent 68907a5 commit 51388eb

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public enum ErrorMessageID {
9696
WrongNumberOfParametersID,
9797
DuplicatePrivateProtectedQualifierID,
9898
ExpectedStartOfTopLevelDefinitionID,
99+
MissingReturnTypeWithReturnStatementID,
99100
;
100101

101102
public int errorNumber() {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,17 @@ object messages {
598598
|}"""
599599
}
600600

601+
case class MissingReturnTypeWithReturnStatement(method: Symbol)(implicit ctx: Context)
602+
extends Message(MissingReturnTypeWithReturnStatementID) {
603+
val kind = "Syntax"
604+
val msg = hl"$method has a return statement; it needs a result type"
605+
val explanation =
606+
hl"""|If a method contains a ${"return"} statement, it must have an
607+
|explicit return type. For example:
608+
|
609+
|${"def good: Int /* explicit return type */ = return 1"}"""
610+
}
611+
601612
case class YieldOrDoExpectedInForComprehension()(implicit ctx: Context)
602613
extends Message(YieldOrDoExpectedInForComprehensionID) {
603614
val kind = "Syntax"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
987987
if (owner.isInlineMethod)
988988
(EmptyTree, errorType(em"no explicit return allowed from inline $owner", tree.pos))
989989
else if (!owner.isCompleted)
990-
(EmptyTree, errorType(em"$owner has return statement; needs result type", tree.pos))
990+
(EmptyTree, errorType(MissingReturnTypeWithReturnStatement(owner), tree.pos))
991991
else {
992992
val from = Ident(TermRef(NoPrefix, owner.asTerm))
993993
val proto = returnProto(owner, cx.scope)

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,4 +955,19 @@ class ErrorMessagesTests extends ErrorMessagesTest {
955955

956956
assertEquals(ExpectedStartOfTopLevelDefinition(), err)
957957
}
958+
959+
@Test def missingReturnTypeWithReturnStatement =
960+
checkMessagesAfter("frontend") {
961+
"""class BadFunction {
962+
| def bad() = { return "fail" }
963+
|}
964+
""".stripMargin
965+
}.expect { (ictx, messages) =>
966+
implicit val ctx: Context = ictx
967+
968+
assertMessageCount(1, messages)
969+
970+
val MissingReturnTypeWithReturnStatement(method) :: Nil = messages
971+
assertEquals(method.name.show, "bad")
972+
}
958973
}

0 commit comments

Comments
 (0)