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 {
96
96
WrongNumberOfParametersID ,
97
97
DuplicatePrivateProtectedQualifierID ,
98
98
ExpectedStartOfTopLevelDefinitionID ,
99
+ MissingReturnTypeWithReturnStatementID ,
99
100
;
100
101
101
102
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -598,6 +598,17 @@ object messages {
598
598
|} """
599
599
}
600
600
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
+
601
612
case class YieldOrDoExpectedInForComprehension ()(implicit ctx : Context )
602
613
extends Message (YieldOrDoExpectedInForComprehensionID ) {
603
614
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
987
987
if (owner.isInlineMethod)
988
988
(EmptyTree , errorType(em " no explicit return allowed from inline $owner" , tree.pos))
989
989
else if (! owner.isCompleted)
990
- (EmptyTree , errorType(em " $ owner has return statement; needs result type " , tree.pos))
990
+ (EmptyTree , errorType(MissingReturnTypeWithReturnStatement ( owner) , tree.pos))
991
991
else {
992
992
val from = Ident (TermRef (NoPrefix , owner.asTerm))
993
993
val proto = returnProto(owner, cx.scope)
Original file line number Diff line number Diff line change @@ -955,4 +955,19 @@ class ErrorMessagesTests extends ErrorMessagesTest {
955
955
956
956
assertEquals(ExpectedStartOfTopLevelDefinition (), err)
957
957
}
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
+ }
958
973
}
You can’t perform that action at this time.
0 commit comments