From f20ca06f859c7742cd9bf429f2cf3605032db4a7 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 11 Mar 2023 00:26:19 +0100 Subject: [PATCH] give correct error when using memory mapped var as array pointer --- compiler/test/TestAstChecks.kt | 17 +++++++++++++++++ compiler/test/TestScoping.kt | 15 ++++++++++----- .../src/prog8/ast/expressions/AstExpressions.kt | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/compiler/test/TestAstChecks.kt b/compiler/test/TestAstChecks.kt index a7fd01e32..69a17ba6e 100644 --- a/compiler/test/TestAstChecks.kt +++ b/compiler/test/TestAstChecks.kt @@ -112,4 +112,21 @@ class TestAstChecks: FunSpec({ errors.warnings.size shouldBe 0 errors.errors[0] shouldContain "const modifier can only be used" } + + test("array indexing is not allowed on a memory mapped variable") { + val text = """ + main { + sub start() { + &ubyte a = 10000 + uword z = 500 + a[4] = (z % 3) as ubyte + } + } + """ + 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 "indexing requires" + } }) diff --git a/compiler/test/TestScoping.kt b/compiler/test/TestScoping.kt index 4ea5bd758..8e7596d0c 100644 --- a/compiler/test/TestScoping.kt +++ b/compiler/test/TestScoping.kt @@ -162,7 +162,8 @@ class TestScoping: FunSpec({ val errors= ErrorReporterForTests() compileText(C64Target(), false, text, writeAssembly = false, errors = errors) shouldBe null errors.errors.size shouldBe 1 - errors.errors[0] shouldContain "undefined symbol: routine2" + errors.errors[0] shouldContain "undefined" + errors.errors[0] shouldContain "routine2" } test("good subroutine calls with qualified names (from root)") { @@ -201,10 +202,14 @@ class TestScoping: FunSpec({ val errors= ErrorReporterForTests() compileText(C64Target(), false, text, writeAssembly = false, errors=errors) shouldBe null errors.errors.size shouldBe 4 - errors.errors[0] shouldContain "undefined symbol: start.routine2" - errors.errors[1] shouldContain "undefined symbol: wrong.start.routine2" - errors.errors[2] shouldContain "undefined symbol: start.routine2" - errors.errors[3] shouldContain "undefined symbol: wrong.start.routine2" + errors.errors[0] shouldContain "undefined" + errors.errors[0] shouldContain "start.routine2" + errors.errors[1] shouldContain "undefined" + errors.errors[1] shouldContain "wrong.start.routine2" + errors.errors[2] shouldContain "undefined" + errors.errors[2] shouldContain "start.routine2" + errors.errors[3] shouldContain "undefined" + errors.errors[3] shouldContain "wrong.start.routine2" } test("good variables without qualified names") { diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index 774316f09..7dab41ce0 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -315,7 +315,7 @@ class ArrayIndexedExpression(var arrayvar: IdentifierReference, return when (target.datatype) { DataType.STR, DataType.UWORD -> InferredTypes.knownFor(DataType.UBYTE) in ArrayDatatypes -> InferredTypes.knownFor(ArrayToElementTypes.getValue(target.datatype)) - else -> InferredTypes.unknown() + else -> InferredTypes.knownFor(target.datatype) } } return InferredTypes.unknown()