mirror of
https://github.com/irmen/prog8.git
synced 2024-11-22 15:33:02 +00:00
flag "returning a statement" as a syntax error
This commit is contained in:
parent
ef198f1493
commit
932035cdc5
@ -140,6 +140,18 @@ internal class AstChecker(private val program: Program,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val statements = (returnStmt.parent as IStatementContainer).statements
|
||||
val myIndex = statements.indexOf(returnStmt)
|
||||
if(myIndex>=0 && myIndex<statements.size-1) {
|
||||
val stmtAfterReturn = statements[myIndex+1]
|
||||
if(stmtAfterReturn.position.line==returnStmt.position.line) {
|
||||
// this error is not generated by the parser, unfortunately.
|
||||
// it parses "return somestatement" as: "return" "somestatement" (and this then only results in a warning about unreachable code).
|
||||
errors.err("a statement is not a return value", stmtAfterReturn.position)
|
||||
}
|
||||
}
|
||||
|
||||
super.visit(returnStmt)
|
||||
}
|
||||
|
||||
|
@ -174,4 +174,22 @@ main {
|
||||
compileText(Cx16Target(), false, text, writeAssembly = true) shouldNotBe null
|
||||
compileText(VMTarget(), false, text, writeAssembly = true) shouldNotBe null
|
||||
}
|
||||
|
||||
test("return with a statement instead of a value is a syntax error") {
|
||||
val src="""
|
||||
main {
|
||||
|
||||
sub invalid() {
|
||||
return cx16.r0++
|
||||
}
|
||||
|
||||
sub start() {
|
||||
invalid()
|
||||
}
|
||||
}"""
|
||||
val errors=ErrorReporterForTests()
|
||||
compileText(C64Target(), false, src, writeAssembly = false, errors=errors) shouldBe null
|
||||
errors.errors.size shouldBe 1
|
||||
errors.errors[0] shouldContain "statement"
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user