give correct error on attempt to const array

This commit is contained in:
Irmen de Jong 2023-03-10 23:46:13 +01:00
parent 043df18daa
commit a636d3f394
2 changed files with 20 additions and 0 deletions

View File

@ -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) {

View File

@ -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"
}
})