diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index ae85997d9..0d0c23406 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -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 diff --git a/compiler/test/TestAstChecks.kt b/compiler/test/TestAstChecks.kt index 69a17ba6e..e8aff3ea2 100644 --- a/compiler/test/TestAstChecks.kt +++ b/compiler/test/TestAstChecks.kt @@ -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 + } })