From 12fd92536a03730b2fa59dc55ba5ae1ff87cd07b Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 19 Feb 2024 14:00:53 +0100 Subject: [PATCH] No need for Convert record. Boolean is enough. --- .../test/EqualsConversionsTest.java | 14 ++++---- .../expression/builtin/meta/EqualsNode.java | 35 ++++++++++--------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/EqualsConversionsTest.java b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/EqualsConversionsTest.java index 05c0dcfddfc4..c47144e646c7 100644 --- a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/EqualsConversionsTest.java +++ b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/EqualsConversionsTest.java @@ -1,13 +1,13 @@ package org.enso.interpreter.test; -import java.util.List; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.List; import org.graalvm.polyglot.Context; import org.graalvm.polyglot.PolyglotException; import org.junit.AfterClass; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.junit.BeforeClass; import org.junit.Test; @@ -89,7 +89,8 @@ public void testDifferentComparators() { gen.intNumConversion = true; gen.intComparator = true; gen.numComparator = false; - gen.extraBlock = """ + gen.extraBlock = + """ type Second_Comparator compare a:Num b:Num = Num_Comparator.compare a b hash a:Num = Num_Comparator.hash a @@ -165,7 +166,8 @@ boolean evaluate() { r0 = 42 == num42 r0 """; - var res = TestBase.evalModule(context, block0 + block1 + block2 + block3 + mainBlock + extraBlock); + var res = + TestBase.evalModule(context, block0 + block1 + block2 + block3 + mainBlock + extraBlock); return res.asBoolean(); } } diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/EqualsNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/EqualsNode.java index 883e71ad89dd..8a56ab67181a 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/EqualsNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/meta/EqualsNode.java @@ -126,8 +126,6 @@ static Type findTypeUncached(Object obj) { return findType(TypeOfNode.getUncached(), obj); } - record Convert(boolean flip, Function f1, Function f2, Function f3) {} - private static boolean isDefinedIn(ModuleScope scope, Function fn) { if (fn.getCallTarget().getRootNode() instanceof EnsoRootNode ensoRoot) { return ensoRoot.getModuleScope() == scope; @@ -146,23 +144,28 @@ private static Object convertor(EnsoContext ctx, Function convFn, Object value) convFn, null, state, new Object[] {ctx.getBuiltins().comparable(), value}); } - Convert findConversions(Type selfType, Type thatType, Object self, Object that) { + /** + * @return {@code null} if no conversion found + */ + Boolean findConversions(Type selfType, Type thatType, Object self, Object that) { if (selfType == null || thatType == null) { return null; } var ctx = EnsoContext.get(this); - var c1 = findConversionImpl(ctx, false, selfType, thatType, self, that); - if (c1 != null) { - return c1; + if (findConversionImpl(ctx, selfType, thatType, self, that)) { + return false; } else { - var c2 = findConversionImpl(ctx, true, thatType, selfType, that, self); - return c2; + if (findConversionImpl(ctx, thatType, selfType, that, self)) { + return true; + } else { + return null; + } } } - private static Convert findConversionImpl( - EnsoContext ctx, boolean flip, Type selfType, Type thatType, Object self, Object that) { + private static boolean findConversionImpl( + EnsoContext ctx, Type selfType, Type thatType, Object self, Object that) { var selfScope = selfType.getDefinitionScope(); var comparableType = ctx.getBuiltins().comparable().getType(); @@ -176,14 +179,14 @@ private static Convert findConversionImpl( && isDefinedIn(selfScope, fromThatType) && convertor(ctx, fromSelfType, self) == convertor(ctx, fromThatType, that) && betweenBoth != null) { - return new Convert(flip, fromSelfType, fromThatType, betweenBoth); + return true; } else { - return null; + return false; } } @Specialization( - limit = "3", + limit = "10", guards = { "selfType != null", "thatType != null", @@ -199,13 +202,13 @@ final boolean doConversionCached( Type selfType, @Cached(value = "findType(typeOfNode, that)", uncached = "findTypeUncached(that)") Type thatType, - @Cached("findConversions(selfType, thatType, self, that)") Convert convert, + @Cached("findConversions(selfType, thatType, self, that)") Boolean convert, @Shared("convert") @Cached InteropConversionCallNode convertNode, @Shared("invoke") @Cached(allowUncached = true) EqualsSimpleNode equalityNode) { if (convert == null) { return false; } - if (convert.flip) { + if (convert) { return doDispatch(frame, that, self, thatType, convertNode, equalityNode); } else { return doDispatch(frame, self, that, selfType, convertNode, equalityNode); @@ -225,7 +228,7 @@ final boolean doConversionUncached( var conv = findConversions(selfType, thatType, self, that); if (conv != null) { var result = - conv.flip + conv ? doDispatch(frame, that, self, thatType, convertNode, equalityNode) : doDispatch(frame, self, that, selfType, convertNode, equalityNode); return result;