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

Check types of lazy (constructor) arguments #6883

Closed
JaroslavTulach opened this issue May 30, 2023 · 3 comments · Fixed by #7727
Closed

Check types of lazy (constructor) arguments #6883

JaroslavTulach opened this issue May 30, 2023 · 3 comments · Fixed by #7727
Assignees

Comments

@JaroslavTulach
Copy link
Member

Follow up of #6682:

    from Standard.Base import Integer

    type Maybe a
        Nothing
        Some (~unwrap : Integer)

one can instantiate Some with non-Integer values. That shouldn't be possible and should yield an error. Similar checks shall be performed for suspended method arguments.

@JaroslavTulach JaroslavTulach added this to the Beta Release milestone May 30, 2023
@JaroslavTulach JaroslavTulach self-assigned this May 30, 2023
@jdunkerley jdunkerley moved this from ❓New to 📤 Backlog in Issues Board May 30, 2023
@jdunkerley jdunkerley removed this from the Beta Release milestone Jun 27, 2023
@JaroslavTulach
Copy link
Member Author

Basic test written:

enso$ git diff engine/runtime/src/test/java/org/enso/interpreter/test/SignatureTest.java
diff --git engine/runtime/src/test/java/org/enso/interpreter/test/SignatureTest.java engine/runtime/src/test/java/org/enso/interpreter/test/SignatureTest.java
index 6ec4136f48..3d79a8d457 100644
--- engine/runtime/src/test/java/org/enso/interpreter/test/SignatureTest.java
+++ engine/runtime/src/test/java/org/enso/interpreter/test/SignatureTest.java
@@ -2,6 +2,7 @@ package org.enso.interpreter.test;
 
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
 
 import org.graalvm.polyglot.Context;
 import org.graalvm.polyglot.PolyglotException;
@@ -96,6 +97,96 @@ public class SignatureTest extends TestBase {
     assertTrue("Yields Error value", yieldsError.isException());
   }
 
+  @Test
+  public void runtimeCheckOfLazyAscribedFunctionSignature() throws Exception {
+    final URI uri = new URI("memory://neg_lazy.enso");
+    final Source src = Source.newBuilder("enso", """
+    from Standard.Base import Integer, IO
+
+    build (~zero : Integer) =
+      neg (~a : Integer) = zero - a
+      neg
+
+    make arr = build <|
+      IO.println "returning"+(arr.at 0 . to_text)
+      arr.at 0
+    """, uri.getHost())
+            .uri(uri)
+            .buildLiteral();
+
+    var module = ctx.eval(src);
+
+    var zeroValue = new Object[] { 0 };
+    var neg = module.invokeMember("eval_expression", "make").execute((Object)zeroValue);
+
+    zeroValue[0] = "Wrong";
+    var error = neg.execute(-5);
+    assertEquals("Just ten as the zeroValue[0] is always read again", 10, error.asInt());
+
+    var five = neg.execute(-5);
+    assertEquals("Five", 5, five.asInt());
+
+    try {
+      var res = neg.execute("Hi");
+      fail("Expecting an exception, not: " + res);
+    } catch (PolyglotException e) {
+      assertTypeError("`a`", "Integer", "Text", e.getMessage());
+    }
+    zeroValue[0] = 5;
+    var fifteen = neg.execute(-10);
+    assertEquals("Five + Ten as the zeroValue[0] is always read again", 15, fifteen.asInt());
+
+    zeroValue[0] = 0;
+    var ten = neg.execute(-10);
+    assertEquals("Just ten as the zeroValue[0] is always read again", 10, ten.asInt());
+  }
+
+  @Test
+  public void runtimeCheckOfLazyAscribedConstructorSignature() throws Exception {
+    final URI uri = new URI("memory://neg_lazy_const.enso");
+    final Source src = Source.newBuilder("enso", """
+    from Standard.Base import Integer, IO, Polyglot
+
+    type Lazy
+        Value (~zero : Integer)
+
+        neg self (~a : Integer) = self.zero - a
+
+    make arr = Lazy.Value <|
+      Polyglot.invoke arr "add" [ arr.length ]
+      arr.at 0
+    """, uri.getHost())
+            .uri(uri)
+            .buildLiteral();
+
+    var module = ctx.eval(src);
+
+    var zeroValue = new ArrayList<Integer>();
+    zeroValue.add(0);
+    var lazy = module.invokeMember("eval_expression", "make").execute((Object)zeroValue);
+    assertEquals("No read from zeroValue, still size 1", 1, zeroValue.size());
+
+    var five = lazy.invokeMember("neg", -5);
+    assertEquals("Five", 5, five.asInt());
+    assertEquals("One read from zeroValue, size 2", 2, zeroValue.size());
+
+    try {
+      var res = lazy.invokeMember("neg", "Hi");
+      fail("Expecting an exception, not: " + res);
+    } catch (PolyglotException e) {
+      assertTypeError("`a`", "Integer", "Text", e.getMessage());
+    }
+    zeroValue.set(0, 5);
+    var fifteen = lazy.invokeMember("neg", -10);
+    assertEquals("Five + Ten as the zeroValue[0] is never read again", 10, fifteen.asInt());
+    assertEquals("One read from zeroValue, size 2", 2, zeroValue.size());
+
+    zeroValue.set(0, 0);
+    var ten = lazy.invokeMember("neg", -9);
+    assertEquals("Just nine as the zeroValue[0] is always read again", 9, ten.asInt());
+    assertEquals("One read from zeroValue, size 2", 2, zeroValue.size());
+  }
+
   @Test
   public void runtimeCheckOfAscribedInstanceMethodSignature() throws Exception {
     final URI uri = new URI("memory://twice_instance.enso");

@JaroslavTulach JaroslavTulach moved this from 📤 Backlog to 🔧 Implementation in Issues Board Sep 4, 2023
@enso-bot
Copy link

enso-bot bot commented Sep 5, 2023

Jaroslav Tulach reports a new STANDUP for yesterday (2023-09-04):

Progress: - investigating "suggestion DB & conversions": #7716 (comment)

Next Day: bugfixing

GitHub
Pull Request Description This change replaces Enso's custom logger with an existing, mostly off the shelf logging implementation. The change attempts to provide a 1:1 replacement for the existi...

@enso-bot
Copy link

enso-bot bot commented Sep 6, 2023

Jaroslav Tulach reports a new STANDUP for yesterday (2023-09-05):

Progress: - thinking about yjs: https://github.com/yjs/yjs

Next Day: Planning & bugfixing

GitHub
Shared data types for building collaborative software - GitHub - yjs/yjs: Shared data types for building collaborative software

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants