Multiple Shadowing Mistakes

Since the beginning of using Odin, the most common mistake I make is like this:

make_test_data :: proc(iterations: int, seed: int) -> []int {
    seed := seed
    data : []int
    for iteration in 0..iterations {
        data := make([]int, random_int())   
    }
    return data

This returns nothing, because I re-initialized data with := rather than setting its value with just =.

If you are unfortunate enough to miss this (a lot easier to miss in non-trivial examples) it can be a very non-obvious source of bugs.

A compiler warning or error would trivially prevent this. It’s rare a reasonable person would want to re-use variable names in nested scopes like this. Downside is it breaks code for people that have used the same names in nested scopes, unless of course just a warning.

1 Like

Try building/checking with -vet-shadowing. That specifically enables errors for just this kind of thing.

You can also just use -vet, which includes -vet-shadowing and a few other checks as well.

4 Likes

Oh fantastic - sorry I missed that. Thank you.

No sweat, there are a lot of flags to the compiler to look through. There are a number of -vet flags that are worth taking a look at, though.