V: unmutated mutables in modules aren't reported correctly

Created on 1 Sep 2019  路  6Comments  路  Source: vlang/v

V version: 0.1.18
OS: MacOS 10.14.6

What did you do?

in mod/mod.v

module mod

struct A {
    a int
}

pub fn load() {
    mut a := []A

    num := 480

    mut i:= 0
    for i < num {
        a << A{}
    }

    println('done')
}

in main.v

import mod

fn main() {
    mod.load()
}

What did you expect to see?
compilation abort: unused variable 'i' in 'load()' in mod.v,

What did you see instead?
successful compilation, unterminated loop, segfault due to out-of-memory error

Bug

Most helpful comment

Ok I'm being silly.

V already does that but right now it's only enabled in module main :)

All 6 comments

Actually, 'i' was used in the code here after definition. If we want V to warn or abort compilation when mutable variable was not mutated - that's a separate question.

you're right. I changed the title. It would help to catch errors that lead to nondescript bugs.

Yes, @avitkauskas is right. Just tested it in Go, and it doesn't complain.

Might be a good idea to do this in loops.

Some languages (don't remember now where I had this) give you a hint on compilation like: "mutable variable x was not changed; consider making in immutable".

Ok I'm being silly.

V already does that but right now it's only enabled in module main :)

Perhaps I saw that in V :D

mut a := 0
println(a)

test.v:1:0: a is declared as mutable, but it was never changed

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lobotony picture lobotony  路  3Comments

penguindark picture penguindark  路  3Comments

aurora picture aurora  路  3Comments

ArcDrake picture ArcDrake  路  3Comments

PavelVozenilek picture PavelVozenilek  路  3Comments