From 64f300525a94102a575151e7ad4fe0c4a62f34cf Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Mon, 16 Dec 2024 10:09:59 +0530 Subject: [PATCH] Fix bstring array tests --- .../runtime/internal/TypeChecker.java | 8 ++++---- .../internal/values/ArrayValueImpl.java | 20 +++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java index 25f62dbef3ad..6362696460b7 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/TypeChecker.java @@ -258,9 +258,6 @@ public static boolean anyToJBoolean(Object sourceVal) { */ public static boolean checkIsType(Object sourceVal, Type targetType) { Type sourceType = getType(sourceVal); - if (sourceType == targetType || (sourceType.getTag() == targetType.getTag() && sourceType.equals(targetType))) { - return true; - } if (isSubType(sourceType, targetType)) { return true; } @@ -604,7 +601,10 @@ private static boolean isSubTypeWithInherentType(Context cx, Object sourceValue, .orElse(false); } - private static synchronized boolean isSubType(Type source, Type target) { + private static boolean isSubType(Type source, Type target) { + if (source == target || (source.getTag() == target.getTag() && source.equals(target))) { + return true; + } if (source instanceof CacheableTypeDescriptor sourceCacheableType && target instanceof CacheableTypeDescriptor targetCacheableType) { return isSubTypeWithCache(sourceCacheableType, targetCacheableType); diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java index 2fec0ee525e1..8325e6781c78 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/values/ArrayValueImpl.java @@ -660,22 +660,21 @@ public void setArrayRefTypeForcefully(ArrayType type, int size) { } public void addInt(long index, long value) { + Type sourceType = TypeChecker.getType(value); if (intValues != null) { - if (elementType == PredefinedTypes.TYPE_INT) { + if (sourceType == this.elementType) { prepareForAddWithoutTypeCheck(index, intValues.length); } else { - // We need type checker for int subtypes prepareForAdd(index, value, intValues.length); } intValues[(int) index] = value; return; } - if (!TypeChecker.isByteLiteral(value)) { - throw ErrorCreator.createError(getModulePrefixedReason(ARRAY_LANG_LIB, - INHERENT_TYPE_VIOLATION_ERROR_IDENTIFIER), ErrorHelper.getErrorDetails( - ErrorCodes.INCOMPATIBLE_TYPE, this.elementType, TypeChecker.getType(value))); + if (sourceType == this.elementType) { + prepareForAddWithoutTypeCheck(index, byteValues.length); + } else { + prepareForAdd(index, value, byteValues.length); } - prepareForAddWithoutTypeCheck(index, byteValues.length); byteValues[(int) index] = (byte) ((Long) value).intValue(); } @@ -700,7 +699,12 @@ private void addString(long index, String value) { } private void addBString(long index, BString value) { - prepareForAddWithoutTypeCheck(index, bStringValues.length); + Type sourceType = TypeChecker.getType(value); + if (sourceType == this.elementType) { + prepareForAddWithoutTypeCheck(index, bStringValues.length); + } else { + prepareForAdd(index, value, bStringValues.length); + } bStringValues[(int) index] = value; }