-
Notifications
You must be signed in to change notification settings - Fork 327
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
Delay check of suspended arguments until they are about to be computed #7727
Merged
JaroslavTulach
merged 3 commits into
develop
from
wip/jtulach/SuspendedArgumentsTypeCheck_6883
Sep 6, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
|
||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.PolyglotException; | ||
import org.graalvm.polyglot.Source; | ||
import org.graalvm.polyglot.Value; | ||
import org.junit.AfterClass; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
import org.junit.BeforeClass; | ||
|
@@ -96,6 +98,149 @@ public void runtimeCheckOfAscribedFunctionSignature() throws Exception { | |
assertTrue("Yields Error value", yieldsError.isException()); | ||
} | ||
|
||
@Test | ||
public void lazyIntegerInConstructor() throws Exception { | ||
final URI uri = new URI("memory://int_simple_complex.enso"); | ||
final Source src = Source.newBuilder("enso", """ | ||
from Standard.Base import all | ||
|
||
type Int | ||
Simple v | ||
Complex (~unwrap : Int) | ||
|
||
value self = case self of | ||
Int.Simple v -> v | ||
Int.Complex unwrap -> unwrap.value | ||
|
||
+ self (that:Int) = Int.Simple self.value+that.value | ||
|
||
simple v = Int.Simple v | ||
complex x y = Int.Complex (x+y) | ||
""", uri.getHost()) | ||
.uri(uri) | ||
.buildLiteral(); | ||
|
||
var module = ctx.eval(src); | ||
|
||
var simple = module.invokeMember("eval_expression", "simple"); | ||
var complex = module.invokeMember("eval_expression", "complex"); | ||
|
||
var six = simple.execute(6); | ||
var seven = simple.execute(7); | ||
var some13 = complex.execute(six, seven); | ||
var thirteen = some13.invokeMember("value"); | ||
assertNotNull("member found", thirteen); | ||
assertEquals(13, thirteen.asInt()); | ||
|
||
var someHello = complex.execute("Hello", "World"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constructing |
||
try { | ||
var error = someHello.invokeMember("value"); | ||
fail("not expecting any value: " + error); | ||
} catch (PolyglotException e) { | ||
assertTypeError("`unwrap`", "Int", "Text", e.getMessage()); | ||
} | ||
try { | ||
var secondError = someHello.invokeMember("value"); | ||
fail("not expecting any value again: " + secondError); | ||
} catch (PolyglotException e) { | ||
assertTypeError("`unwrap`", "Int", "Text", e.getMessage()); | ||
} | ||
} | ||
|
||
@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 <| | ||
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"; | ||
try { | ||
var error = neg.execute(-5); | ||
fail("Expecting an error: " + error); | ||
} catch (PolyglotException ex) { | ||
assertTypeError("`zero`", "Integer", "Text", ex.getMessage()); | ||
} | ||
|
||
zeroValue[0] = 0; | ||
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"); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set.execute
was failing withSuspendedException
because the lazy field are runtime type checked andSuspendedException
didn't have the right type ;-)Changing into
DataflowError
as that isisAllFitValue
.