From 3d048be9a056ac85c673a102f75dd8d6ff06c4af Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Tue, 26 Nov 2024 07:28:40 +0100 Subject: [PATCH] Documenting the example of Text and extra Integer equality check --- .../test/EqualsMultiValueTest.java | 32 +++++++++++++------ .../builtin/meta/EqualsSimpleNode.java | 6 +++- .../runtime/data/EnsoMultiValue.java | 25 ++++++++++----- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/EqualsMultiValueTest.java b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/EqualsMultiValueTest.java index 4a866fc0355d..7113f7377d1f 100644 --- a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/EqualsMultiValueTest.java +++ b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/EqualsMultiValueTest.java @@ -68,25 +68,37 @@ public void testEqualityIntegerAndMultiValue() { } @Test - public void testEqualityIntegerAndTextMultiValue() { + public void testEqualityTextAndExtraIntegerMultiValue() { executeInContext( context, () -> { var builtins = ContextUtils.leakContext(context).getBuiltins(); var intType = builtins.number().getInteger(); - var textText = builtins.text(); - var fourExtraText = - EnsoMultiValue.create( - new Type[] {textText, intType}, 1, new Object[] {Text.create("Hi"), 4L}); - + var textType = builtins.text(); + var bothTypes = new Type[] {textType, intType}; + + var text = Text.create("Hi"); + var ahoj = Text.create("Ahoj"); + var integer = 4L; + // + // following variable represents result of + // x = _ : (Text & Integer) : Text + // e.g. multi value with Text and Integer, casted to Text only + // + var multiV = EnsoMultiValue.create(bothTypes, 1, text, integer); + + assertTrue("'Hi' == multiV", equalityCheck(text, multiV)); + assertFalse("'Ahoj' != multiV", equalityCheck(ahoj, multiV)); assertTrue( "Only Text is 'dispatch type'. Not integer. Shall we be equal to 4?", - equalityCheck(4L, fourExtraText)); - assertFalse("5 != t4", equalityCheck(5L, fourExtraText)); + equalityCheck(integer, multiV)); + assertFalse("5 != t4", equalityCheck(5L, multiV)); assertTrue( "Only Text is 'dispatch type'. Not integer. Shall we be equal to 4?", - equalityCheck(fourExtraText, 4L)); - assertFalse("4 != t5", equalityCheck(fourExtraText, 5L)); + equalityCheck(multiV, integer)); + assertFalse("4 != t5", equalityCheck(multiV, 5L)); + assertTrue("multiV == 'Hi'", equalityCheck(multiV, text)); + assertFalse("multiV != 'Ahoj'", equalityCheck(multiV, ahoj)); return null; }); diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/EqualsSimpleNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/EqualsSimpleNode.java index a08974e8788a..d5308abcf8e8 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/EqualsSimpleNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/EqualsSimpleNode.java @@ -268,7 +268,11 @@ EqualsAndInfo equalsTextBigInt(Text self, EnsoBigInteger other) { * lexicographical order, handling Unicode normalization. See {@code Text_Utils.compare_to}. */ @Specialization( - guards = {"selfInterop.isString(selfString)", "isNotMulti(selfString)"}, + guards = { + "selfInterop.isString(selfString)", + "isNotMulti(selfString)", + "isNotMulti(otherString)" + }, limit = "3") EqualsAndInfo equalsStrings( Object selfString, diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoMultiValue.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoMultiValue.java index a05f4680e636..9108bce0b443 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoMultiValue.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/data/EnsoMultiValue.java @@ -2,7 +2,6 @@ import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; -import com.oracle.truffle.api.dsl.Cached; import com.oracle.truffle.api.dsl.Cached.Shared; import com.oracle.truffle.api.dsl.GenerateUncached; import com.oracle.truffle.api.dsl.NeverDefault; @@ -57,7 +56,21 @@ private EnsoMultiValue(Type[] types, int dispatchTypes, Object[] values) { : "Avoid double wrapping " + Arrays.toString(values); } - public static EnsoObject create(Type[] types, int dispatchTypes, Object[] values) { + /** + * Creates new instance of EnsoMultiValue from provided information. + * + * @param types all the types this value can be {@link CastToNode cast to} + * @param dispatchTypes the (subset of) types that the value is cast to currently - bigger than + * {@code 0} and at most {@code type.length} + * @param values value of each of the provided {@code types} + * @return non-{@code null} multi value instance + */ + @NeverDefault + public static EnsoMultiValue create( + @NeverDefault Type[] types, @NeverDefault int dispatchTypes, @NeverDefault Object... values) { + assert dispatchTypes > 0; + assert dispatchTypes <= types.length; + assert types.length == values.length; return new EnsoMultiValue(types, dispatchTypes, values); } @@ -451,13 +464,9 @@ public static CastToNode getUncached() { } @Specialization - Object castsToAType( - Type type, - EnsoMultiValue mv, - boolean reorderOnly, - @Cached(value = "type", allowUncached = true, neverDefault = true) Type cachedType) { + Object castsToAType(Type type, EnsoMultiValue mv, boolean reorderOnly) { for (var i = 0; i < mv.types.length; i++) { - if (mv.types[i] == cachedType) { + if (mv.types[i] == type) { if (reorderOnly) { var copyTypes = mv.types.clone(); var copyValues = mv.values.clone();