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 1 commit
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
Next Next commit
Convert "method has return statement; needs result type" to new error…
… format with MissingReturnTypeWithReturnStatement
  • Loading branch information
TheReturningVoid committed Sep 9, 2017
commit 0988ffa829559e905dd03e688364c4301d9cdd0c
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
15 changes: 15 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,21 @@ 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 a return
|type explicitly given to it. For example:
Copy link
Contributor

Choose a reason for hiding this comment

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

...it must have an explicit result type. For example:

|
|${"def bad() = { return \"fail\" }"}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would give a valid example instead:

def good: Int /* explicit result type */ = {
  return 1
}

|
|This is illegal because bad does not have a return type given
|to it. Giving bad a return type of ${"String"} will resolve
|this error."""
}

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
17 changes: 17 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,21 @@ class ErrorMessagesTests extends ErrorMessagesTest {

assertEquals(ExpectedStartOfTopLevelDefinition(), err)
}

@Test def missingReturnTypeWithReturnStatement =
checkMessagesAfter("frontend") {
"""class BadFunction {
| def bad() = { return "fail" }
|}
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
val defn = ictx.definitions
Copy link
Contributor

Choose a reason for hiding this comment

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

not needed


assertMessageCount(1, messages)

val MissingReturnTypeWithReturnStatement(method) :: Nil = messages

assertEquals(method.name.toString, "bad")
Copy link
Contributor

Choose a reason for hiding this comment

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

name.show

}
}