checker: prevent C error on const mutation
parent
38277d1dac
commit
3a3d00ac72
|
@ -502,6 +502,8 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) {
|
|||
c.error('`$it.name` is immutable, declare it with `mut` to make it mutable',
|
||||
it.pos)
|
||||
}
|
||||
} else if it.name in c.const_names {
|
||||
c.error('cannot assign to constant `$it.name`', it.pos)
|
||||
}
|
||||
}
|
||||
ast.IndexExpr {
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
vlib/v/checker/tests/const_field_add_err.v:6:2: error: cannot assign to constant `a`
|
||||
4 |
|
||||
5 | fn main() {
|
||||
6 | a += 1
|
||||
| ^
|
||||
7 | }
|
|
@ -0,0 +1,7 @@
|
|||
const (
|
||||
a = 1
|
||||
)
|
||||
|
||||
fn main() {
|
||||
a += 1
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
vlib/v/checker/tests/const_field_dec_err.v:6:2: error: cannot assign to constant `a`
|
||||
4 |
|
||||
5 | fn main() {
|
||||
6 | a--
|
||||
| ^
|
||||
7 | }
|
|
@ -0,0 +1,7 @@
|
|||
const (
|
||||
a = 1
|
||||
)
|
||||
|
||||
fn main() {
|
||||
a--
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
vlib/v/checker/tests/const_field_inc_err.v:6:2: error: cannot assign to constant `a`
|
||||
4 |
|
||||
5 | fn main() {
|
||||
6 | a++
|
||||
| ^
|
||||
7 | }
|
|
@ -0,0 +1,7 @@
|
|||
const (
|
||||
a = 1
|
||||
)
|
||||
|
||||
fn main() {
|
||||
a++
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
vlib/v/checker/tests/const_field_sub_err.v:6:2: error: cannot assign to constant `a`
|
||||
4 |
|
||||
5 | fn main() {
|
||||
6 | a -= 1
|
||||
| ^
|
||||
7 | }
|
|
@ -0,0 +1,7 @@
|
|||
const (
|
||||
a = 1
|
||||
)
|
||||
|
||||
fn main() {
|
||||
a -= 1
|
||||
}
|
Loading…
Reference in New Issue