Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector should preserve warnings #3938

Merged
merged 9 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@
- [Import modules' extension methods only with unqualified imports][3906]
- [Don't export polyglot symbols][3915]
- [From/all import must not include module in name resolution][3931]
- [Vector returns warnings of individual elements][3938]

[3227]: https://github.com/enso-org/enso/pull/3227
[3248]: https://github.com/enso-org/enso/pull/3248
Expand Down Expand Up @@ -538,6 +539,7 @@
[3906]: https://github.com/enso-org/enso/pull/3906
[3915]: https://github.com/enso-org/enso/pull/3915
[3931]: https://github.com/enso-org/enso/pull/3931
[3938]: https://github.com/enso-org/enso/pull/3938

# Enso 2.0.0-alpha.18 (2021-10-12)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type Warning
result = warnings.partition w-> predicate w.value
matched = result.first
remaining = result.second
Pair.new (Warning.set remaining value) matched
Pair.new (Warning.set value remaining) matched

## UNSTABLE
A helper function which gathers warnings matching some predicate and passes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.error.PanicSentinel;
import org.enso.interpreter.runtime.error.Warning;
import org.enso.interpreter.runtime.error.WarningsLibrary;
import org.enso.interpreter.runtime.error.WithWarnings;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
import org.enso.interpreter.runtime.state.State;
Expand Down Expand Up @@ -183,6 +184,7 @@ Object doPolyglot(
Object[] arguments,
@CachedLibrary(limit = "10") TypesLibrary methods,
@CachedLibrary(limit = "10") InteropLibrary interop,
@CachedLibrary(limit = "10") WarningsLibrary warnings,
@Cached MethodResolverNode preResolveMethod,
@Bind("getPolyglotCallType(self, symbol, interop, preResolveMethod)")
HostMethodCallNode.PolyglotCallType polyglotCallType,
Expand All @@ -195,17 +197,21 @@ Object doPolyglot(
boolean anyWarnings = false;
ArrayRope<Warning> accumulatedWarnings = new ArrayRope<>();
for (int i = 0; i < argExecutors.length; i++) {
var r = argExecutors[i].executeThunk(arguments[i + 1], state, TailStatus.NOT_TAIL);
args[i] = r;
var r = argExecutors[i].executeThunk(arguments[i + 1], state, TailStatus.NOT_TAIL);
if (r instanceof DataflowError) {
profiles[i].enter();
return r;
} else if (r instanceof WithWarnings w) {
} else if (warnings.hasWarnings(r)) {
hubertp marked this conversation as resolved.
Show resolved Hide resolved
warningProfiles[i].enter();
anyWarnings = true;
accumulatedWarnings =
accumulatedWarnings.append(w.getReassignedWarnings(this));
args[i] = w.getValue();
try {
accumulatedWarnings = accumulatedWarnings.append(warnings.getWarnings(r, this));
args[i] = warnings.removeWarnings(r);
} catch (UnsupportedMessageException e) {
throw new IllegalStateException(e);
}
} else {
args[i] = r;
}
}
Object res = hostMethodCallNode.execute(polyglotCallType, symbol.getName(), self, args);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.NodeInfo;
import org.enso.interpreter.node.ExpressionNode;
Expand Down Expand Up @@ -69,11 +71,16 @@ public Object doPanicSentinel(VirtualFrame frame, PanicSentinel sentinel) {
throw sentinel;
}

@Specialization
Object doWarning(VirtualFrame frame, WithWarnings object) {
ArrayRope<Warning> warnings = object.getReassignedWarnings(this);
Object result = doMatch(frame, object.getValue());
return WithWarnings.appendTo(result, warnings);
@Specialization(guards = {"object != null", "warnings.hasWarnings(object)"})
Object doWarning(
VirtualFrame frame, Object object, @CachedLibrary(limit = "3") WarningsLibrary warnings) {
try {
Warning[] ws = warnings.getWarnings(object, this);
Object result = doMatch(frame, warnings.removeWarnings(object), warnings);
return new WithWarnings(result, ws);
} catch (UnsupportedMessageException e) {
throw new IllegalStateException(e);
}
}

/**
Expand All @@ -84,9 +91,14 @@ Object doWarning(VirtualFrame frame, WithWarnings object) {
* @return the result of executing the case expression on {@code object}
*/
@Specialization(
guards = {"!isDataflowError(object)", "!isPanicSentinel(object)", "!isWarning(object)"})
guards = {
"!isDataflowError(object)",
"!isPanicSentinel(object)",
"!warnings.hasWarnings(object)"
})
@ExplodeLoop
public Object doMatch(VirtualFrame frame, Object object) {
public Object doMatch(
VirtualFrame frame, Object object, @CachedLibrary(limit = "3") WarningsLibrary warnings) {
State state = Function.ArgumentsHelper.getState(frame.getArguments());
try {
for (BranchNode branchNode : cases) {
Expand All @@ -109,10 +121,6 @@ boolean isPanicSentinel(Object sentinel) {
return TypesGen.isPanicSentinel(sentinel);
}

boolean isWarning(Object warning) {
return warning instanceof WithWarnings;
}

/* Note [Branch Selection Control Flow]
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Truffle provides no easy way to return control flow from multiple paths. This is entirely due
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.profiles.BranchProfile;
Expand All @@ -10,6 +11,7 @@
import org.enso.interpreter.runtime.callable.atom.AtomConstructor;
import org.enso.interpreter.runtime.data.ArrayRope;
import org.enso.interpreter.runtime.error.Warning;
import org.enso.interpreter.runtime.error.WarningsLibrary;
import org.enso.interpreter.runtime.error.WithWarnings;
import org.enso.interpreter.runtime.type.TypesGen;

Expand All @@ -21,6 +23,7 @@
public class InstantiateNode extends ExpressionNode {
private final AtomConstructor constructor;
private @Children ExpressionNode[] arguments;
private @Child WarningsLibrary warnings = WarningsLibrary.getFactory().createDispatched(3);
private @CompilationFinal(dimensions = 1) ConditionProfile[] profiles;
private @CompilationFinal(dimensions = 1) ConditionProfile[] warningProfiles;
private @CompilationFinal(dimensions = 1) BranchProfile[] sentinelProfiles;
Expand Down Expand Up @@ -70,11 +73,14 @@ public Object executeGeneric(VirtualFrame frame) {
Object argument = arguments[i].executeGeneric(frame);
if (profile.profile(TypesGen.isDataflowError(argument))) {
return argument;
} else if (warningProfile.profile(argument instanceof WithWarnings)) {
} else if (warningProfile.profile(warnings.hasWarnings(argument))) {
anyWarnings = true;
WithWarnings originalArg = (WithWarnings) argument;
accumulatedWarnings = accumulatedWarnings.append(originalArg.getReassignedWarnings(this));
argumentValues[i] = originalArg.getValue();
try {
accumulatedWarnings = accumulatedWarnings.append(warnings.getWarnings(argument, this));
argumentValues[i] = warnings.removeWarnings(argument);
} catch (UnsupportedMessageException e) {
throw new IllegalStateException(e);
}
} else if (TypesGen.isPanicSentinel(argument)) {
sentinelProfile.enter();
throw TypesGen.asPanicSentinel(argument);
Expand Down
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 @@ -5,12 +5,15 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.callable.atom.Atom;
import org.enso.interpreter.runtime.data.text.Text;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.error.WarningsLibrary;
import org.enso.interpreter.runtime.error.WithWarnings;

public abstract class ExpectStringNode extends Node {
private @Child InteropLibrary library = InteropLibrary.getFactory().createDispatched(10);
Expand All @@ -31,6 +34,15 @@ String doString(String str) {
return str;
}

@Specialization(guards = "warnings.hasWarnings(warning)")
String doWarning(Object warning, @CachedLibrary(limit = "3") WarningsLibrary warnings) {
try {
return execute(warnings.removeWarnings(warning));
} catch (UnsupportedMessageException e) {
throw new IllegalStateException(e);
}
}

@Fallback
String doFallback(Object str) {
try {
Expand Down
Loading