fix problem with initializing certain array decls with single value

This commit is contained in:
Irmen de Jong 2023-03-11 00:43:30 +01:00
parent f20ca06f85
commit ed597423cd
2 changed files with 29 additions and 2 deletions

View File

@ -626,8 +626,15 @@ internal class AstChecker(private val program: Program,
val declValue = decl.value
if(declValue!=null && decl.type==VarDeclType.VAR) {
if (declValue.inferType(program) isnot decl.datatype) {
err("initialisation value has incompatible type (${declValue.inferType(program)}) for the variable (${decl.datatype})")
val iDt = declValue.inferType(program)
if (iDt isnot decl.datatype) {
if(decl.datatype in ArrayDatatypes) {
val eltDt = ArrayToElementTypes.getValue(decl.datatype)
if(iDt isnot eltDt)
err("initialisation value has incompatible type (${declValue.inferType(program)}) for the variable (${decl.datatype})")
} else {
err("initialisation value has incompatible type (${declValue.inferType(program)}) for the variable (${decl.datatype})")
}
}
}
@ -1462,6 +1469,10 @@ internal class AstChecker(private val program: Program,
DataType.BOOL -> {
return true
}
in ArrayDatatypes -> {
val eltDt = ArrayToElementTypes.getValue(targetDt)
return checkValueTypeAndRange(eltDt, value)
}
else -> return err("value of type ${value.type} not compatible with $targetDt")
}
return true

View File

@ -129,4 +129,20 @@ class TestAstChecks: FunSpec({
errors.warnings.size shouldBe 0
errors.errors[0] shouldContain "indexing requires"
}
test("array decl with expression as size can be initialized with a single value") {
val text = """
main {
sub start() {
const ubyte n = 40
const ubyte half = n / 2
ubyte[half] @shared a = 5
}
}
"""
val errors = ErrorReporterForTests(keepMessagesAfterReporting = true)
compileText(C64Target(), true, text, writeAssembly = true, errors=errors) shouldNotBe null
errors.errors.size shouldBe 0
errors.warnings.size shouldBe 0
}
})