Skip to content

Commit

Permalink
Merge pull request #43017 from MaryamZi/fix-43012
Browse files Browse the repository at this point in the history
Fix dependently-typed functions with type reference union member types
  • Loading branch information
gimantha authored Sep 16, 2024
2 parents d2a0167 + aecb98c commit fd93760
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,8 @@ private BType getMatchingTypeForInferrableType(BType originalType, BType expType

for (BType inferableType : inferableTypes) {
for (BType expectedType : expectedTypes) {
if (Types.getImpliedType(inferableType).tag == Types.getImpliedType(expectedType).tag) {
expectedType = Types.getImpliedType(expectedType);
if (Types.getImpliedType(inferableType).tag == expectedType.tag) {
matchedTypes.add(expectedType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ public void testNegatives() {
validateError(errors, indx++, "incompatible type for parameter 't' with inferred typedesc value: expected " +
"'typedesc<(int|string)>', found 'typedesc<boolean>'", 369, 17);
validateError(errors, indx++, "incompatible types: expected 'TargetType', found 'typedesc<boolean>'", 371, 64);
validateError(errors, indx++, "incompatible type for parameter 'td' with inferred typedesc value: expected " +
"'typedesc<anydata>', found 'typedesc'", 383, 24);
Assert.assertEquals(errors.getErrorCount(), indx);
}

Expand Down Expand Up @@ -237,7 +239,8 @@ public Object[][] getFuncNames() {
{"testDependentlyTypedMethodCallOnObjectType"},
{"testDependentlyTypedMethodCallOnObjectTypeWithInferredArgument"},
{"testDependentlyTypedFunctionWithInferredArgForParamOfTypeReferenceType"},
{"testDependentlyTypedResourceMethods"}
{"testDependentlyTypedResourceMethods"},
{"testDependentlyTypedFunctionWithTypeReferenceType"}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ function getArray(typedesc<anydata> td) returns td[] = @java:Method {
paramTypes: ["io.ballerina.runtime.api.values.BTypedesc"]
} external;

function getArrayInferred(typedesc<anydata> td = <>) returns td[] = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.tests.VariableReturnType",
name: "getArray",
paramTypes: ["io.ballerina.runtime.api.values.BTypedesc"]
} external;

function getInvalidValue(typedesc<int|Person> td1, typedesc<Person> td2) returns td1 = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.tests.VariableReturnType",
name: "getInvalidValue",
Expand Down Expand Up @@ -973,6 +979,11 @@ function testDependentlyTypedResourceMethods() {
assert(0, checkpanic c);
}

function testDependentlyTypedFunctionWithTypeReferenceType() returns error? {
IntArray arr = check getArrayInferred();
assert(<int[]>[10, 20, 30], arr);
}

// Util functions
function assert(anydata expected, anydata actual) {
if (expected != actual) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,15 @@ function testDependentlyTypedFunctionWithInferredArgForParamOfTypeReferenceTypeN

var _ = functionWithInferredArgForParamOfTypeReferenceType(boolean);
}

function getArrayInferred(typedesc<anydata> td = <>) returns td[] = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.tests.VariableReturnType",
name: "getArray",
paramTypes: ["io.ballerina.runtime.api.values.BTypedesc"]
} external;

type AnyArray any[];

function testDependentlyTypedFunctionWithTypeReferenceTypeNegative() returns error? {
AnyArray _ = check getArrayInferred();
}

0 comments on commit fd93760

Please sign in to comment.