Avoid WithWarnings without any warnings (#8583)

This commit is contained in:
Jaroslav Tulach 2023-12-19 17:03:16 +01:00 committed by GitHub
parent fce6d5dce6
commit 7daad75dd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 8 deletions

View File

@ -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;

View File

@ -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

View File

@ -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);
}
}