vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.83k stars 2.17k forks source link

checker: mutability of if-expression branch returns is not enforced #22653

Open zeozeozeo opened 3 weeks ago

zeozeozeo commented 3 weeks ago

V doctor:

V full version: V 0.4.8 5c65e58.a454f06
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 10 cpus, 64bit, little endian, 

getwd: C:\Users\user\Downloads
vexe: C:\Users\user\Desktop\Code\v\v.exe
vexe mtime: 2024-10-25 17:08:19

vroot: OK, value: C:\Users\user\Desktop\Code\v
VMODULES: OK, value: C:\Users\user\.vmodules
VTMP: OK, value: C:\Users\user\AppData\Local\Temp\v_0

Git version: git version 2.42.0.windows.1
Git vroot status: weekly.2024.43-30-ga454f064
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,

operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 b425ac82

What did you do? ./v -g -o vdbg cmd/v && ./vdbg test.v

fn main() {
    arr := [1, 2, 3] // declared as immutable!
    mut arr_mut := if true { arr } else { []int{} }
    arr_mut[0] = 999
    println(arr) // [999, 2, 3]
}

What did you expect to see?

Compilation error saying that arr is declared as immutable (assigning immutable arr to mutable arr_mut should not be allowed).

What did you see instead?

[999, 2, 3]

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21102

heyimtcn commented 3 weeks ago

it should force cloning the immutable array