improve error message for undefined symbol in when choices

This commit is contained in:
Irmen de Jong 2024-09-18 23:00:03 +02:00
parent 4e143d45c8
commit 5cda750e5e
3 changed files with 20 additions and 3 deletions

View File

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

View File

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

View File

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