From 17175df835c277508ce607686328bddf3c105328 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 24 Oct 2021 19:09:44 +0200 Subject: [PATCH] more precise error messages checks --- compiler/src/prog8/compiler/astprocessing/AstChecker.kt | 8 ++++---- compiler/test/ModuleImporterTests.kt | 4 ++-- compiler/test/TestCompilerOnRanges.kt | 5 +++-- compiler/test/TestSubroutines.kt | 7 +++++-- compiler/test/helpers/ErrorReporterForTests.kt | 4 ++-- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index b938e2b03..898a3609e 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -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})") } } diff --git a/compiler/test/ModuleImporterTests.kt b/compiler/test/ModuleImporterTests.kt index d5dd6474e..700ca9054 100644 --- a/compiler/test/ModuleImporterTests.kt +++ b/compiler/test/ModuleImporterTests.kt @@ -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)) } diff --git a/compiler/test/TestCompilerOnRanges.kt b/compiler/test/TestCompilerOnRanges.kt index 56f60561f..8621b77e6 100644 --- a/compiler/test/TestCompilerOnRanges.kt +++ b/compiler/test/TestCompilerOnRanges.kt @@ -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 diff --git a/compiler/test/TestSubroutines.kt b/compiler/test/TestSubroutines.kt index 51dbec8ec..0030aa460 100644 --- a/compiler/test/TestSubroutines.kt +++ b/compiler/test/TestSubroutines.kt @@ -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 diff --git a/compiler/test/helpers/ErrorReporterForTests.kt b/compiler/test/helpers/ErrorReporterForTests.kt index c76070d50..f6f47e81a 100644 --- a/compiler/test/helpers/ErrorReporterForTests.kt +++ b/compiler/test/helpers/ErrorReporterForTests.kt @@ -10,11 +10,11 @@ class ErrorReporterForTests(private val throwExceptionAtReportIfErrors: Boolean= val warnings = mutableListOf() 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()