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.86k stars 2.17k forks source link

checker error `cannot access fields of an Option` for `if optval != none {` where `optval` is muttable #22936

Closed spytheman closed 2 days ago

spytheman commented 5 days ago

V doctor:

V full version: V 0.4.8 2fbb3df.58fc4de
OS: linux, Ubuntu 20.04.6 LTS
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /home/delian/code/v
vexe: /home/delian/code/v/v
vexe mtime: 2024-11-21 18:27:40

vroot: OK, value: /home/delian/code/v
VMODULES: OK, value: /home/delian/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.46.0
Git vroot status: weekly.2024.47-13-g58fc4dea
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
emcc version: N/A
thirdparty/tcc status: thirdparty-linux-amd64 0134e9b9

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

mut
entrykey := ?string(none)
if entrykey != none {
    println('entrykey is a string')
    println(entrykey.len)
    println(entrykey)
    if entrykey.len > 0 {
        println('ok')
    }
} else {
    println('entrykey was none')
}

What did you expect to see?

A compiled program. entrykey should be considered immutable inside the smartcasted block

What did you see instead?

pp.v:5:19: error: cannot access fields of an Option, handle the error with `or {...}` or propagate it with `?`
    3 | if entrykey != none {
    4 |     println('entrykey is a string')
    5 |     println(entrykey.len)
      |                      ~~~
    6 |     println(entrykey)
    7 |     if entrykey.len > 0 {
pp.v:7:14: error: cannot access fields of an Option, handle the error with `or {...}` or propagate it with `?`
    5 |     println(entrykey.len)
    6 |     println(entrykey)
    7 |     if entrykey.len > 0 {
      |                 ~~~
    8 |         println('ok')
    9 |     }

[!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-21378

spytheman commented 5 days ago

Note: commenting the mut at the start, makes the program compile and run fine.