diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index 4b636f521..aa39a8ab1 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -519,7 +519,7 @@ internal class AstChecker(private val program: Program, } // FLOATS enabled? - if(!compilerOptions.floats && decl.datatype in setOf(DataType.FLOAT, DataType.ARRAY_F) && decl.type!= VarDeclType.MEMORY) + if(!compilerOptions.floats && decl.datatype.oneOf(DataType.FLOAT, DataType.ARRAY_F) && decl.type!= VarDeclType.MEMORY) err("floating point used, but that is not enabled via options") if(decl.datatype == DataType.FLOAT && (decl.zeropage==ZeropageWish.REQUIRE_ZEROPAGE || decl.zeropage==ZeropageWish.PREFER_ZEROPAGE)) diff --git a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt index 83c19af1e..f45f7dec7 100644 --- a/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt +++ b/compiler/src/prog8/compiler/astprocessing/TypecastsAdder.kt @@ -182,7 +182,7 @@ class TypecastsAdder(val program: Program, val errors: IErrorReporter) : AstWalk override fun after(typecast: TypecastExpression, parent: Node): Iterable { // warn about any implicit type casts to Float, because that may not be intended - if(typecast.implicit && typecast.type in setOf(DataType.FLOAT, DataType.ARRAY_F)) { + if(typecast.implicit && typecast.type.oneOf(DataType.FLOAT, DataType.ARRAY_F)) { errors.warn("integer implicitly converted to float. Suggestion: use float literals, add an explicit cast, or revert to integer arithmetic", typecast.position) } return noModifications diff --git a/compilerAst/src/prog8/ast/base/Base.kt b/compilerAst/src/prog8/ast/base/Base.kt index a446fc264..a0c93931c 100644 --- a/compilerAst/src/prog8/ast/base/Base.kt +++ b/compilerAst/src/prog8/ast/base/Base.kt @@ -24,16 +24,17 @@ enum class DataType { */ infix fun isAssignableTo(targetType: DataType) = when(this) { - UBYTE -> targetType in setOf(UBYTE, WORD, UWORD, FLOAT) - BYTE -> targetType in setOf(BYTE, WORD, FLOAT) - UWORD -> targetType in setOf(UWORD, FLOAT) - WORD -> targetType in setOf(WORD, FLOAT) + UBYTE -> targetType.oneOf(UBYTE, WORD, UWORD, FLOAT) + BYTE -> targetType.oneOf(BYTE, WORD, FLOAT) + UWORD -> targetType.oneOf(UWORD, FLOAT) + WORD -> targetType.oneOf(WORD, FLOAT) FLOAT -> targetType == FLOAT STR -> targetType == STR in ArrayDatatypes -> targetType == this else -> false } + fun oneOf(vararg types: DataType) = this in types infix fun isAssignableTo(targetTypes: Set) = targetTypes.any { this isAssignableTo it } infix fun isNotAssignableTo(targetType: DataType) = !this.isAssignableTo(targetType) infix fun isNotAssignableTo(targetTypes: Set) = !this.isAssignableTo(targetTypes) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 2c089be3b..bbd42c7d3 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,8 +3,9 @@ TODO For next compiler release ^^^^^^^^^^^^^^^^^^^^^^^^^ -replace checks against multiple types with .oneOf(...) +replace checks of InferredType against multiple types with .oneOf(...) replace certain uses of inferredType.getOr(UNKNOWN) by i.getOrElse({ errorhandler }) +replace registerOrPair in setOf(..) by .oneOf(..) varargs function Blocked by Commander-x16 v39 release