Skip to content

Commit

Permalink
Always try to resolve conversion for Any type (#6184)
Browse files Browse the repository at this point in the history
Fixes #5898 by removing `Catch.panic` and speeding the `sieve.enso` benchmark from 1058 ms to 514 ms. Should there be no dedicated conversion, let's use one defined on `Any` type - e.g. defining a conversion `from(Any)` makes such a conversion is always available.
  • Loading branch information
JaroslavTulach authored Apr 4, 2023
1 parent ad2645a commit 519df66
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions distribution/lib/Standard/Base/0.0.0-dev/src/Any.enso
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ type Any
== self that =
# If there is No_Such_Conversion, then `self` and `that` are probably
# host or polyglot values, so we just compare them with the default comparator.
eq_self = Panic.catch No_Such_Conversion (Comparable.from self) _-> Default_Comparator
eq_that = Panic.catch No_Such_Conversion (Comparable.from that) _-> Default_Comparator
eq_self = Comparable.from self
eq_that = Comparable.from that
similar_type = Meta.is_same_object eq_self eq_that
case similar_type of
False -> False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,18 @@ Object doFallback(
Object that,
Object[] arguments,
@CachedLibrary(limit = "10") TypesLibrary methods,
@CachedLibrary(limit = "10") InteropLibrary interop) {
throw new PanicException(
EnsoContext.get(this).getBuiltins().error().makeNoSuchConversion(self, that, conversion),
this);
@CachedLibrary(limit = "10") InteropLibrary interop,
@Cached ConversionResolverNode conversionResolverNode) {
var ctx = EnsoContext.get(this);
var function =
conversionResolverNode.execute(
extractConstructor(self), ctx.getBuiltins().any(), conversion);
if (function == null) {
throw new PanicException(
ctx.getBuiltins().error().makeNoSuchConversion(self, that, conversion), this);
} else {
return invokeFunctionNode.execute(function, frame, state, arguments);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Function resolveCached(
@Specialization(replaces = "resolveCached")
@CompilerDirectives.TruffleBoundary
Function resolveUncached(Type target, Type self, UnresolvedConversion conversion) {
return conversion.resolveFor(target, self);
var ctx = EnsoContext.get(this);
return conversion.resolveFor(ctx, target, self);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ModuleScope getScope() {
*
* @return the resolved function definition, or null if not found
*/
public Function resolveFor(Type into, Type from) {
public Function resolveFor(EnsoContext ctx, Type into, Type from) {
Type current = from;
while (current != null) {
Function candidate = scope.lookupConversionDefinition(current, into);
Expand All @@ -52,7 +52,7 @@ public Function resolveFor(Type into, Type from) {
current = current.getSupertype();
}
}
return null;
return scope.lookupConversionDefinition(ctx.getBuiltins().any(), into);
}

@Override
Expand Down
11 changes: 10 additions & 1 deletion test/Tests/src/Semantic/Conversion_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ from Standard.Base import all
import project.Semantic.Conversion.Methods
import project.Semantic.Conversion.Types

from Standard.Test import Test
from Standard.Test import Test, Test_Suite
import Standard.Test.Extensions

polyglot java import java.lang.Object

type Foo
Value foo
type Bar
Expand Down Expand Up @@ -70,6 +72,11 @@ spec =
Not_Foo.from 4 . notfoo . should_equal "ANY!!!"
Not_Foo.from (e -> e) . notfoo . should_equal "ANY!!!"
Not_Foo.from [1,2,3].to_array . notfoo . should_equal "ANY!!!"
Not_Foo.from [1,2,3] . notfoo . should_equal "ANY!!!"
Test.specify "apply Any conversion to foreign object" <|
Not_Foo.from Object.new . notfoo . should_equal "ANY!!!"
Test.specify "apply Any conversion to type" <|
Not_Foo.from Boolean . notfoo . should_equal "ANY!!!"
Test.specify "should call intrinsic object conversions for unimported constructors" <|
Vector.from Methods.get_foo . should_equal ["foo"]
Test.specify "should call extension conversions" <|
Expand Down Expand Up @@ -104,3 +111,5 @@ spec =

meta_from.rename "foo" 123 . should_equal "foo called"
meta_from.rename "foo" . should_equal .foo

main = Test_Suite.run_main spec

0 comments on commit 519df66

Please sign in to comment.