-
Notifications
You must be signed in to change notification settings - Fork 323
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 type of self
in static dispatch
#8867
Conversation
engine/runtime/src/main/scala/org/enso/interpreter/runtime/IrToTruffle.scala
Show resolved
Hide resolved
|""".stripMargin | ||
eval(code) | ||
consumeOut should contain( | ||
" at <enso> <default::Test.fn::y>(Test:7:10-35)" |
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.
The reported location checked by this test used to be:
- at <enso> <default::Test.fn::Name.Literal(name = y, isMethod = false, location = Some(IdentifiedLocation[location=Location[start=89, end=90], uuid=None]), passData = MetadataStorage[], diagnostics = DiagnosticStorage(diagnostics = List()), id = null)>(Internal)
+ at <enso> <default::Test.fn::y>(Test:7:10-35)
- the name was wrongly too long displaying IR internals
- the location was
Internal
as it was not set correctly
@Test | ||
public void selfTypeConversion() throws Exception { | ||
final URI uri = new URI("memory://self_type_conversion.enso"); | ||
final Source src = | ||
Source.newBuilder( | ||
"enso", | ||
""" | ||
from Standard.Base import all | ||
type My_Type | ||
Value x | ||
f self y = self.x+y | ||
|
||
type Convertible_Type | ||
A x | ||
|
||
type Inconvertible_Type | ||
B x | ||
|
||
My_Type.from (that : Convertible_Type) = My_Type.Value that.x+1 | ||
|
||
static_convertible = My_Type.f (Convertible_Type.A 23) 1000 | ||
static_inconvertible = My_Type.f (Inconvertible_Type.B 23) 1000 | ||
""", | ||
uri.getAuthority()) | ||
.uri(uri) | ||
.buildLiteral(); | ||
|
||
var module = ctx.eval(src); | ||
var convertible = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "static_convertible"); | ||
assertEquals(1024, convertible.asInt()); | ||
|
||
try { | ||
var invalid_static_call = | ||
module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "static_inconvertible"); | ||
fail("Expecting an exception, not: " + invalid_static_call); | ||
} catch (PolyglotException e) { | ||
assertTypeError("`self`", "My_Type", "Inconvertible_Type", e.getMessage()); | ||
} | ||
} |
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.
This test also ensures that not only values of requested type but also values convertible to it are supported, if they go through the conversion.
|
||
var module = ctx.eval(src); | ||
var convertible = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "static_convertible"); | ||
assertEquals(1024, convertible.asInt()); |
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.
We see that the value is 1024
not 1023
, meaning that the conversion has been applied.
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.
Probably better to turn this note into assertEquals("messgage", ...)
message.
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.
Ok, will do
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.
Tests seems to correctly request the desired behavior. The implementation is at surprising (to me) place, but it works and that's what counts.
module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "invalid_static_call"); | ||
fail("Expecting an exception, not: " + invalid_static_call); | ||
} catch (PolyglotException e) { | ||
assertTypeError("`self`", "My_Type", "Other_Type", e.getMessage()); |
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.
OK, the test looks fine.
|
||
var module = ctx.eval(src); | ||
var convertible = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "static_convertible"); | ||
assertEquals(1024, convertible.asInt()); |
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.
Probably better to turn this note into assertEquals("messgage", ...)
message.
engine/runtime-compiler/src/main/scala/org/enso/compiler/pass/resolve/MethodDefinitions.scala
Outdated
Show resolved
Hide resolved
_, | ||
_ | ||
)) :: rest => | ||
selfArg.copy(ascribedType = Some(typ)) :: rest |
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.
I was expecting fix in the IrToTruffle
rather than in the IR passes. Probably because I am more comfortable with the Truffle nodes than the IR...
@radeusgd, you will need to stop delegating from However that's all good - it demonstrates your check really detects violations!
|
oh, right, I completely forgot about it! Of course, so I will need to do a slight refactor to make this work. I will see to it when I have a moment. |
Once you merge with
|
I enabled them and they seem to be passing 🙂 |
Benchmarks trial 1
|
Stdlib Run after fixing remaining warning tests: https://github.com/enso-org/enso/actions/runs/7841630104 And one after changing to the simpler approach suggested by @jdunkerley (instead of extracting common vector/array logic, we just use the Vector methods by converting array by I was unable to run engine benchmarks as these have been failing for a while, I imagine it would be best to try checking these as well before merging this PR... |
# Conflicts: # distribution/lib/Standard/Base/0.0.0-dev/src/Data/Array.enso
@JaroslavTulach I re-requested your review as I have added some additional changes and thought you may want to see them. My initial solution was a bit of a hack apparently - I assumed the type I define methods on is always in scope so I can just refer to that type by its regular name. The test https://github.com/enso-org/enso/blob/wip/radeusgd/8805-self-type-check/test/Base_Tests/src/Semantic/Names_Spec.enso#L8-L12 showed me that we may define methods on a name that is not in the current scope - by extension methods on a type accessed by a (not-necessarily-fully) qualified name. I then tried just using fully qualified names for that - but that failed due to a bug #8997. Towards the
|
… gets rid of the not-really-necessary self type check in that case
Done in 28a4e34.
|
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.
I don't like the from_polyglot_array
wrapping. If engine benchmarks are OK, then maybe... but the right fix was supposed to be to move the implementations into Array_Like_Helpers
.
rationale ... was to simplify the traces for Vector operations
I see. I still continue to like Array_Like_Helpers
solution more.
@@ -159,7 +159,7 @@ type Array | |||
If a `Range`, the selection is specified by two indices, from and to. | |||
@range Index_Sub_Range.default_widget | |||
take : (Index_Sub_Range | Range | Integer) -> Vector Any | |||
take self range=(Index_Sub_Range.First 1) = Vector.take self range | |||
take self range=(Index_Sub_Range.First 1) = (Vector.from_polyglot_array self).take range |
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.
What's the effect of Vector.from_polyglot_array
wrapping on engine benchmarks? I am afraid some of them might slow down.
Wrapping all Array
objects by from_polyglot_array
isn't the expected fix. I was expecting all the static methods to move into Array_Like_Helpers
and both the Array
and Vector
instance methods to delegate to the Array_Like_Helpers
implementations.
Array_Like_Helpers
solution would directly operate on the vector/array. Wrapping by from_polyglot_array
creates a proxy object in memory. All the code accessing it needs to unwrap the original object and then perform the original operation. E.g. we allocate more memory on heap, we need bigger Truffle ASTs (not that we'd measure any such aspect) than Array_Like_Helpers
solution would require.
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.
I did implement the Array_Like_Helpers
solution in the first place, but @jdunkerley suggested the wrapping one might be better.
If we decide to go back to Array_Like_Helpers
, it's just the matter of reverting the commit a8171168789d7252478cc334d82b2e29e51ee024
I guess either way we should wait for the fix of engine benchmarks and see how both solutions compare there.
I'm happy to go for either of these.
Those are StdLib benchmarks, not engine benchmarks! We need to compare the engine benchmarks (which are currently broken, being addressed by @Akirathan as part of his #8975). The engine benchmarks contain microbenchmarks comparing various array-like implementations. They will tell us if the |
Fix of the engine benchmarks is tracked in #9026 with the highest priority. |
As noted below, I ran what I was able to run, but yes I also want to see the engine benchmarks comparison once they are fixed. |
(Engine) benchmark run on current branch state: https://github.com/enso-org/enso/actions/runs/7886066367 Run after reverting the |
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.
I've just checked the benchmarks and I see no slowdown. I still maintain slight preference in favor of Array_Like_Helpers
approach, but approving even the proxy way.
# Conflicts: # CHANGELOG.md
Pull Request Description
self
in the static method call syntax #8805Important Notes
Checklist
Please ensure that the following checklist has been satisfied before submitting the PR:
Scala,
Java,
and
Rust
style guides. In case you are using a language not listed above, follow the Rust style guide.
*BinaryDispatchTest.staticWithRFirst*
tests./run ide build
.