Skip to content

Convert "method has return statement; needs result type" to new error format with MissingReturnTypeWithReturnStatement #3094

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
Show file tree
Hide file tree
Changes from 2 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 @@ -96,6 +96,7 @@ public enum ErrorMessageID {
WrongNumberOfParametersID,
DuplicatePrivateProtectedQualifierID,
ExpectedStartOfTopLevelDefinitionID,
MissingReturnTypeWithReturnStatementID,
;

public int errorNumber() {
Expand Down
11 changes: 11 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,17 @@ object messages {
|}"""
}

case class MissingReturnTypeWithReturnStatement(method: Symbol)(implicit ctx: Context)
extends Message(MissingReturnTypeWithReturnStatementID) {
val kind = "Syntax"
val msg = hl"$method has a return statement; it needs a result type"
val explanation =
hl"""|If a method contains a ${"return"} statement, it must have an
|explicit return type. For example:
|
|${"def good: Int /* explicit return type */ = return 1"}"""
}

case class YieldOrDoExpectedInForComprehension()(implicit ctx: Context)
extends Message(YieldOrDoExpectedInForComprehensionID) {
val kind = "Syntax"
Expand Down
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 @@ -987,7 +987,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
if (owner.isInlineMethod)
(EmptyTree, errorType(em"no explicit return allowed from inline $owner", tree.pos))
else if (!owner.isCompleted)
(EmptyTree, errorType(em"$owner has return statement; needs result type", tree.pos))
(EmptyTree, errorType(MissingReturnTypeWithReturnStatement(owner), tree.pos))
else {
val from = Ident(TermRef(NoPrefix, owner.asTerm))
val proto = returnProto(owner, cx.scope)
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -955,4 +955,17 @@ class ErrorMessagesTests extends ErrorMessagesTest {

assertEquals(ExpectedStartOfTopLevelDefinition(), err)
}

@Test def missingReturnTypeWithReturnStatement =
checkMessagesAfter("frontend") {
"""class BadFunction {
| def bad() = { return "fail" }
|}
""".stripMargin
}.expect { (ictx, messages) =>
assertMessageCount(1, messages)

val MissingReturnTypeWithReturnStatement(method) :: Nil = messages
assertEquals(method.show, "bad")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be method.name.show

}
}