rollback implicit casts to boolean in struct initializers and function call arguments

This commit is contained in:
Irmen de Jong
2025-07-27 03:27:26 +02:00
parent 624220e9a3
commit a52966f327
5 changed files with 27 additions and 17 deletions

View File

@@ -679,14 +679,8 @@ class TypecastsAdder(val program: Program, val options: CompilationOptions, val
val sourceDt = expressionToCast.inferType(program).getOrUndef() val sourceDt = expressionToCast.inferType(program).getOrUndef()
if(sourceDt.base == requiredType.base) if(sourceDt.base == requiredType.base)
return return
if(requiredType.isBool) { if(requiredType.isBool)
if(sourceDt.isNumeric || sourceDt.isPointer) { return // don't allow any implicit cast to bool
// only allow numerics and pointers to be implicitly cast to bool
val cast = TypecastExpression(expressionToCast, DataType.BOOL, true, expressionToCast.position)
modifications += IAstModification.ReplaceNode(expressionToCast, cast, parent)
}
return
}
// uwords are allowed to be assigned to pointers without a cast // uwords are allowed to be assigned to pointers without a cast
if(requiredType.isPointer && sourceDt.isUnsignedWord) if(requiredType.isPointer && sourceDt.isUnsignedWord)

View File

@@ -1356,8 +1356,8 @@ main {
val errors=ErrorReporterForTests() val errors=ErrorReporterForTests()
compileText(VMTarget(), false, src, outputDir, errors=errors) shouldBe null compileText(VMTarget(), false, src, outputDir, errors=errors) shouldBe null
errors.errors.size shouldBe 4 errors.errors.size shouldBe 4
errors.errors[0] shouldContain "doesn't match target type" errors.errors[0] shouldContain "incompatible type"
errors.errors[1] shouldContain "doesn't match target type" errors.errors[1] shouldContain "incompatible type"
errors.errors[2] shouldContain "doesn't match target type" errors.errors[2] shouldContain "doesn't match target type"
errors.errors[3] shouldContain "doesn't match target type" errors.errors[3] shouldContain "doesn't match target type"
} }

View File

@@ -679,7 +679,7 @@ main {
errors.errors[3] shouldContain ("value type bool doesn't match target") errors.errors[3] shouldContain ("value type bool doesn't match target")
} }
test("bool function parameters correct typing and implicit casts to bool") { test("bool function parameters correct typing and no implicit casts to bool") {
val src = """ val src = """
main { main {
sub start() { sub start() {
@@ -703,8 +703,15 @@ main {
}""" }"""
val errors = ErrorReporterForTests() val errors = ErrorReporterForTests()
compileText(C64Target(), false, src, outputDir, writeAssembly = false, errors = errors) shouldBe null compileText(C64Target(), false, src, outputDir, writeAssembly = false, errors = errors) shouldBe null
errors.errors.size shouldBe 1 errors.errors.size shouldBe 8
errors.errors[0] shouldContain ("value type bool doesn't match target") errors.errors[0] shouldContain ("type mismatch")
errors.errors[1] shouldContain ("type mismatch")
errors.errors[2] shouldContain ("type mismatch")
errors.errors[3] shouldContain ("type mismatch")
errors.errors[4] shouldContain ("type mismatch")
errors.errors[5] shouldContain ("type mismatch")
errors.errors[6] shouldContain ("type mismatch")
errors.errors[7] shouldContain ("value type bool doesn't match target")
} }
test("no implicit bool-to-int cast in assignment") { test("no implicit bool-to-int cast in assignment") {

View File

@@ -1,8 +1,6 @@
TODO TODO
==== ====
initializing a struct with a numberic for a boolean field, should give a type error (seems to silently cast to bool now)
STRUCTS and TYPED POINTERS STRUCTS and TYPED POINTERS
-------------------------- --------------------------

View File

@@ -6,9 +6,20 @@ main {
} }
sub start() { sub start() {
bool @shared b1
b1 = 4.44
b1 = 99
bfunc(4.44)
bfunc(99)
^^Enemy @shared e1 = Enemy() ^^Enemy @shared e1 = Enemy()
^^Enemy @shared e2 = Enemy(1,2,3,true) ^^Enemy @shared e2 = Enemy(1,2,3,true)
^^Enemy @shared e3 = Enemy(1,2,3,4) ; TODO type error for the boolean ^^Enemy @shared e3 = Enemy(1,2,3,4)
^^Enemy @shared e4 = Enemy(1,2,3,4.555) ; TODO type error for the boolean ^^Enemy @shared e4 = Enemy(1,2,3,4.555)
}
sub bfunc(bool bb) {
cx16.r0++
} }
} }