vtereshkov / umka-lang

Umka: a statically typed embeddable scripting language
BSD 2-Clause "Simplified" License
1.05k stars 53 forks source link

`append` stops after the first two appends on an empty dynarr #394

Closed thacuber2a03 closed 4 months ago

thacuber2a03 commented 4 months ago

on an empty dynamic array, it seems to only append the first two elements and stop there

import ( "std.um" )

fn main() {
    arr := make([]uint, 0)
    append(arr, 1)
    append(arr, 2)
    append(arr, 3) // these two don't 
    append(arr, 4) // seem to happen?
    printf("%v", arr) // prints [1 2]
    fprintf(std::stderr(), "%v", arr) // also prints [1 2]
    std::assert(arr[2] == 3) // this throws an index out of bounds error
}

bug seems consistent on both master and stable wasn't a bug, I just didn't notice that

vtereshkov commented 4 months ago

@thacuber2a03 Here is what the language specification says about append:

... If len(a) + len(x) <= cap(a), it [the dynamic array] is altered and returned. Otherwise, a new dynamic array is constructed and returned.

Thus, the normal way to use append is:

a = append(a, x)

If you lose the returned value and don't check cap(a), you'll never know what happens to the argument a.

If you want to save the result to a dynamic array other than a and keep a intact regardless of cap(a), you can use copy:

b = append(copy(a), x)
thacuber2a03 commented 4 months ago

oh, I forgot about that, dang it

thacuber2a03 commented 4 months ago

although, now that this issue is here, @vtereshkov why did you choose that form and not append(&array, value)?