Skip to content

Commit 8de4b8c

Browse files
Appending warning to DataflowError is noop (#10765)
1 parent 5079b21 commit 8de4b8c

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/WarningsTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.containsString;
55
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertFalse;
67
import static org.junit.Assert.assertTrue;
78
import static org.junit.Assert.fail;
89

10+
import org.enso.common.LanguageInfo;
911
import org.enso.common.MethodNames;
1012
import org.enso.interpreter.runtime.EnsoContext;
1113
import org.enso.interpreter.runtime.warning.AppendWarningNode;
@@ -59,8 +61,10 @@ public void doubleWithWarningsWrap() {
5961
var warn2 = Warning.create(ensoContext, "w2", this);
6062
var value = 42L;
6163

62-
var with1 = AppendWarningNode.getUncached().executeAppend(null, value, warn1);
63-
var with2 = AppendWarningNode.getUncached().executeAppend(null, with1, warn2);
64+
var with1 =
65+
(WithWarnings) AppendWarningNode.getUncached().executeAppend(null, value, warn1);
66+
var with2 =
67+
(WithWarnings) AppendWarningNode.getUncached().executeAppend(null, with1, warn2);
6468

6569
assertEquals(value, with1.getValue());
6670
assertEquals(value, with2.getValue());
@@ -73,7 +77,7 @@ public void doubleWithWarningsWrap() {
7377
@Test
7478
public void wrapAndUnwrap() {
7579
var value = 42;
76-
WithWarnings without;
80+
Object without;
7781
try {
7882
without = AppendWarningNode.getUncached().executeAppend(null, 42, new Warning[0]);
7983
} catch (AssertionError e) {
@@ -154,4 +158,26 @@ private void assertWarningsForAType(Value v) {
154158
}
155159
}
156160
}
161+
162+
@Test
163+
public void warningOnAnError() throws Exception {
164+
var code =
165+
"""
166+
from Standard.Base import Integer, Warning, Error
167+
import Standard.Base.Errors.Illegal_Argument.Illegal_Argument
168+
from Standard.Base.Errors.Common import Out_Of_Range
169+
170+
err_warn -> Integer ! Illegal_Argument =
171+
v = Warning.attach (Out_Of_Range.Error "qewr") 12
172+
case v of
173+
_ : Integer -> Error.throw (Illegal_Argument.Error "asdf")
174+
""";
175+
176+
var module = ctx.eval(LanguageInfo.ID, code);
177+
var errorWithWarning = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "err_warn");
178+
assertFalse("Something is returned", errorWithWarning.isNull());
179+
assertTrue("But it represents an exception object", errorWithWarning.isException());
180+
assertEquals(
181+
"Standard.Base.Error.Error", errorWithWarning.getMetaObject().getMetaQualifiedName());
182+
}
157183
}

engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/AppendWarningNode.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
import com.oracle.truffle.api.nodes.Node;
1313
import com.oracle.truffle.api.profiles.ConditionProfile;
1414
import org.enso.interpreter.runtime.EnsoContext;
15+
import org.enso.interpreter.runtime.data.EnsoObject;
1516
import org.enso.interpreter.runtime.data.hash.EnsoHashMap;
1617
import org.enso.interpreter.runtime.data.hash.HashMapInsertAllNode;
1718
import org.enso.interpreter.runtime.data.hash.HashMapInsertNode;
1819
import org.enso.interpreter.runtime.data.hash.HashMapSizeNode;
1920
import org.enso.interpreter.runtime.data.vector.ArrayLikeAtNode;
2021
import org.enso.interpreter.runtime.data.vector.ArrayLikeLengthNode;
22+
import org.enso.interpreter.runtime.error.DataflowError;
2123

2224
@GenerateUncached
2325
public abstract class AppendWarningNode extends Node {
@@ -38,9 +40,9 @@ public static AppendWarningNode getUncached() {
3840
* It is expected that all the elements in the container are of {@link Warning} class.
3941
* @return A wrapped object with warnings
4042
*/
41-
public abstract WithWarnings executeAppend(VirtualFrame frame, Object object, Object warnings);
43+
public abstract EnsoObject executeAppend(VirtualFrame frame, Object object, Object warnings);
4244

43-
@Specialization
45+
@Specialization(guards = "!isError(object)")
4446
WithWarnings doSingleWarning(
4547
VirtualFrame frame,
4648
Object object,
@@ -70,7 +72,7 @@ WithWarnings doSingleWarning(
7072
return new WithWarnings(value, warnsLimit, isLimitReached, warnsMap);
7173
}
7274

73-
@Specialization
75+
@Specialization(guards = "!isError(object)")
7476
WithWarnings doMultipleWarningsArray(
7577
VirtualFrame frame,
7678
Object object,
@@ -106,7 +108,7 @@ WithWarnings doMultipleWarningsArray(
106108
* This specialization should be the most frequent - just wrapping the given {@code object} with
107109
* warnings hash map
108110
*/
109-
@Specialization(guards = {"!isWithWarns(object)"})
111+
@Specialization(guards = {"!isError(object)", "!isWithWarns(object)"})
110112
WithWarnings doObjectMultipleWarningsHashMap(
111113
VirtualFrame frame,
112114
Object object,
@@ -138,7 +140,8 @@ WithWarnings doWithWarnMultipleWarningsHashMap(
138140
return new WithWarnings(withWarnings.value, withWarnings.maxWarnings, isLimitReached, warnsMap);
139141
}
140142

141-
@Specialization(guards = {"interop.hasArrayElements(warnings)", "!isWarnArray(warnings)"})
143+
@Specialization(
144+
guards = {"interop.hasArrayElements(warnings)", "!isError(object)", "!isWarnArray(warnings)"})
142145
WithWarnings doMultipleWarningsInterop(
143146
VirtualFrame frame,
144147
Object object,
@@ -171,6 +174,11 @@ WithWarnings doMultipleWarningsInterop(
171174
return new WithWarnings(value, warnsLimit, isLimitReached, resWarningMap);
172175
}
173176

177+
@Specialization(guards = "isError(object)")
178+
EnsoObject dontAnnotateError(Object object, Object ignoreWarnings) {
179+
return (EnsoObject) object;
180+
}
181+
174182
/** Inserts all {@code warnings} to the {@code initialWarningMap}. */
175183
private EnsoHashMap insertToWarningMap(
176184
VirtualFrame frame,
@@ -199,11 +207,15 @@ private EnsoHashMap insertToWarningMap(
199207
return resWarningMap;
200208
}
201209

202-
protected static boolean isWarnArray(Object obj) {
210+
static boolean isWarnArray(Object obj) {
203211
return obj instanceof Warning[];
204212
}
205213

206-
protected static boolean isWithWarns(Object obj) {
214+
static boolean isWithWarns(Object obj) {
207215
return obj instanceof WithWarnings;
208216
}
217+
218+
static boolean isError(Object obj) {
219+
return obj instanceof DataflowError;
220+
}
209221
}

engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/Warning.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public static Warning create(EnsoContext ctx, Object payload, Object origin) {
5757
description = "Attaches the given warning to the value.",
5858
autoRegister = false)
5959
@Builtin.Specialize
60-
public static WithWarnings attach(
60+
public static EnsoObject attach(
6161
EnsoContext ctx,
6262
Object value,
6363
Object warning,

0 commit comments

Comments
 (0)