Skip to content

Commit

Permalink
Use tags instead of kind for type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Jan 4, 2024
1 parent b93be06 commit 7afbd4f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2480,24 +2480,22 @@ public void visit(BLangAssignment assignNode) {
}

private boolean isOptionalBasicTypeFieldAssignment(BLangAssignment assignNode) {
if (assignNode.varRef.getKind() != NodeKind.FIELD_BASED_ACCESS_EXPR) {
BLangNode varRef = assignNode.varRef;
if (varRef.getKind() != NodeKind.FIELD_BASED_ACCESS_EXPR) {
return false;
}
BLangFieldBasedAccess fieldAccessNode = (BLangFieldBasedAccess) assignNode.varRef;
BLangFieldBasedAccess fieldAccessNode = (BLangFieldBasedAccess) varRef;
BType targetType = Types.getImpliedType(fieldAccessNode.expr.getBType());
if (targetType.getKind() != TypeKind.RECORD) {
if (targetType.tag != TypeTags.RECORD) {
return false;
}
BRecordType recordType = (BRecordType) targetType;
BField field = recordType.fields.get(fieldAccessNode.field.value);
if (field == null || (field.symbol.flags & Flags.OPTIONAL) != Flags.OPTIONAL) {
if (field == null || !Symbols.isOptional(field.symbol)) {
return false;
}
BType fieldType = field.getType();
return switch (fieldType.getKind()) {
case BOOLEAN, FLOAT, STRING, DECIMAL -> true;
default -> TypeTags.isIntegerTypeTag(fieldType.tag);
};
BType fieldType = Types.getImpliedType(field.getType());
return TypeTags.isSimpleBasicType(fieldType.tag);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ type Bar record {
decimal c;
};

type MyString string;

type R1 record {
int a?;
float b?;
string c?;
boolean d?;
decimal e?;
string:Char f?;
MyString g?;
};

type R2 record {
Expand All @@ -61,17 +65,21 @@ function testOptionalFieldAccessOnRequiredRecordField() returns boolean {
}

function testOptionalFieldRemovalBasicType() {
R1 r = {a: 1, b: 2.0, c: "test", d: true, e: 3.0};
R1 r = {a: 1, b: 2.0, c: "test", d: true, e: 3.0, f:"c", g: "test"};
r.a = ();
r.b = ();
r.c = ();
r.d = ();
r.e = ();
r.f = ();
r.g = ();
assertFalse(r.hasKey("a"));
assertFalse(r.hasKey("b"));
assertFalse(r.hasKey("c"));
assertFalse(r.hasKey("d"));
assertFalse(r.hasKey("e"));
assertFalse(r.hasKey("f"));
assertFalse(r.hasKey("g"));
}

function testOptionalFieldRemovalIndirect() {
Expand Down

0 comments on commit 7afbd4f

Please sign in to comment.