fix crash when using undefined variable in for loop

This commit is contained in:
Irmen de Jong 2024-12-06 21:50:22 +01:00
parent 96bed8f57f
commit 2e303041c1
3 changed files with 23 additions and 1 deletions

View File

@ -186,7 +186,7 @@ internal class AstChecker(private val program: Program,
}
}
val iterableDt = forLoop.iterable.inferType(program).getOr(DataType.forDt(BaseDataType.BYTE))
val iterableDt = forLoop.iterable.inferType(program).getOrUndef()
if(iterableDt.isNumeric) TODO("iterable type should not be simple numeric!? "+forLoop.position) // TODO

View File

@ -1015,6 +1015,24 @@ main {
val handler = sub.children[8] as PtSub
handler.name shouldBe "p8s_prog8_invoke_defers"
}
test("unknown variable in for loop gives proper errors") {
val src="""
main {
sub start() {
ubyte i
for i in 0 to count - 1 {
break
}
}
}"""
val errors = ErrorReporterForTests()
compileText(C64Target(), optimize=false, src, writeAssembly=false, errors = errors) shouldBe null
errors.errors.size shouldBe 3
errors.errors[0] shouldContain "loop variable can only loop over"
errors.errors[1] shouldContain "undefined symbol"
}
}
})

View File

@ -241,6 +241,10 @@ class BinaryExpression(
companion object {
fun commonDatatype(leftDt: DataType, rightDt: DataType,
left: Expression?, right: Expression?): Pair<DataType, Expression?> {
if(leftDt.isUndefined || rightDt.isUndefined)
return DataType.forDt(BaseDataType.UNDEFINED) to null
// byte + byte -> byte
// byte + word -> word
// word + byte -> word