ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.68k stars 751 forks source link

InherentTypeViolation when targetType is used with start keyword #43178

Open xlight05 opened 3 months ago

xlight05 commented 3 months ago

Description: $Subject

Consider the code below :

    future<CentralDocResp|error>[] futureList = [];
    foreach string lib in supportedLibs {
        future<CentralDocResp|error> resp = start bCentralApi->get("docs/" + lib, targetType= CentralDocResp);
        futureList.push(resp);
    }

This doesn't have any compile errors but it gives following error in the runtime.

 error: {ballerina/lang.array}InherentTypeViolation {"message":"incompatible types: expected 'future<(ballerina_copilot.retrieve.central:CentralDocResp|error)>', found 'future<(http:Response|anydata|stream<http:SseEvent,error?>|http:ClientError)>'"}
        at ballerina.lang.array.0:push(array.bal:419)
           wso2.ballerina_copilot:pullCentralApiDocs(retrieve.central.bal:646)
           wso2.ballerina_copilot.0:loadLibraries(retriever.bal:25)

Please move to lang repo if this should be handled in the lang level. Steps to reproduce:

Affected Versions:

OS, DB, other environment details and versions:

Related Issues (optional):

Suggested Labels (optional):

Suggested Assignees (optional):

MaryamZi commented 3 months ago

Looks like a lang issue.

import ballerina/lang.value;

type Person record {|
    int id;
    string name;
|};

public function main() {
    future<Person|error>[] arr = [];

    map<json> p = {name: "Joy", id: 1100};
    future<Person|error> ft = start value:cloneWithType(p, Person);
    arr.push(ft);
}

Also fails at runtime with

error: {ballerina/lang.array}InherentTypeViolation {"message":"incompatible types: expected 'future<(Person|error)>', found 'future<(anydata|error)>'"}
        at ballerina.lang.array.0:push(array.bal:419)
           test:main(test.bal:31)