Skip to content

Commit

Permalink
Fix error type distinct
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Jul 31, 2024
1 parent 3c99a44 commit de35941
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.PredefinedTypes;
import io.ballerina.runtime.api.TypeTags;
import io.ballerina.runtime.api.flags.SymbolFlags;
import io.ballerina.runtime.api.types.ErrorType;
import io.ballerina.runtime.api.types.IntersectionType;
import io.ballerina.runtime.api.types.Type;
Expand All @@ -31,13 +30,9 @@
import io.ballerina.runtime.api.types.semtype.SemType;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.internal.TypeChecker;
import io.ballerina.runtime.internal.TypeConverter;
import io.ballerina.runtime.internal.types.semtype.ErrorUtils;
import io.ballerina.runtime.internal.types.semtype.MappingDefinition;
import io.ballerina.runtime.internal.values.ErrorValue;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -171,7 +166,7 @@ public Optional<SemType> shapeOf(Context cx, Object object) {
}
SemType detailType = Builder.from(cx, errorDetails.getType());
boolean hasBType = !Core.isNever(Core.intersect(detailType, Core.B_TYPE_TOP));
return BMapType.shapeOfInner(cx, errorDetails)
return BMapType.readonlyShape(cx, errorDetails)
.map(ErrorUtils::errorDetail)
.map(err -> distinctIdSupplier.get().stream().map(ErrorUtils::errorDistinct)
.reduce(err, Core::intersect))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ public Optional<SemType> shapeOf(Context cx, Object object) {
return Optional.of(cachedShape);
}

return shapeOfInner(cx, value);
return readonlyShape(cx, value);
}

static Optional<SemType> shapeOfInner(Context cx, BMap value) {
static Optional<SemType> readonlyShape(Context cx, BMap value) {
int nFields = value.size();
MappingDefinition.Field[] fields = new MappingDefinition.Field[nFields];
Map.Entry[] entries = (Map.Entry[]) value.entrySet().toArray(Map.Entry[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

final class DistinctIdSupplier implements Supplier<Collection<Integer>> {

private List<Integer> ids = null;
private static final Map<TypeId, Integer> allocatedIds = new ConcurrentHashMap<>();
private static final Map<TypeIdWrapper, Integer> allocatedIds = new ConcurrentHashMap<>();
private final Env env;
private final TypeIdSet typeIdSet;

Expand All @@ -48,9 +49,27 @@ public synchronized Collection<Integer> get() {
if (typeIdSet == null) {
return List.of();
}
ids = typeIdSet.getIds().stream().map(typeId -> allocatedIds.computeIfAbsent(typeId,
ids = typeIdSet.getIds().stream().map(TypeIdWrapper::new).map(typeId -> allocatedIds.computeIfAbsent(typeId,
ignored -> env.distinctAtomCountGetAndIncrement()))
.toList();
return ids;
}

// This is to avoid whether id is primary or not affecting the hashcode.
private record TypeIdWrapper(TypeId typeId) {

@Override
public boolean equals(Object obj) {
if (obj instanceof TypeIdWrapper other) {
return typeId.getName().equals(other.typeId().getName()) &&
typeId.getPkg().equals(other.typeId().getPkg());
}
return false;
}

@Override
public int hashCode() {
return Objects.hash(typeId.getPkg(), typeId.getName());
}
}
}

0 comments on commit de35941

Please sign in to comment.