File tree 4 files changed +26
-1
lines changed
test/dotty/tools/dotc/reporting
4 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -98,6 +98,7 @@ public enum ErrorMessageID {
98
98
ExpectedStartOfTopLevelDefinitionID ,
99
99
MissingReturnTypeWithReturnStatementID ,
100
100
NoReturnFromInlineID ,
101
+ ReturnOutsideMethodDefinitionID ,
101
102
;
102
103
103
104
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -1747,4 +1747,15 @@ object messages {
1747
1747
|returned from a method.
1748
1748
| """
1749
1749
}
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
+
1750
1761
}
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
980
980
def enclMethInfo (cx : Context ): (Tree , Type ) = {
981
981
val owner = cx.owner
982
982
if (owner.isType) {
983
- ctx.error(" return outside method definition " , tree.pos)
983
+ ctx.error(ReturnOutsideMethodDefinition (owner) , tree.pos)
984
984
(EmptyTree , WildcardType )
985
985
}
986
986
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 {
985
985
val NoReturnFromInline (method) :: Nil = messages
986
986
assertEquals(" method usesReturn" , method.show)
987
987
}
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
+ }
988
1001
}
You can’t perform that action at this time.
0 commit comments