Skip to content

Commit

Permalink
Fix spotbugs issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lochana-chathura committed Oct 23, 2024
1 parent f47d0f6 commit ad2e562
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.ballerina.types.SemTypes;
import io.ballerina.types.definition.ObjectDefinition;
import io.ballerina.types.definition.ObjectQualifiers;
import io.ballerina.types.subtypedata.Range;
import org.ballerinalang.model.Name;
import org.ballerinalang.model.TreeBuilder;
import org.ballerinalang.model.elements.Flag;
Expand Down Expand Up @@ -4369,13 +4370,16 @@ private boolean comparableNillableList(Context cx, SemType t1, SemType t2) {

ListMemberTypes lmTypes1 = Core.listAllMemberTypesInner(cx, t1);
ListMemberTypes lmTypes2 = Core.listAllMemberTypesInner(cx, t2);
CombinedRange[] combinedRanges = combineRanges(lmTypes1.ranges(), lmTypes2.ranges());
CombinedRange[] combinedRanges = combineRanges(
lmTypes1.ranges().toArray(Range[]::new),
lmTypes2.ranges().toArray(Range[]::new)
);
SemType accum = PredefinedType.NIL;
for (CombinedRange combinedRange : combinedRanges) {
Long i1 = combinedRange.i1();
Long i2 = combinedRange.i2();
if (i1 == null) {
SemType lmType = lmTypes2.semTypes()[Math.toIntExact(i2)];
SemType lmType = lmTypes2.semTypes().get(Math.toIntExact(i2));
if (!comparable(accum, lmType)) {
return false;
}
Expand All @@ -4384,7 +4388,7 @@ private boolean comparableNillableList(Context cx, SemType t1, SemType t2) {
}

if (i2 == null) {
SemType lmType = lmTypes1.semTypes()[Math.toIntExact(i1)];
SemType lmType = lmTypes1.semTypes().get(Math.toIntExact(i1));
if (!comparable(accum, lmType)) {
return false;
}
Expand All @@ -4393,7 +4397,8 @@ private boolean comparableNillableList(Context cx, SemType t1, SemType t2) {
}


if (!comparable(lmTypes1.semTypes()[Math.toIntExact(i1)], lmTypes2.semTypes()[Math.toIntExact(i2)])) {
if (!comparable(lmTypes1.semTypes().get(Math.toIntExact(i1)),
lmTypes2.semTypes().get(Math.toIntExact(i2)))) {
cx.comparableMemo.put(semPair, false);
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions semtypes/spotbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,11 @@
</Or>
<Bug pattern="DLS_DEAD_LOCAL_STORE"/>
</Match>
<Match>
<Class name="io.ballerina.types.Core">
<Method name="nextBoundary">
<Bug pattern="NP_LOAD_OF_KNOWN_NULL_VALUE"/>
</Method>
</Class>
</Match>
</FindBugsFilter>
13 changes: 7 additions & 6 deletions semtypes/src/main/java/io/ballerina/types/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ public static SemType listMemberTypeInnerVal(Context cx, SemType t, SemType k) {
}

static final ListMemberTypes LIST_MEMBER_TYPES_ALL = ListMemberTypes.from(
new Range[]{Range.from(0, MAX_VALUE)},
new SemType[]{VAL}
List.of(Range.from(0, MAX_VALUE)),
List.of(VAL)
);

static final ListMemberTypes LIST_MEMBER_TYPES_NONE = ListMemberTypes.from(new Range[0], new SemType[0]);
static final ListMemberTypes LIST_MEMBER_TYPES_NONE = ListMemberTypes.from(List.of(), List.of());

public static ListMemberTypes listAllMemberTypesInner(Context cx, SemType t) {
if (t instanceof BasicTypeBitSet b) {
Expand All @@ -460,7 +460,7 @@ public static ListMemberTypes listAllMemberTypesInner(Context cx, SemType t) {
types.add(m);
}
}
return ListMemberTypes.from(ranges.toArray(Range[]::new), types.toArray(SemType[]::new));
return ListMemberTypes.from(ranges, types);
}

static Range[] bddListAllRanges(Context cx, Bdd b, Range[] accum) {
Expand All @@ -469,7 +469,8 @@ static Range[] bddListAllRanges(Context cx, Bdd b, Range[] accum) {
} else {
BddNode bddNode = (BddNode) b;
ListMemberTypes listMemberTypes = listAtomicTypeAllMemberTypesInnerVal(cx.listAtomType(bddNode.atom()));
return distinctRanges(bddListAllRanges(cx, bddNode.left(), distinctRanges(listMemberTypes.ranges(), accum)),
return distinctRanges(bddListAllRanges(cx, bddNode.left(),
distinctRanges(listMemberTypes.ranges().toArray(Range[]::new), accum)),
distinctRanges(bddListAllRanges(cx, bddNode.middle(), accum),
bddListAllRanges(cx, bddNode.right(), accum)));
}
Expand Down Expand Up @@ -583,7 +584,7 @@ public static ListMemberTypes listAtomicTypeAllMemberTypesInnerVal(ListAtomicTyp
ranges.add(Range.from(fixedLength, MAX_VALUE));
}

return ListMemberTypes.from(ranges.toArray(Range[]::new), types.toArray(SemType[]::new));
return ListMemberTypes.from(ranges, types);
}

public static MappingAtomicType mappingAtomicType(Context cx, SemType t) {
Expand Down
22 changes: 20 additions & 2 deletions semtypes/src/main/java/io/ballerina/types/ListMemberTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import io.ballerina.types.subtypedata.Range;

import java.util.Collections;
import java.util.List;

/**
* Holds a pair of semtype[] and range[].
* <i>Note: Member types at the indices that are not contained in `Range` array represent `never.
Expand All @@ -28,9 +31,24 @@
* @param semTypes SemType array
* @since 2201.11.0
*/
public record ListMemberTypes(Range[] ranges, SemType[] semTypes) {
public record ListMemberTypes(List<Range> ranges, List<SemType> semTypes) {

public ListMemberTypes {
ranges = Collections.unmodifiableList(ranges);
semTypes = Collections.unmodifiableList(semTypes);
}

@Override
public List<Range> ranges() {
return Collections.unmodifiableList(ranges);
}

@Override
public List<SemType> semTypes() {
return Collections.unmodifiableList(semTypes);
}

public static ListMemberTypes from(Range[] ranges, SemType[] semTypes) {
public static ListMemberTypes from(List<Range> ranges, List<SemType> semTypes) {
assert ranges != null && semTypes != null;
return new ListMemberTypes(ranges, semTypes);
}
Expand Down

0 comments on commit ad2e562

Please sign in to comment.