Skip to content

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

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package reporting
package diagnostic

import dotc.core._
import Contexts.Context
import Contexts.{Context, NoContext}
import Decorators._
import Symbols._
import Names._
Expand All @@ -23,6 +23,7 @@ import dotty.tools.dotc.ast.Trees
import dotty.tools.dotc.ast.untpd.Modifiers
import dotty.tools.dotc.core.Flags.{FlagSet, Mutable}
import dotty.tools.dotc.core.SymDenotations.SymDenotation

import scala.util.control.NonFatal

object messages {
Expand Down Expand Up @@ -1747,4 +1748,17 @@ object messages {
|returned from a method.
|"""
}

case class ReturnOutsideMethodDefinition(checkedContext: Context)(implicit ctx: Context)
extends Message(ReturnOutsideMethodDefinitionID) {
val kind = "Syntax"
val msg = hl"${"return"} outside method definition"
private def contextInfo =
if (checkedContext == NoContext) "outside any declaration"
else s"in ${checkedContext.owner.show}"
val explanation =
hl"""You used ${"return"} $contextInfo.
|${"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 (cx == NoContext || owner.isType) {
Copy link
Contributor

@allanrenucci allanrenucci Sep 26, 2017

Choose a reason for hiding this comment

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

@nicolasstucki I'm curious. Does it ever happen to have NoContext here?

ctx.error("return outside method definition", tree.pos)
ctx.error(ReturnOutsideMethodDefinition(cx), tree.pos)
(EmptyTree, WildcardType)
}
else if (owner != cx.outer.owner && owner.isRealMethod) {
Expand Down
14 changes: 14 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,18 @@ 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(checkedContext) :: Nil = messages
assertEquals("object A", checkedContext.owner.show)
}

}