diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java index e249daa10f9f..e1188c157871 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/desugar/Desugar.java @@ -5794,8 +5794,8 @@ public void visit(BLangForkJoin forkJoin) { @Override public void visit(BLangLiteral literalExpr) { - if (Types.getReferredType(literalExpr.getBType()).tag == TypeTags.ARRAY || - Types.getReferredType(literalExpr.getBType()).tag == TypeTags.TUPLE) { + int tag = Types.getReferredType(literalExpr.getBType()).tag; + if (tag == TypeTags.ARRAY || tag == TypeTags.TUPLE) { // this is blob literal as byte array result = rewriteBlobLiteral(literalExpr); return; diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.java index 6fb27c861a14..274c4f9f0c83 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/parser/BLangNodeBuilder.java @@ -944,10 +944,8 @@ public BLangNode transform(ConstantDeclarationNode constantDeclarationNode) { // Check whether the value is a literal or a unary expression and if it is not any one of the before mentioned // kinds it is an invalid case, so we don't need to consider it. - if ((nodeKind == NodeKind.LITERAL || nodeKind == NodeKind.NUMERIC_LITERAL || - nodeKind == NodeKind.UNARY_EXPR) - && (constantNode.typeNode == null - || constantNode.typeNode.getKind() != NodeKind.ARRAY_TYPE)) { + if ((nodeKind == NodeKind.LITERAL || nodeKind == NodeKind.NUMERIC_LITERAL || nodeKind == NodeKind.UNARY_EXPR) + && (constantNode.typeNode == null || constantNode.typeNode.getKind() != NodeKind.ARRAY_TYPE)) { // Note - If the RHS is a literal, we need to create an anonymous type definition which can later be used // in type definitions.h createAnonymousTypeDefForConstantDeclaration(constantNode, pos, identifierPos); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java index 01239aeb3f7d..53f491915b97 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/ConstantTypeChecker.java @@ -256,17 +256,7 @@ public void visit(BLangLiteral literalExpr, AnalyzerData data) { } if (literalType.tag == TypeTags.BYTE_ARRAY) { - BLangListConstructorExpr listConstructorExpr = rewriteByteArrayLiteral(literalExpr); - BType expType = Types.getReferredType(data.expType); - if (expType.tag == TypeTags.ARRAY && ((BArrayType) expType).state == BArrayState.INFERRED) { - ((BArrayType) expType).size = listConstructorExpr.exprs.size(); - ((BArrayType) expType).state = BArrayState.CLOSED; - } - if (types.typeIncompatible(literalExpr.pos, listConstructorExpr.getBType(), data.expType)) { - data.resultType = symTable.semanticError; - return; - } - data.resultType = checkListConstructorCompatibility(literalType, listConstructorExpr, data); + data.resultType = rewriteByteArrayLiteral(literalExpr, data); return; } @@ -283,25 +273,27 @@ public void visit(BLangLiteral literalExpr, AnalyzerData data) { data.resultType = finiteType; } - private BLangListConstructorExpr rewriteByteArrayLiteral(BLangLiteral literalExpr) { + private BType rewriteByteArrayLiteral(BLangLiteral literalExpr, AnalyzerData data) { byte[] values = types.convertToByteArray((String) literalExpr.value); - BLangListConstructorExpr.BLangArrayLiteral arrayLiteralNode - = (BLangListConstructorExpr.BLangArrayLiteral) TreeBuilder.createArrayLiteralExpressionNode(); - BArrayType literalType = new BArrayType(symTable.byteType, null, values.length, - BArrayState.CLOSED); - arrayLiteralNode.setBType(literalType); - arrayLiteralNode.pos = literalExpr.pos; - arrayLiteralNode.exprs = new ArrayList<>(); + + List memberTypes = new ArrayList<>(); for (byte b : values) { - arrayLiteralNode.exprs.add(createByteLiteral(literalExpr.pos, b)); + memberTypes.add(getFiniteType(Byte.toUnsignedInt(b), data.constantSymbol, literalExpr.pos, + symTable.byteType)); + } + + BType expType = Types.getReferredType(data.expType); + if (expType.tag == TypeTags.ARRAY && ((BArrayType) expType).state == BArrayState.INFERRED) { + ((BArrayType) expType).size = memberTypes.size(); + ((BArrayType) expType).state = BArrayState.CLOSED; + } + + BArrayType arrayType = new BArrayType(symTable.byteType, null, values.length, BArrayState.CLOSED); + if (types.typeIncompatible(literalExpr.pos, arrayType, expType)) { + return symTable.semanticError; } - return arrayLiteralNode; - } - private BLangLiteral createByteLiteral(Location pos, Byte value) { - BLangLiteral byteLiteral = new BLangLiteral(Byte.toUnsignedInt(value), symTable.byteType); - byteLiteral.pos = pos; - return byteLiteral; + return createNewTupleType(literalExpr.pos, memberTypes, data); } @Override