Skip to content

Commit

Permalink
reformat after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Dec 16, 2023
1 parent 26a433f commit f44a285
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.enso.compiler.core.ir.Name;
import org.enso.interpreter.EnsoLanguage;
import org.enso.interpreter.node.BaseNode.TailStatus;
import org.enso.interpreter.node.EnsoRootNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.net.URI;
import java.nio.file.Paths;
import java.util.logging.Level;

import org.enso.polyglot.MethodNames;
import org.enso.polyglot.RuntimeOptions;
import org.graalvm.polyglot.Context;
Expand Down Expand Up @@ -249,7 +248,10 @@ member_foo self (y : Integer) z -> Integer = 100*z + 10*y + self.x

@Test
public void inlineReturnSignatureOnLocalFunction() throws Exception {
var module = ctx.eval("enso", """
var module =
ctx.eval(
"enso",
"""
foo x y =
inner_foo (z : Integer) -> Integer = 100*z + 10*y + x
a = 3
Expand All @@ -271,19 +273,24 @@ public void inlineReturnSignatureWithoutArguments() throws Exception {
}

/**
* This test demonstrates a slightly un-intuitive, but apparently needed by our rules, behaviour of `->` with ascriptions:
* 1. for `foo a:Integer -> Integer` it is interpreted as foo (a:Integer) -> Integer - i.e. a function taking an Integer and returning an Integer.
* 2. for `foo a : Integer -> Integer`, this results in a compile error currently.
* This test demonstrates a slightly un-intuitive, but apparently needed by our rules, behaviour
* of `->` with ascriptions: 1. for `foo a:Integer -> Integer` it is interpreted as foo
* (a:Integer) -> Integer - i.e. a function taking an Integer and returning an Integer. 2. for
* `foo a : Integer -> Integer`, this results in a compile error currently.
*/
@Test
public void weirdReturnTypeSignature1() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer
foo a:Integer -> Integer = a+10
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var foo = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "foo");
Expand All @@ -293,12 +300,16 @@ public void weirdReturnTypeSignature1() throws Exception {
@Test
public void weirdReturnTypeSignature2() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer
foo a : Integer -> Integer = a+10
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

try {
var module = ctx.eval(src);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,12 @@ public void andConversions() throws Exception {
@Test
public void unresolvedReturnTypeSignature() throws Exception {
final URI uri = new URI("memory://neg.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder("enso", """
neg a -> Xyz = 0 - a
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""", uri.getAuthority())
.uri(uri)
.buildLiteral();

try {
var module = ctx.eval(src);
Expand All @@ -830,13 +831,17 @@ public void unresolvedReturnTypeSignature() throws Exception {
@Test
public void validReturnTypeSignature() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer
add1 a b -> Integer = a+b
add2 (a : Integer) (b : Integer) -> Integer = a+b
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var add1 = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "add1");
Expand All @@ -849,12 +854,16 @@ public void validReturnTypeSignature() throws Exception {
@Test
public void returnTypeCheckOptInError() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer
plusChecked a b -> Integer = b+a
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var plusChecked = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "plusChecked");
Expand All @@ -867,17 +876,24 @@ public void returnTypeCheckOptInError() throws Exception {
}
}

/** Similar scenario to {@code returnTypeCheckOptInError}, but with the opt out signature the check is not currently performed. */
/**
* Similar scenario to {@code returnTypeCheckOptInError}, but with the opt out signature the check
* is not currently performed.
*/
@Test
public void returnTypeCheckOptOut() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer
plusUnchecked : Integer -> Integer -> Integer
plusUnchecked a b = b+a
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var plusChecked = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "plusUnchecked");
Expand All @@ -889,13 +905,17 @@ public void returnTypeCheckOptOut() throws Exception {
@Test
public void returnTypeCheckOptInErrorZeroArguments() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer
constant -> Integer = "foo"
foo a b = a + constant + b
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var plusChecked = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "foo");
Expand All @@ -907,18 +927,21 @@ public void returnTypeCheckOptInErrorZeroArguments() throws Exception {
}
}


@Test
public void returnTypeCheckOptInErrorZeroArgumentsExpression() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer
foo a =
x -> Integer = a+a
x+x
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var plusChecked = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "foo");
Expand All @@ -934,15 +957,19 @@ public void returnTypeCheckOptInErrorZeroArgumentsExpression() throws Exception
@Test
public void returnTypeCheckOptInErrorZeroArgumentsBlock() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer, IO
foo a =
x -> Integer =
a+a
x+x
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var plusChecked = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "foo");
Expand All @@ -958,16 +985,20 @@ public void returnTypeCheckOptInErrorZeroArgumentsBlock() throws Exception {
@Test
public void returnTypeCheckOptInAllowDataflowErrors() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer, Error
foo x -> Integer = case x of
1 -> 100
2 -> "TWO"
3 -> Error.throw "My error"
_ -> x+1
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var foo = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "foo");
Expand All @@ -988,17 +1019,21 @@ public void returnTypeCheckOptInAllowDataflowErrors() throws Exception {
@Test
public void returnTypeCheckOptInTailRec() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer, Error
factorial (x : Integer) -> Integer =
go n acc -> Integer =
if n == 0 then acc else
if n == 10 then "TEN :)" else
@Tail_Call go (n-1) (acc*n)
go x 1
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var factorial = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "factorial");
Expand All @@ -1014,23 +1049,28 @@ public void returnTypeCheckOptInTailRec() throws Exception {
}

/**
* An additional test to ensure that the way the return type check is implemented does not break the TCO. We execute a
* recursive computation that goes 100k frames deep - if TCO did not kick in here, we'd get a stack overflow.
* An additional test to ensure that the way the return type check is implemented does not break
* the TCO. We execute a recursive computation that goes 100k frames deep - if TCO did not kick in
* here, we'd get a stack overflow.
*/
@Test
public void returnTypeCheckOptInTCO() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer, Error
foo (counter : Integer) (trap : Integer) -> Integer =
go i acc -> Integer =
if i == 0 then acc else
if i == trap then "TRAP!" else
@Tail_Call go (i-1) (acc+1)
go counter 1
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var foo = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "foo");
Expand All @@ -1047,17 +1087,21 @@ public void returnTypeCheckOptInTCO() throws Exception {
@Test
public void returnTypeCheckOptInTCO2() throws Exception {
final URI uri = new URI("memory://rts.enso");
final Source src = Source.newBuilder("enso", """
final Source src =
Source.newBuilder(
"enso",
"""
from Standard.Base import Integer, Error
foo_ok counter -> Integer =
if counter == 0 then 0 else
@Tail_Call foo_ok (counter-1)
foo_bad counter -> Integer =
if counter == 0 then "ZERO" else
@Tail_Call foo_bad (counter-1)
""",uri.getAuthority())
.uri(uri)
.buildLiteral();
""",
uri.getAuthority())
.uri(uri)
.buildLiteral();

var module = ctx.eval(src);
var foo_ok = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "foo_ok");
Expand Down

0 comments on commit f44a285

Please sign in to comment.