From 5cda750e5ee61cfdf7846deefb7458bd797297cd Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 18 Sep 2024 23:00:03 +0200 Subject: [PATCH] improve error message for undefined symbol in when choices --- .../compiler/astprocessing/TypecastsAdder.kt | 2 +- compiler/test/TestTypecasts.kt | 18 ++++++++++++++++++ .../prog8/ast/expressions/AstExpressions.kt | 3 +-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt index 7c129e417..310e614e3 100644 --- a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt +++ b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt @@ -330,7 +330,7 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val val values = whenChoice.values values?.toTypedArray()?.withIndex()?.forEach { (index, value) -> val valueDt = value.inferType(program) - if(valueDt!=conditionDt) { + if(valueDt.isKnown && valueDt!=conditionDt) { val castedValue = value.typecastTo(conditionDt.getOr(DataType.UNDEFINED), valueDt.getOr(DataType.UNDEFINED), true) if(castedValue.first) { castedValue.second.linkParents(whenChoice) diff --git a/compiler/test/TestTypecasts.kt b/compiler/test/TestTypecasts.kt index 236e28790..b26127f0c 100644 --- a/compiler/test/TestTypecasts.kt +++ b/compiler/test/TestTypecasts.kt @@ -839,4 +839,22 @@ main { compileText(VMTarget(), false, src, writeAssembly = true) shouldNotBe null compileText(C64Target(), false, src, writeAssembly = true) shouldNotBe null } + + test("undefined symbol error instead of type cast error") { + val src=""" +main { + const ubyte foo = 0 + ubyte bar = 0 + sub start() { + when foo { + notdefined -> bar = 1 + else -> bar = 2 + } + } +}""" + val errors = ErrorReporterForTests() + compileText(C64Target(), false, src, writeAssembly = false, errors = errors) shouldBe null + errors.errors.size shouldBe 2 + errors.errors[1] shouldContain "undefined symbol" + } }) diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index 379a4835d..327c68efd 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -5,7 +5,6 @@ import prog8.ast.Node import prog8.ast.Program import prog8.ast.base.ExpressionError import prog8.ast.base.FatalAstException -import prog8.ast.base.UndefinedSymbolError import prog8.ast.statements.* import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstVisitor @@ -1100,7 +1099,7 @@ data class IdentifierReference(val nameInSource: List, override val posi if(sub.parameters.any { it.name==nameInSource.last() }) return null } - throw UndefinedSymbolError(this) + return null } val vardecl = node as? VarDecl if(vardecl==null) {