Skip to content

move 'no explicit return allowed from inline' to error class #3174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public enum ErrorMessageID {
DuplicatePrivateProtectedQualifierID,
ExpectedStartOfTopLevelDefinitionID,
MissingReturnTypeWithReturnStatementID,
NoReturnFromInlineID,
;

public int errorNumber() {
Expand Down
10 changes: 10 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1737,4 +1737,14 @@ object messages {
hl"you have to provide either ${"class"}, ${"trait"}, ${"object"}, or ${"enum"} definitions after qualifiers"
}

case class NoReturnFromInline(owner: Symbol)(implicit ctx: Context)
extends Message(NoReturnFromInlineID) {
val kind = "Syntax"
val msg = hl"no explicit ${"return"} allowed from inline $owner"
val explanation =
hl"""Methods marked with ${"@inline"} may not use ${"return"} statements.
|Instead, you should rely on the last expression's value being
|returned from a method.
|"""
}
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
}
else if (owner != cx.outer.owner && owner.isRealMethod) {
if (owner.isInlineMethod)
(EmptyTree, errorType(em"no explicit return allowed from inline $owner", tree.pos))
(EmptyTree, errorType(NoReturnFromInline(owner), tree.pos))
else if (!owner.isCompleted)
(EmptyTree, errorType(MissingReturnTypeWithReturnStatement(owner), tree.pos))
else {
Expand Down
15 changes: 15 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -970,4 +970,19 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val MissingReturnTypeWithReturnStatement(method) :: Nil = messages
assertEquals(method.name.show, "bad")
}

@Test def noReturnInInline =
checkMessagesAfter("frontend") {
"""class BadFunction {
| @inline def usesReturn: Int = { return 42 }
|}
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

assertMessageCount(1, messages)

val NoReturnFromInline(method) :: Nil = messages
assertEquals("method usesReturn", method.show)
}
}