diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/ordering/SortVectorNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/ordering/SortVectorNode.java index 38131760ed..91e8558bd4 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/ordering/SortVectorNode.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/node/expression/builtin/ordering/SortVectorNode.java @@ -263,7 +263,10 @@ public abstract class SortVectorNode extends Node { } case REPORT_WARNING -> { return attachDifferentComparatorsWarning( - attachWarnings(sortedVector, gatheredWarnings), groups); + gatheredWarnings.isEmpty() + ? sortedVector + : attachWarnings(sortedVector, gatheredWarnings), + groups); } case IGNORE -> { return sortedVector; diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/error/WithWarnings.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/error/WithWarnings.java index 2bbe92e555..221bbba703 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/error/WithWarnings.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/error/WithWarnings.java @@ -35,11 +35,12 @@ public final class WithWarnings implements EnsoObject { * @param maxWarnings maximal number of warnings allowed to be attached to the value * @param limitReached if `true`, indicates that `warnings` have already been limited for a * custom-method, `false` otherwise - * @param warnings warnings to be attached to a value + * @param warnings non-empty warnings to be attached to a value */ private WithWarnings(Object value, int maxWarnings, boolean limitReached, Warning... warnings) { assert !(value instanceof WithWarnings); this.warnings = createSetFromArray(maxWarnings, warnings); + assert this.warnings.size() > 0; this.value = value; this.limitReached = limitReached || this.warnings.size() >= maxWarnings; this.maxWarnings = maxWarnings; @@ -69,6 +70,7 @@ public final class WithWarnings implements EnsoObject { Warning... additionalWarnings) { assert !(value instanceof WithWarnings); this.warnings = cloneSetAndAppend(maxWarnings, warnings, additionalWarnings); + assert this.warnings.size() > 0; this.value = value; this.limitReached = limitReached || this.warnings.size() >= maxWarnings; this.maxWarnings = maxWarnings; @@ -168,7 +170,7 @@ public final class WithWarnings implements EnsoObject { @ExportMessage boolean hasWarnings() { - return warnings.size() > 0; + return true; } @ExportMessage diff --git a/engine/runtime/src/test/java/org/enso/interpreter/test/WarningsTest.java b/engine/runtime/src/test/java/org/enso/interpreter/test/WarningsTest.java index 46e74a8aca..b7abdeac36 100644 --- a/engine/runtime/src/test/java/org/enso/interpreter/test/WarningsTest.java +++ b/engine/runtime/src/test/java/org/enso/interpreter/test/WarningsTest.java @@ -1,6 +1,7 @@ package org.enso.interpreter.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import org.enso.interpreter.runtime.EnsoContext; import org.enso.interpreter.runtime.error.Warning; @@ -17,24 +18,26 @@ import org.junit.Test; public class WarningsTest extends TestBase { private static Context ctx; + private static EnsoContext ensoContext; @BeforeClass public static void initEnsoContext() { ctx = createDefaultContext(); + ensoContext = + (EnsoContext) + ctx.getBindings(LanguageInfo.ID) + .invokeMember(MethodNames.TopScope.LEAK_CONTEXT) + .asHostObject(); } @AfterClass public static void disposeContext() { + ensoContext = null; ctx.close(); } @Test public void doubleWithWarningsWrap() { - var ensoContext = - (EnsoContext) - ctx.getBindings(LanguageInfo.ID) - .invokeMember(MethodNames.TopScope.LEAK_CONTEXT) - .asHostObject(); var warn1 = Warning.create(ensoContext, "w1", this); var warn2 = Warning.create(ensoContext, "w2", this); var value = 42; @@ -49,4 +52,17 @@ public class WarningsTest extends TestBase { Assert.assertArrayEquals( new Object[] {warn1, warn2}, with2.getWarningsArray(WarningsLibrary.getUncached())); } + + @Test + public void wrapAndUnwrap() { + var value = 42; + WithWarnings without; + try { + without = WithWarnings.wrap(ensoContext, 42, new Warning[0]); + } catch (AssertionError e) { + // OK + return; + } + fail("One shall not be created WithWarnings without any warnings " + without); + } }