File tree Expand file tree Collapse file tree 4 files changed +28
-1
lines changed
test/dotty/tools/dotc/reporting Expand file tree Collapse file tree 4 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -96,6 +96,7 @@ public enum ErrorMessageID {
9696 WrongNumberOfParametersID ,
9797 DuplicatePrivateProtectedQualifierID ,
9898 ExpectedStartOfTopLevelDefinitionID ,
99+ MissingReturnTypeWithReturnStatementID ,
99100 ;
100101
101102 public int errorNumber () {
Original file line number Diff line number Diff line change @@ -598,6 +598,17 @@ object messages {
598598 |} """
599599 }
600600
601+ case class MissingReturnTypeWithReturnStatement (method : Symbol )(implicit ctx : Context )
602+ extends Message (MissingReturnTypeWithReturnStatementID ) {
603+ val kind = " Syntax"
604+ val msg = hl " $method has a return statement; it needs a result type "
605+ val explanation =
606+ hl """ |If a method contains a ${" return" } statement, it must have an
607+ |explicit return type. For example:
608+ |
609+ | ${" def good: Int /* explicit return type */ = return 1" }"""
610+ }
611+
601612 case class YieldOrDoExpectedInForComprehension ()(implicit ctx : Context )
602613 extends Message (YieldOrDoExpectedInForComprehensionID ) {
603614 val kind = " Syntax"
Original file line number Diff line number Diff line change @@ -987,7 +987,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
987987 if (owner.isInlineMethod)
988988 (EmptyTree , errorType(em " no explicit return allowed from inline $owner" , tree.pos))
989989 else if (! owner.isCompleted)
990- (EmptyTree , errorType(em " $ owner has return statement; needs result type " , tree.pos))
990+ (EmptyTree , errorType(MissingReturnTypeWithReturnStatement ( owner) , tree.pos))
991991 else {
992992 val from = Ident (TermRef (NoPrefix , owner.asTerm))
993993 val proto = returnProto(owner, cx.scope)
Original file line number Diff line number Diff line change @@ -955,4 +955,19 @@ class ErrorMessagesTests extends ErrorMessagesTest {
955955
956956 assertEquals(ExpectedStartOfTopLevelDefinition (), err)
957957 }
958+
959+ @ Test def missingReturnTypeWithReturnStatement =
960+ checkMessagesAfter(" frontend" ) {
961+ """ class BadFunction {
962+ | def bad() = { return "fail" }
963+ |}
964+ """ .stripMargin
965+ }.expect { (ictx, messages) =>
966+ implicit val ctx : Context = ictx
967+
968+ assertMessageCount(1 , messages)
969+
970+ val MissingReturnTypeWithReturnStatement (method) :: Nil = messages
971+ assertEquals(method.name.show, " bad" )
972+ }
958973}
You can’t perform that action at this time.
0 commit comments