diff --git a/codeOptimizers/src/prog8/optimizer/ConstantIdentifierReplacer.kt b/codeOptimizers/src/prog8/optimizer/ConstantIdentifierReplacer.kt index 8cf4d5aa1..3324100c7 100644 --- a/codeOptimizers/src/prog8/optimizer/ConstantIdentifierReplacer.kt +++ b/codeOptimizers/src/prog8/optimizer/ConstantIdentifierReplacer.kt @@ -87,6 +87,10 @@ internal class ConstantIdentifierReplacer(private val program: Program, private if(forloop!=null && identifier===forloop.loopVar) return noModifications + val dt = identifier.inferType(program) + if(!dt.isKnown || !dt.isNumeric) + return noModifications + try { val cval = identifier.constValue(program) ?: return noModifications return when (cval.type) { diff --git a/compiler/test/TestAstChecks.kt b/compiler/test/TestAstChecks.kt index bbc3c8eb7..a7fd01e32 100644 --- a/compiler/test/TestAstChecks.kt +++ b/compiler/test/TestAstChecks.kt @@ -96,4 +96,20 @@ class TestAstChecks: FunSpec({ """ compileText(C64Target(), false, text, writeAssembly = false) shouldNotBe null } + + test("const is not allowed on arrays") { + val text = """ + main { + sub start() { + const ubyte[5] a = 5 + a[2]=42 + } + } + """ + val errors = ErrorReporterForTests(keepMessagesAfterReporting = true) + compileText(C64Target(), true, text, writeAssembly = true, errors=errors) + errors.errors.size shouldBe 1 + errors.warnings.size shouldBe 0 + errors.errors[0] shouldContain "const modifier can only be used" + } })