Avoid duplicated conversion & search target type scope (#7849)

This commit is contained in:
Jaroslav Tulach 2023-09-20 17:23:09 +02:00 committed by GitHub
parent 74d1d0861c
commit 5150c14afd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 9 deletions

View File

@ -42,6 +42,4 @@ type Request_Body
Request_Body.from (that:Text) = Request_Body.Text that Request_Body.from (that:Text) = Request_Body.Text that
Request_Body.from (that:File) = Request_Body.Binary that Request_Body.from (that:File) = Request_Body.Binary that
# TODO: determine if this is needed.
Request_Body.from (that:Request_Body) = that
Request_Body.from (that:Any) = Request_Body.Json that Request_Body.from (that:Any) = Request_Body.Json that

View File

@ -288,6 +288,9 @@ public abstract class ReadArgumentCheckNode extends Node {
} }
private Pair<Function, Type> findConversion(Type from) { private Pair<Function, Type> findConversion(Type from) {
if (expectedType == from) {
return null;
}
var ctx = EnsoContext.get(this); var ctx = EnsoContext.get(this);
if (getRootNode() instanceof EnsoRootNode root) { if (getRootNode() instanceof EnsoRootNode root) {

View File

@ -203,17 +203,23 @@ public final class ModuleScope implements EnsoObject {
} }
@TruffleBoundary @TruffleBoundary
public Function lookupConversionDefinition(Type type, Type target) { public Function lookupConversionDefinition(Type original, Type target) {
Function definedWithAtom = type.getDefinitionScope().getConversionsFor(target).get(type); Function definedWithOriginal =
if (definedWithAtom != null) { original.getDefinitionScope().getConversionsFor(target).get(original);
return definedWithAtom; if (definedWithOriginal != null) {
return definedWithOriginal;
} }
Function definedHere = getConversionsFor(target).get(type); Function definedWithTarget =
target.getDefinitionScope().getConversionsFor(target).get(original);
if (definedWithTarget != null) {
return definedWithTarget;
}
Function definedHere = getConversionsFor(target).get(original);
if (definedHere != null) { if (definedHere != null) {
return definedHere; return definedHere;
} }
return imports.stream() return imports.stream()
.map(scope -> scope.getExportedConversion(type, target)) .map(scope -> scope.getExportedConversion(original, target))
.filter(Objects::nonNull) .filter(Objects::nonNull)
.findFirst() .findFirst()
.orElse(null); .orElse(null);

View File

@ -485,7 +485,7 @@ spec =
test_file = enso_project.data / "does_not_exist.txt" test_file = enso_project.data / "does_not_exist.txt"
Data.post url_post (Request_Body.Binary test_file) . should_fail_with Request_Error Data.post url_post (Request_Body.Binary test_file) . should_fail_with Request_Error
Test.group "Headers" <| Test.group "Headers" pending=pending_has_url <|
base_url_with_slash = if base_url.ends_with "/" then base_url else base_url + "/" base_url_with_slash = if base_url.ends_with "/" then base_url else base_url + "/"
url_post = base_url_with_slash + "post" url_post = base_url_with_slash + "post"

View File

@ -65,6 +65,16 @@ type Fool
Fool.from (that : Any) = Fool.Value that Fool.from (that : Any) = Fool.Value that
type Blob
Text c:Text
Binary b:File
Json anything
Blob.from (that:Text) = Blob.Text that
Blob.from (that:File) = Blob.Binary that
Blob.from (that:Any) = Blob.Json that
spec = spec =
Test.group "Conversion" <| Test.group "Conversion" <|
Test.specify "should be able to convert atoms" <| Test.specify "should be able to convert atoms" <|
@ -162,6 +172,37 @@ spec =
Hello.formulate_with_to [ Hello.Say "Proper", Hello.Say "Type" ] . should_equal "ProperType" Hello.formulate_with_to [ Hello.Say "Proper", Hello.Say "Type" ] . should_equal "ProperType"
Hello.formulate_with_to [ Foo.Value "Perform", Bar.Value "Conversion" ] . should_equal "PERFORM conversion!" Hello.formulate_with_to [ Foo.Value "Perform", Bar.Value "Conversion" ] . should_equal "PERFORM conversion!"
Test.specify "Avoid parameter conversion of Blob into Blob" <|
blob_me (b:Blob) = b
once = blob_me "Ahoj"
second = blob_me once
Meta.type_of once . should_equal Blob
Meta.type_of second . should_equal Blob
once . should_equal second
Meta.is_same_object once second . should_be_true
Test.specify "Avoid Any.to conversion of Blob into Blob" <|
blob_me b = b.to Blob
once = blob_me "Ahoj"
second = blob_me once
Meta.type_of once . should_equal Blob
Meta.type_of second . should_equal Blob
once . should_equal second
Meta.is_same_object once second . should_be_true
Test.specify "Avoid inline conversion of Blob into Blob" <|
once = "Ahoj" : Blob
second = once : Blob
Meta.type_of once . should_equal Blob
Meta.type_of second . should_equal Blob
once . should_equal second
Meta.is_same_object once second . should_be_true
Test.specify "Requesting Text & Foo" <| Test.specify "Requesting Text & Foo" <|
check a (n : Text & Foo) = case a of check a (n : Text & Foo) = case a of
0 -> n.foo 0 -> n.foo