ballerina-platform / ballerina-lang

The Ballerina Programming Language

Home Page:https://ballerina.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Type of added values not accurately checked during compilation for `unshift` operation on tuples

ravinperera00 opened this issue · comments

Description

Currently a few array operations are not supported on tuples at runtime (#41431). However I noticed that the logic on the compiler side is also not fully complete for operations like unshift when used on tuples.

Example:

[int, int, string...] tuple_temp1 = [];
tuple_temp1.unshift(10, "hello");

The above code should not compile because the values that are added to the start of the tuple are of types int and string. But the tuple types strictly allow only int values for those places. Regardless, the program compiles just fine. From what I can gather, this is because the compiler checks for a union of int and string types (int | string) when adding values instead of strictly comparing the newly added member's location and its relevant type for that location. This can be noted when observing the error message displayed when trying to compile the following program.

[int, int, string...] tuple_temp2 = [];
tuple_temp2.unshift(false, "hello");

Error message: incompatible types: expected '(int|string)', found 'boolean'

It seems like the Ballerina spec does not require a compile-time error for these operations (ballerina-platform/ballerina-spec#474) but since it is done in operations like shift, I think it'll be helpful if we can stay consistent with that approach.

Steps to Reproduce

  1. Create a new Ballerina project using bal new <project_path>
  2. Go to the main.bal file and remove ballerina/io package as we won't be needing that.
  3. Add the following code segment within the main function.
[int, int, string...] tuple = [];
tuple.unshift("hello");
  1. Build the program using bal build

You will see the program compile successfully.

Affected Version(s)

No response

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