Skip to content

move 'return outside method definition' to error class #3193

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 1 commit into from
Sep 27, 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 @@ -98,6 +98,7 @@ public enum ErrorMessageID {
ExpectedStartOfTopLevelDefinitionID,
MissingReturnTypeWithReturnStatementID,
NoReturnFromInlineID,
ReturnOutsideMethodDefinitionID,
;

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 @@ -1747,4 +1747,15 @@ object messages {
|returned from a method.
|"""
}

case class ReturnOutsideMethodDefinition(owner: Symbol)(implicit ctx: Context)
extends Message(ReturnOutsideMethodDefinitionID) {
val kind = "Syntax"
val msg = hl"${"return"} outside method definition"
val explanation =
hl"""You used ${"return"} in ${owner}.
|${"return"} is a keyword and may only be used within method declarations.
|"""
}

}
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 @@ -980,7 +980,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def enclMethInfo(cx: Context): (Tree, Type) = {
val owner = cx.owner
if (owner.isType) {
ctx.error("return outside method definition", tree.pos)
ctx.error(ReturnOutsideMethodDefinition(owner), tree.pos)
(EmptyTree, WildcardType)
}
else if (owner != cx.outer.owner && owner.isRealMethod) {
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 @@ -985,4 +985,17 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val NoReturnFromInline(method) :: Nil = messages
assertEquals("method usesReturn", method.show)
}

@Test def returnOutsideMethodDefinition =
checkMessagesAfter("frontend") {
"""object A {
| return 5
|}
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val ReturnOutsideMethodDefinition(owner) :: Nil = messages
assertEquals("object A", owner.show)
}
}