Skip to content

Commit

Permalink
Use referred type when creating typedesc for records
Browse files Browse the repository at this point in the history
  • Loading branch information
rdulmina committed Nov 21, 2024
1 parent 35555f3 commit 5551ed0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ public static void processAnnotations(MapValue<BString, Object> globalAnnotMap,

if (type.getTag() == TypeTags.TYPE_REFERENCED_TYPE_TAG) {
Type impliedType = TypeUtils.getImpliedType(type);
if (impliedType.getTag() == TypeTags.RECORD_TYPE_TAG) {
processAnnotations(globalAnnotMap, impliedType);
return;
}
if (isNonObjectType(impliedType.getTag())) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.wso2.ballerinalang.compiler.bir.codegen.split;

import org.ballerinalang.model.elements.PackageID;
import org.ballerinalang.model.symbols.SymbolOrigin;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.wso2.ballerinalang.compiler.bir.codegen.BallerinaClassWriter;
Expand Down Expand Up @@ -128,8 +127,8 @@ private void loadAnnotations(MethodVisitor mv, String pkgName, BIRNode.BIRTypeDe
jvmPackageGen.lookupGlobalVarClassName(pkgName, ANNOTATION_MAP_NAME);
mv.visitFieldInsn(GETSTATIC, pkgClassName, ANNOTATION_MAP_NAME, JvmSignatures.GET_MAP_VALUE);
BType type = typeDef.type;
BType refType = typeDef.referenceType == null ||
(type.tag == TypeTags.RECORD && typeDef.origin == SymbolOrigin.VIRTUAL) ? type : typeDef.referenceType;
BType refType = typeDef.referenceType == null || type.tag == TypeTags.RECORD
? type : typeDef.referenceType;
jvmTypeGen.loadType(mv, refType);
mv.visitMethodInsn(INVOKESTATIC, ANNOTATION_UTILS, "processAnnotations",
JvmSignatures.PROCESS_ANNOTATIONS, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleMember;
import org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BTypeReferenceType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BTypedescType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType;
import org.wso2.ballerinalang.compiler.semantics.model.types.BXMLSubType;
Expand Down Expand Up @@ -892,10 +893,11 @@ private void desugarConstants(BLangConstant constant, List<BLangVariable> desuga
}

private void createTypedescVariable(BType type, Location pos) {
BType finalType = type;
if ((Types.getReferredType(type).tag != TypeTags.INTERSECTION && this.env.enclPkg.typeDefinitions.stream()
.anyMatch(typeDef ->
(Types.getReferredType(typeDef.typeNode.getBType()).tag == TypeTags.INTERSECTION) &&
typeDef.symbol.name.value.equals(type.tsymbol.name.value)))) {
typeDef.symbol.name.value.equals(finalType.tsymbol.name.value)))) {
// This is a workaround for an issue where we create two type defs with same name for the below sample
// type T1 [T1] & readonly;
return;
Expand All @@ -905,6 +907,13 @@ private void createTypedescVariable(BType type, Location pos) {
BType typedescType = new BTypedescType(type, symTable.typeDesc.tsymbol);
BSymbol owner = this.env.scope.owner;
BVarSymbol varSymbol = new BVarSymbol(0, name, owner.pkgID, typedescType, owner, pos, VIRTUAL);
if (type.tag == TypeTags.TYPEREFDESC) {
BType referredType = ((BTypeReferenceType) type).referredType;
int tag = referredType.tag;
if (tag == TypeTags.RECORD) {
type = referredType;
}
}
BLangTypedescExpr typedescExpr = ASTBuilderUtil.createTypedescExpr(pos, typedescType, type);
typedescList.add(createSimpleVariableDef(pos, name.value, typedescType, typedescExpr, varSymbol));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public void testValRefType() {
public void testRefTypes() {
Object returns = BRunUtil.invoke(compileResult, "testRefTypes");
BMap<?, ?> foo2 = (BMap<?, ?>) returns;
assertEquals(foo2.getType().getTag(), TypeTags.TYPE_REFERENCED_TYPE_TAG);
assertEquals(foo2.get(StringUtils.fromString("s")).toString(), "qwerty");
assertEquals(foo2.get(StringUtils.fromString("i")), 10L);
assertEquals(getType(foo2.get(StringUtils.fromString("rj"))).getTag(), TypeTags.MAP_TAG);
Expand All @@ -121,8 +120,7 @@ public void testRefTypes() {
assertEquals(getType(foo2.get(StringUtils.fromString("rp"))).getTag(), TypeTags.OBJECT_TYPE_TAG);
assertEquals(((BObject) foo2.get(StringUtils.fromString("rp"))).get(StringUtils.fromString("name")).toString(),
"John Doe");
assertEquals(getType(foo2.get(StringUtils.fromString("ra"))).getTag(),
TypeTags.RECORD_TYPE_TAG);
assertEquals(getType(foo2.get(StringUtils.fromString("ra"))).getTag(), TypeTags.RECORD_TYPE_TAG);
assertEquals(foo2.get(StringUtils.fromString("ra")).toString(), "{\"city\":\"Colombo\",\"country\":\"Sri " +
"Lanka\"}");
assertEquals(getType(foo2.get(StringUtils.fromString("crx"))).getTag(), TypeTags.XML_ELEMENT_TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function testRecordTypeAnnotationReadonlyValueEdit() {

error resError = <error> res;
assertEquality("{ballerina/lang.map}InherentTypeViolation", resError.message());
assertEquality("cannot update 'readonly' field 'foo' in record of type '(Annot & readonly)'",
assertEquality("cannot update 'readonly' field 'foo' in record of type 'Annot & readonly'",
resError.detail()["message"]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ function testFrozenAnyArrayElementUpdate() returns error? {
test:assertTrue(actualError is error);
test:assertEquals((<error>actualError).message(), "{ballerina/lang.map}InherentTypeViolation");
test:assertEquals((<error>actualError).detail()["message"],
"cannot update 'readonly' field 'name' in record of type '(Employee & readonly)'");
"cannot update 'readonly' field 'name' in record of type 'Employee & readonly'");
}

0 comments on commit 5551ed0

Please sign in to comment.