Skip to content

Commit 9e900ca

Browse files
Merge pull request #3174 from ennru/ennru_NoReturnFromInline
move 'no explicit return allowed from inline' to error class
2 parents d365291 + 1bd4c69 commit 9e900ca

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public enum ErrorMessageID {
9797
DuplicatePrivateProtectedQualifierID,
9898
ExpectedStartOfTopLevelDefinitionID,
9999
MissingReturnTypeWithReturnStatementID,
100+
NoReturnFromInlineID,
100101
;
101102

102103
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,4 +1737,14 @@ object messages {
17371737
hl"you have to provide either ${"class"}, ${"trait"}, ${"object"}, or ${"enum"} definitions after qualifiers"
17381738
}
17391739

1740+
case class NoReturnFromInline(owner: Symbol)(implicit ctx: Context)
1741+
extends Message(NoReturnFromInlineID) {
1742+
val kind = "Syntax"
1743+
val msg = hl"no explicit ${"return"} allowed from inline $owner"
1744+
val explanation =
1745+
hl"""Methods marked with ${"@inline"} may not use ${"return"} statements.
1746+
|Instead, you should rely on the last expression's value being
1747+
|returned from a method.
1748+
|"""
1749+
}
17401750
}

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
985985
}
986986
else if (owner != cx.outer.owner && owner.isRealMethod) {
987987
if (owner.isInlineMethod)
988-
(EmptyTree, errorType(em"no explicit return allowed from inline $owner", tree.pos))
988+
(EmptyTree, errorType(NoReturnFromInline(owner), tree.pos))
989989
else if (!owner.isCompleted)
990990
(EmptyTree, errorType(MissingReturnTypeWithReturnStatement(owner), tree.pos))
991991
else {

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,4 +970,19 @@ class ErrorMessagesTests extends ErrorMessagesTest {
970970
val MissingReturnTypeWithReturnStatement(method) :: Nil = messages
971971
assertEquals(method.name.show, "bad")
972972
}
973+
974+
@Test def noReturnInInline =
975+
checkMessagesAfter("frontend") {
976+
"""class BadFunction {
977+
| @inline def usesReturn: Int = { return 42 }
978+
|}
979+
""".stripMargin
980+
}.expect { (ictx, messages) =>
981+
implicit val ctx: Context = ictx
982+
983+
assertMessageCount(1, messages)
984+
985+
val NoReturnFromInline(method) :: Nil = messages
986+
assertEquals("method usesReturn", method.show)
987+
}
973988
}

0 commit comments

Comments
 (0)