File tree Expand file tree Collapse file tree 4 files changed +26
-1
lines changed
test/dotty/tools/dotc/reporting Expand file tree Collapse file tree 4 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -98,6 +98,7 @@ public enum ErrorMessageID {
9898 ExpectedStartOfTopLevelDefinitionID ,
9999 MissingReturnTypeWithReturnStatementID ,
100100 NoReturnFromInlineID ,
101+ ReturnOutsideMethodDefinitionID ,
101102 ;
102103
103104 public int errorNumber () {
Original file line number Diff line number Diff line change @@ -1747,4 +1747,15 @@ object messages {
17471747 |returned from a method.
17481748 | """
17491749 }
1750+
1751+ case class ReturnOutsideMethodDefinition (owner : Symbol )(implicit ctx : Context )
1752+ extends Message (ReturnOutsideMethodDefinitionID ) {
1753+ val kind = " Syntax"
1754+ val msg = hl " ${" return" } outside method definition "
1755+ val explanation =
1756+ hl """ You used ${" return" } in ${owner}.
1757+ | ${" return" } is a keyword and may only be used within method declarations.
1758+ | """
1759+ }
1760+
17501761}
Original file line number Diff line number Diff line change @@ -980,7 +980,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
980980 def enclMethInfo (cx : Context ): (Tree , Type ) = {
981981 val owner = cx.owner
982982 if (owner.isType) {
983- ctx.error(" return outside method definition " , tree.pos)
983+ ctx.error(ReturnOutsideMethodDefinition (owner) , tree.pos)
984984 (EmptyTree , WildcardType )
985985 }
986986 else if (owner != cx.outer.owner && owner.isRealMethod) {
Original file line number Diff line number Diff line change @@ -985,4 +985,17 @@ class ErrorMessagesTests extends ErrorMessagesTest {
985985 val NoReturnFromInline (method) :: Nil = messages
986986 assertEquals(" method usesReturn" , method.show)
987987 }
988+
989+ @ Test def returnOutsideMethodDefinition =
990+ checkMessagesAfter(" frontend" ) {
991+ """ object A {
992+ | return 5
993+ |}
994+ """ .stripMargin
995+ }.expect { (ictx, messages) =>
996+ implicit val ctx : Context = ictx
997+ assertMessageCount(1 , messages)
998+ val ReturnOutsideMethodDefinition (owner) :: Nil = messages
999+ assertEquals(" object A" , owner.show)
1000+ }
9881001}
You can’t perform that action at this time.
0 commit comments