diff --git a/compiler/src/prog8/ast/processing/AstChecker.kt b/compiler/src/prog8/ast/processing/AstChecker.kt index ccdd77359..8629df5ce 100644 --- a/compiler/src/prog8/ast/processing/AstChecker.kt +++ b/compiler/src/prog8/ast/processing/AstChecker.kt @@ -327,6 +327,8 @@ internal class AstChecker(private val program: Program, err("Pass-by-reference types (str, array) cannot occur as a parameter type directly. Instead, use an uword for their address, or access the variable from the outer scope directly.") } } + + visitStatements(subroutine.statements) } override fun visit(repeatLoop: RepeatLoop) { @@ -1040,6 +1042,20 @@ internal class AstChecker(private val program: Program, } } + override fun visit(scope: AnonymousScope) { + visitStatements(scope.statements) + } + + private fun visitStatements(statements: List) { + for((index, stmt) in statements.withIndex()) { + if(stmt is FunctionCallStatement && stmt.target.nameInSource.last()=="exit") { + if(index < statements.size-1) { + printWarning("unreachable code", statements[index+1].position, "exit call above never returns") + } + } + } + } + private fun checkFunctionOrLabelExists(target: IdentifierReference, statement: Statement): Statement? { val targetStatement = target.targetStatement(program.namespace) if(targetStatement is Label || targetStatement is Subroutine || targetStatement is BuiltinFunctionStatementPlaceholder) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index e776b423a..8029585d8 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO ==== - option to load library files from a directory instead of the embedded ones -- compiler warning 'unreachable code' for statement following an exit() function call - code optimizer: remove all statements following an exit() function call - vector inc/dec/add/sub/mul/div...? arrayvar++ / arrayvar-- / arrayvar += 2 / arrayvar -= 2 / arrayvar *= 3 / arrayvar /= 3 diff --git a/examples/test.p8 b/examples/test.p8 index c6d64e8d1..55daaa391 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -21,9 +21,9 @@ main { c64scr.print("sp2:") print_stackpointer() exit(65) - sub3() ; TODO unreachable code compiler warning + remove the code in optimizer - sub3() ; TODO unreachable code compiler warning + remove the code in optimizer - sub3() ; TODO unreachable code compiler warning + remove the code in optimizer + sub3() ; TODO remove the code in optimizer + sub3() ; TODO remove the code in optimizer + sub3() ; TODO remove the code in optimizer } sub sub3() {