diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 516bb00157..763a55ac29 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/checker/tests/const_field_add_err.out b/vlib/v/checker/tests/const_field_add_err.out new file mode 100644 index 0000000000..dcae543d9f --- /dev/null +++ b/vlib/v/checker/tests/const_field_add_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/const_field_add_err.vv b/vlib/v/checker/tests/const_field_add_err.vv new file mode 100644 index 0000000000..24a6bb5e9b --- /dev/null +++ b/vlib/v/checker/tests/const_field_add_err.vv @@ -0,0 +1,7 @@ +const ( + a = 1 +) + +fn main() { + a += 1 +} diff --git a/vlib/v/checker/tests/const_field_dec_err.out b/vlib/v/checker/tests/const_field_dec_err.out new file mode 100644 index 0000000000..27e96e62c5 --- /dev/null +++ b/vlib/v/checker/tests/const_field_dec_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/const_field_dec_err.vv b/vlib/v/checker/tests/const_field_dec_err.vv new file mode 100644 index 0000000000..0f9983989a --- /dev/null +++ b/vlib/v/checker/tests/const_field_dec_err.vv @@ -0,0 +1,7 @@ +const ( + a = 1 +) + +fn main() { + a-- +} diff --git a/vlib/v/checker/tests/const_field_inc_err.out b/vlib/v/checker/tests/const_field_inc_err.out new file mode 100644 index 0000000000..b35ef2c98b --- /dev/null +++ b/vlib/v/checker/tests/const_field_inc_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/const_field_inc_err.vv b/vlib/v/checker/tests/const_field_inc_err.vv new file mode 100644 index 0000000000..9d0d96e744 --- /dev/null +++ b/vlib/v/checker/tests/const_field_inc_err.vv @@ -0,0 +1,7 @@ +const ( + a = 1 +) + +fn main() { + a++ +} diff --git a/vlib/v/checker/tests/const_field_sub_err.out b/vlib/v/checker/tests/const_field_sub_err.out new file mode 100644 index 0000000000..daf95cbe53 --- /dev/null +++ b/vlib/v/checker/tests/const_field_sub_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/const_field_sub_err.vv b/vlib/v/checker/tests/const_field_sub_err.vv new file mode 100644 index 0000000000..e6c021bf75 --- /dev/null +++ b/vlib/v/checker/tests/const_field_sub_err.vv @@ -0,0 +1,7 @@ +const ( + a = 1 +) + +fn main() { + a -= 1 +}