ballerina-platform / ballerina-lang

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

[Bug]: InherentTypeViolation when pushing values to array #43006

Open xlight05 opened 4 months ago

xlight05 commented 4 months ago

Description

$Subject. Happens only when query expression was used. If we assign an array directly, works fine.

Steps to Reproduce

type Complete Sub1|Sub2;

type Sub1 record {
    string test;
};

type Sub2 record {
    string test1;
};

public function main() returns error? {
    Complete[] all = [];
    Sub1[] sub1 = [{test: "test"}];
    all = from Sub1 sub in sub1 select {test: sub.test};
    Sub2 sub2 = {test1: "test1"};
    all.push(sub2);
}

error: {ballerina/lang.array}InherentTypeViolation {"message":"incompatible types: expected 'ai_playground:Sub1', found 'ai_playground:Sub2'"} at ballerina.lang.array.0:push(array.bal:419) wso2.ai_playground.0:main(testz.bal:16)

Works if you use cloneWithType with query expression. all = check (from Sub1 sub in sub1 select {test: sub.test}).cloneWithType();

Affected Version(s)

2201.8.4

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

MaryamZi commented 2 months ago

Spec issue - https://github.com/ballerina-platform/ballerina-spec/issues/1309