flag "returning a statement" as a syntax error

This commit is contained in:
Irmen de Jong 2024-01-31 21:36:39 +01:00
parent ef198f1493
commit 932035cdc5
2 changed files with 30 additions and 0 deletions

View File

@ -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)
}

View File

@ -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"
}
})