Skip to content

Commit

Permalink
Fix intersection type
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Jul 31, 2024
1 parent de35941 commit fce9516
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.ballerina.runtime.api.types.Type;
import io.ballerina.runtime.api.types.semtype.Builder;
import io.ballerina.runtime.api.types.semtype.Context;
import io.ballerina.runtime.api.types.semtype.Core;
import io.ballerina.runtime.api.types.semtype.SemType;

import java.util.ArrayList;
Expand Down Expand Up @@ -225,7 +226,21 @@ SemType createSemType(Context cx) {
if (effectiveType instanceof SemType semType) {
return semType;
}
return Builder.from(cx, effectiveType);
if (constituentTypes.isEmpty()) {
return Builder.neverType();
}
SemType result = Builder.from(cx, constituentTypes.get(0));
boolean hasBType = Core.containsBasicType(Builder.from(cx, effectiveType), Builder.bType());
result = Core.intersect(result, Core.SEMTYPE_TOP);
for (int i = 1; i < constituentTypes.size(); i++) {
SemType memberType = Builder.from(cx, constituentTypes.get(i));
// hasBType |= Core.containsBasicType(memberType, Builder.bType());
result = Core.intersect(result, memberType);
}
if (hasBType) {
return Core.union(result, BTypeConverter.wrapAsPureBType((BType) effectiveType));
}
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static BTypeParts split(Context cx, Type type) {
} else if (type instanceof BTypeReferenceType referenceType) {
return split(cx, referenceType.getReferredType());
} else if (type instanceof BIntersectionType intersectionType) {
return split(cx, intersectionType.getEffectiveType());
return splitIntersection(cx, intersectionType);
} else if (type instanceof BReadonlyType readonlyType) {
return splitReadonly(readonlyType);
} else if (type instanceof BFiniteType finiteType) {
Expand All @@ -134,6 +134,17 @@ private static BTypeParts split(Context cx, Type type) {
}
}

private static BTypeParts splitIntersection(Context cx, BIntersectionType intersectionType) {
List<Type> members = Collections.unmodifiableList(intersectionType.getConstituentTypes());
SemType semTypePart = Builder.valType();
for (Type member : members) {
BTypeParts memberParts = split(cx, member);
semTypePart = Core.intersect(memberParts.semTypePart(), semTypePart);
}
BTypeParts effectiveTypeParts = split(cx, intersectionType.getEffectiveType());
return new BTypeParts(semTypePart, effectiveTypeParts.bTypeParts());
}

private static BTypeParts splitSemTypeSupplier(Context cx, PartialSemTypeSupplier supplier) {
int startingIndex = cx.addProvisionalType((BType) supplier);
SemType semtype = supplier.get(cx);
Expand Down

0 comments on commit fce9516

Please sign in to comment.