more precise error messages checks

This commit is contained in:
Irmen de Jong 2021-10-24 19:09:44 +02:00
parent 6b32535cb6
commit 17175df835
5 changed files with 16 additions and 12 deletions

View File

@ -505,7 +505,7 @@ internal class AstChecker(private val program: Program,
}
override fun visit(decl: VarDecl) {
fun err(msg: String, position: Position?=null) = errors.err(msg, position ?: decl.position)
fun err(msg: String) = errors.err(msg, decl.position)
// the initializer value can't refer to the variable itself (recursive definition)
if(decl.value?.referencesIdentifier(decl.name) == true || decl.arraysize?.indexExpr?.referencesIdentifier(decl.name) == true)
@ -586,10 +586,10 @@ internal class AstChecker(private val program: Program,
val numvalue = decl.value as? NumericLiteralValue
if(numvalue!=null) {
if (numvalue.type !in IntegerDatatypes || numvalue.number.toInt() < 0 || numvalue.number.toInt() > 65535) {
err("memory address must be valid integer 0..\$ffff", decl.value?.position)
err("memory address must be valid integer 0..\$ffff")
}
} else {
err("value of memory mapped variable can only be a fixed number, perhaps you meant to use an address pointer type instead?", decl.value?.position)
err("value of memory mapped variable can only be a fixed number, perhaps you meant to use an address pointer type instead?")
}
}
}
@ -597,7 +597,7 @@ internal class AstChecker(private val program: Program,
val declValue = decl.value
if(declValue!=null && decl.type==VarDeclType.VAR) {
if (declValue.inferType(program) isnot decl.datatype) {
err("initialisation value has incompatible type (${declValue.inferType(program)}) for the variable (${decl.datatype})", declValue.position)
err("initialisation value has incompatible type (${declValue.inferType(program)}) for the variable (${decl.datatype})")
}
}

View File

@ -252,14 +252,14 @@ class TestModuleImporter {
val result = importer.importLibraryModule(filenameNoExt)
assertThat(count[n] + " call / NO .p8 extension", result, Is(nullValue()))
assertFalse(errors.noErrors(), count[n] + " call / NO .p8 extension")
assertEquals(errors.errors.single(), "no module found with name i_do_not_exist")
assertContains(errors.errors.single(), "0:0: no module found with name i_do_not_exist")
errors.report()
assertThat(program.modules.size, equalTo(1))
val result2 = importer.importLibraryModule(filenameWithExt)
assertThat(count[n] + " call / with .p8 extension", result2, Is(nullValue()))
assertFalse(importer.errors.noErrors(), count[n] + " call / with .p8 extension")
assertEquals(errors.errors.single(), "no module found with name i_do_not_exist.p8")
assertContains(errors.errors.single(), "0:0: no module found with name i_do_not_exist.p8")
errors.report()
assertThat(program.modules.size, equalTo(1))
}

View File

@ -17,6 +17,7 @@ import prog8tests.helpers.*
import prog8tests.helpers.assertFailure
import prog8tests.helpers.assertSuccess
import prog8tests.helpers.compileText
import kotlin.test.assertContains
import kotlin.test.assertEquals
@ -236,8 +237,8 @@ class TestCompilerOnRanges {
}
""", errors, false).assertFailure()
assertEquals(2, errors.errors.size)
assertEquals("range expression from value must be integer", errors.errors[0])
assertEquals("range expression to value must be integer", errors.errors[1])
assertContains(errors.errors[0], ".p8:5:29: range expression from value must be integer")
assertContains(errors.errors[1], ".p8:5:44: range expression to value must be integer")
}
@Test

View File

@ -24,6 +24,7 @@ class TestSubroutines {
val text = """
main {
sub start() {
str zzz ; should give uninitialized error
}
asmsub asmfunc(str thing @AY) {
@ -36,7 +37,9 @@ class TestSubroutines {
val errors = ErrorReporterForTests()
compileText(C64Target, false, text, errors, false).assertFailure("currently str type in signature is invalid") // TODO should not be invalid
assertEquals(0, errors.warnings.size)
assertTrue(errors.errors.single().startsWith("Pass-by-reference types (str, array) cannot occur as a parameter type directly."))
assertEquals(2, errors.errors.size)
assertContains(errors.errors[0], ".p8:4:20: string var must be initialized with a string literal")
assertContains(errors.errors[1], ".p8:10:16: Pass-by-reference types (str, array) cannot occur as a parameter type directly")
}
@Test
@ -59,7 +62,7 @@ class TestSubroutines {
val errors = ErrorReporterForTests()
compileText(C64Target, false, text, errors, false).assertFailure("currently array dt in signature is invalid") // TODO should not be invalid?
assertEquals(0, errors.warnings.size)
assertTrue(errors.errors.single().startsWith("Pass-by-reference types (str, array) cannot occur as a parameter type directly."))
assertContains(errors.errors.single(), ".p8:9:16: Pass-by-reference types (str, array) cannot occur as a parameter type directly")
}
@Test

View File

@ -10,11 +10,11 @@ class ErrorReporterForTests(private val throwExceptionAtReportIfErrors: Boolean=
val warnings = mutableListOf<String>()
override fun err(msg: String, position: Position) {
errors.add(msg)
errors.add("${position.toClickableStr()} $msg")
}
override fun warn(msg: String, position: Position) {
warnings.add(msg)
warnings.add("${position.toClickableStr()} $msg")
}
override fun noErrors(): Boolean = errors.isEmpty()