Skip to content

Commit

Permalink
Assert there is no intersection between dispatch and extra types
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Dec 30, 2024
1 parent 615b600 commit a68db22
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,17 @@ private static void registerValue(
if (rawValue instanceof EnsoMultiValue) {
return;
}
var rawInt = (Type) ContextUtils.unwrapValue(ctx(), g.typeInteger());
var rawType = ContextUtils.unwrapValue(ctx(), t);
if (rawType instanceof Type type) {
if (rawType == rawInt) {
return;
}
var singleMultiValue =
EnsoMultiValue.NewNode.getUncached()
.newValue(new Type[] {type}, 1, new Object[] {rawValue});
var n = t.getMetaSimpleName();
data.add(new Object[] {singleMultiValue, n, 0});
var rawInt = (Type) ContextUtils.unwrapValue(ctx(), g.typeInteger());
var secondMultiValue =
EnsoMultiValue.NewNode.getUncached()
.newValue(new Type[] {rawInt, type}, 2, new Object[] {5L, rawValue});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ private static void registerValue(
if (r2 instanceof EnsoMultiValue) {
return;
}
if (typ1 == typ2) {
return;
}
var both =
EnsoMultiValue.NewNode.getUncached()
.newValue(new Type[] {typ1, typ2}, 2, new Object[] {r1, r2});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import java.util.Arrays;
import java.util.HashSet;
import java.util.concurrent.ConcurrentHashMap;
import org.enso.interpreter.runtime.EnsoContext;

Expand Down Expand Up @@ -83,6 +84,14 @@ public String toString() {
return "MultiType{" + "types=" + Arrays.toString(types) + '}';
}

@CompilerDirectives.TruffleBoundary
final boolean hasIntersectionWith(EnsoMultiType other) {
var my = new HashSet<>(Arrays.asList(types));
var their = Arrays.asList(other.types);
my.removeAll(their);
return my.size() < types.length;
}

@GenerateUncached
abstract static class FindIndexNode extends Node {
private static final String INLINE_CACHE_LIMIT = "5";
Expand Down Expand Up @@ -133,6 +142,13 @@ static AllTypesWith create() {
return EnsoMultiTypeFactory.AllTypesWithNodeGen.create();
}

/**
* Don't modify the return value. It maybe cached!
*
* @param first first set of types
* @param second second set of types
* @return union of both types
*/
abstract Type[] executeAllTypes(EnsoMultiType first, EnsoMultiType second);

@Specialization(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public final class EnsoMultiValue extends EnsoObject {
private final Object[] values;

private EnsoMultiValue(EnsoMultiType dispatch, EnsoMultiType extra, Object[] values) {
assert !dispatch.hasIntersectionWith(extra)
: "Dispatch (" + dispatch + " and extra " + extra + " should be disjoin!";
this.dispatch = dispatch;
this.extra = extra;
this.values = values;
Expand Down Expand Up @@ -553,6 +555,7 @@ public final Object findTypeOrNull(
if (i == 0 && dispatch.typesLength() == 1) {
return newNode.newValue(copyTypes, 1, mv.values);
} else {
copyTypes = copyTypes.clone();
var copyValues = mv.values.clone();
copyTypes[0] = copyTypes[i];
copyValues[0] = copyValues[i];
Expand Down

0 comments on commit a68db22

Please sign in to comment.