diff --git a/compiler/src/prog8/compiler/Compiler.kt b/compiler/src/prog8/compiler/Compiler.kt index 9b64c933c..948c04109 100644 --- a/compiler/src/prog8/compiler/Compiler.kt +++ b/compiler/src/prog8/compiler/Compiler.kt @@ -157,7 +157,7 @@ private class BuiltinFunctionsFacade(functions: Map): IBuilt } } else if(func.known_returntype==null) - throw IllegalArgumentException("builtin function $name can't be used here because it doesn't return a value") + return null // builtin function $name can't be used here because it doesn't return a value } return null } diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index 6628de741..9b8906a5c 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -374,7 +374,7 @@ internal class AstChecker(private val program: Program, if(!idt.isKnown) { errors.err("return type mismatch", assignment.value.position) } - if(stmt.returntypes.size <= 1 && stmt.returntypes.single() isNotAssignableTo idt.typeOrElse(DataType.BYTE)) { + if(stmt.returntypes.isEmpty() || (stmt.returntypes.size == 1 && stmt.returntypes.single() isNotAssignableTo idt.typeOrElse(DataType.BYTE))) { errors.err("return type mismatch", assignment.value.position) } } @@ -953,6 +953,24 @@ internal class AstChecker(private val program: Program, } } + // check if a function that doesn't return a value, is used in an expression or assignment + if(targetStatement is Subroutine) { + if(targetStatement.returntypes.isEmpty()) { + if(functionCall.parent is Expression || functionCall.parent is Assignment) + errors.err("subroutine doesn't return a value", functionCall.position) + else + TODO("check $functionCall ${functionCall.parent}") + } + } + else if(targetStatement is BuiltinFunctionStatementPlaceholder) { + if(builtinFunctionReturnType(targetStatement.name, functionCall.args, program).isUnknown) { + if(functionCall.parent is Expression || functionCall.parent is Assignment) + errors.err("function doesn't return a value", functionCall.position) + else + TODO("check builtinfunc $functionCall ${functionCall.parent}") + } + } + super.visit(functionCall) }