odin-lang / Odin

Odin Programming Language
https://odin-lang.org
BSD 3-Clause "New" or "Revised" License
6.12k stars 550 forks source link

Unable to assign to `#soa` array using struct literal with named fields #3748

Closed Feoramund closed 3 days ago

Feoramund commented 3 weeks ago
package main

S :: struct {i,j: int}

main :: proc () {
    // This evokes an error:
    s1: #soa[2]S = {{i = 1, j = 2}, {i = 3, j = 4}}
/*
    /tmp/odin/soa.odin(7:19) Error: Undeclared name: i
        s1: #soa[2]S = {{i = 1, j = 2}, {i = 3, j = 4}}
                         ^
    /tmp/odin/soa.odin(7:26) Error: Undeclared name: j
        s1: #soa[2]S = {{i = 1, j = 2}, {i = 3, j = 4}}
                                ^
    /tmp/odin/soa.odin(7:35) Error: Undeclared name: i
        s1: #soa[2]S = {{i = 1, j = 2}, {i = 3, j = 4}}
                                         ^
    /tmp/odin/soa.odin(7:42) Error: Undeclared name: j
        s1: #soa[2]S = {{i = 1, j = 2}, {i = 3, j = 4}}
                                                ^
*/

    // This too:
    s2: #soa[2]S = {S{i = 1, j = 2}, S{i = 3, j = 4}}
/*
    /tmp/odin/soa.odin(23:18) Error: Cannot assign value 'S{i = 1, j = 2}' of type 'S' to '[2]int' in structure literal
        s2: #soa[2]S = {S{i = 1, j = 2}, S{i = 3, j = 4}}
                        ^~~~~~~~~~~~~~^
    /tmp/odin/soa.odin(23:35) Error: Cannot assign value 'S{i = 3, j = 4}' of type 'S' to '[2]int' in structure literal
        s2: #soa[2]S = {S{i = 1, j = 2}, S{i = 3, j = 4}}
                                         ^~~~~~~~~~~~~~^
*/

    // This is fine:
    aa:     [2]S = {{i = 1, j = 2}, {i = 3, j = 4}}
    s3: #soa[2]S = {{1, 2}, {3, 4}}
}

Interestingly, if I try to compile that program with both s1 and s2 uncommented (instead of individually), I get this error:

/tmp/odin/soa.odin(7:35) Error: Expected a constant integer as an array field
    s1: #soa[2]S = {{i = 1, j = 2}, {i = 3, j = 4}}
                                     ^~~~^

I'm not sure how related this is to #3515, given this is about arrays, and that one is about slices. Even though it says in that report that #soa arrays are not supported for compound literals, I was able to get the s3 declaration to compile. I'm assuming based on the error message for s2 that it's because #soa[2]S turns into a [2]int and the {{1,2}, ... is parsed just fine.

gingerBill commented 3 days ago

Fixed/disallowed in #3515