From a636d3f39421fe7f3f87272ff44a0745885b7693 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 10 Mar 2023 23:46:13 +0100 Subject: [PATCH] give correct error on attempt to const array --- .../optimizer/ConstantIdentifierReplacer.kt | 4 ++++ compiler/test/TestAstChecks.kt | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) 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" + } })