Skip to content

Commit

Permalink
Propagate all vector warnings when reading element
Browse files Browse the repository at this point in the history
Previously, accessing an element of an Array-like structure would only
return warnings of that element or of the structure itself.
Now, accessing an element also returns warnings from all its elements as
well.
  • Loading branch information
hubertp committed Dec 6, 2022
1 parent 74df323 commit fc0b232
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.Vector;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.error.WarningsLibrary;

@BuiltinMethod(
type = "Vector",
name = "at",
description = "Returns an element of Vector at the specified index.")
public class AtVectorNode extends Node {
private @Child InteropLibrary interop = InteropLibrary.getFactory().createDispatched(3);
private @Child WarningsLibrary warnings = WarningsLibrary.getFactory().createDispatched(3);
private @Child HostValueToEnsoNode convert = HostValueToEnsoNode.build();

Object execute(Vector self, long index) {
Expand All @@ -31,7 +33,7 @@ Object execute(Vector self, long index) {
private Object readElement(Vector self, long index) throws UnsupportedMessageException {
try {
long actualIndex = index < 0 ? index + self.length(interop) : index;
return self.readArrayElement(actualIndex, interop, convert);
return self.readArrayElement(actualIndex, interop, warnings, convert);
} catch (InvalidArrayIndexException e) {
EnsoContext ctx = EnsoContext.get(this);
return DataflowError.withoutTrace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,21 @@ public boolean hasArrayElements() {
* @throws InvalidArrayIndexException when the index is out of bounds.
*/
@ExportMessage
public Object readArrayElement(long index) throws InvalidArrayIndexException {
public Object readArrayElement(long index, @CachedLibrary(limit = "3") WarningsLibrary warnings)
throws InvalidArrayIndexException, UnsupportedMessageException {
if (index >= items.length || index < 0) {
throw InvalidArrayIndexException.create(index);
}
return items[(int) index];

var v = items[(int) index];
if (this.hasWarnings(warnings)) {
Warning[] extracted = this.getWarnings(null, warnings);
if (warnings.hasWarnings(v)) {
v = warnings.removeWarnings(v);
}
return new WithWarnings(v, extracted);
}
return v;
}

public long length() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.enso.interpreter.node.expression.builtin.interop.syntax.HostValueToEnsoNode;
import org.enso.interpreter.runtime.error.Warning;
import org.enso.interpreter.runtime.error.WarningsLibrary;
import org.enso.interpreter.runtime.error.WithWarnings;

@ExportLibrary(InteropLibrary.class)
@ExportLibrary(WarningsLibrary.class)
Expand Down Expand Up @@ -67,13 +68,21 @@ public long getArraySize(@CachedLibrary(limit = "3") InteropLibrary interop)
public Object readArrayElement(
long index,
@CachedLibrary(limit = "3") InteropLibrary interop,
@CachedLibrary(limit = "3") WarningsLibrary warnings,
@Cached HostValueToEnsoNode toEnso)
throws InvalidArrayIndexException, UnsupportedMessageException {
if (index < 0 || index >= getArraySize(interop)) {
throw InvalidArrayIndexException.create(index);
}

var v = interop.readArrayElement(storage, start + index);
if (this.hasWarnings(warnings)) {
Warning[] extracted = this.getWarnings(null, warnings);
if (warnings.hasWarnings(v)) {
v = warnings.removeWarnings(v);
}
return new WithWarnings(toEnso.execute(v), extracted);
}
return toEnso.execute(v);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,18 @@ public long getArraySize(@CachedLibrary(limit = "3") InteropLibrary interop)
public Object readArrayElement(
long index,
@CachedLibrary(limit = "3") InteropLibrary interop,
@CachedLibrary(limit = "3") WarningsLibrary warnings,
@Cached HostValueToEnsoNode toEnso)
throws InvalidArrayIndexException, UnsupportedMessageException {
var v = interop.readArrayElement(storage, index);
if (warnings.hasWarnings(this)) {
Warning[] extracted = warnings.getWarnings(this, null);
if (warnings.hasWarnings(v)) {
v = warnings.removeWarnings(v);
}
;
return new WithWarnings(toEnso.execute(v), extracted);
}
return toEnso.execute(v);
}

Expand Down Expand Up @@ -188,7 +197,9 @@ String toDisplayString(boolean allowSideEffects) {
for (long i = 0; i < len; i++) {
sb.append(sep);

Object at = readArrayElement(i, iop, HostValueToEnsoNode.getUncached());
Object at =
readArrayElement(
i, iop, WarningsLibrary.getUncached(), HostValueToEnsoNode.getUncached());
Object str = iop.toDisplayString(at, allowSideEffects);
if (iop.isString(str)) {
sb.append(iop.asString(str));
Expand Down
4 changes: 2 additions & 2 deletions test/Tests/src/Semantic/Warnings_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ spec = Test.group "Dataflow Warnings" <|
res . should_equal [1, 0, 1, 0]
Warning.get_all res . map .value . should_equal [4, 3, 2, 1]
res.at 0 . should_equal 1
Warning.get_all (res.at 0) . map .value . should_equal [1]
Warning.get_all (res.at 0) . map .value . should_equal [4, 3, 2, 1]

slice = res.slice 1 4
Warning.get_all slice . map .value . should_equal [4, 3, 2, 1]
Expand All @@ -220,6 +220,6 @@ spec = Test.group "Dataflow Warnings" <|

res . should_equal [[0], [0, 1], [0, 1, 0], [0, 1, 0, 1]]
Warning.get_all res . map .value . should_equal [3, 2, 1, 0, 2, 1, 0, 1, 0, 0]
Warning.get_all (res.at 2) . map .value . should_equal [2, 1, 0]
Warning.get_all (res.at 2) . map .value . should_equal [3, 2, 1, 0, 2, 1, 0, 1, 0, 0]

main = Test_Suite.run_main spec

0 comments on commit fc0b232

Please sign in to comment.