compiler error instead of crash when using functioncall without returnvalue

This commit is contained in:
Irmen de Jong 2021-03-12 19:31:04 +01:00
parent 1c1da8e38e
commit a00c39e9cf
2 changed files with 20 additions and 2 deletions

View File

@ -157,7 +157,7 @@ private class BuiltinFunctionsFacade(functions: Map<String, FSignature>): 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
}

View File

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