From a547e889af9270d27cbe3f2aabc781091192f559 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 11 Mar 2021 20:57:04 +0800 Subject: [PATCH] checker: check array literal modify error (#9243) --- vlib/rand/mt19937/mt19937.v | 4 +++- vlib/v/checker/checker.v | 1 + vlib/v/checker/tests/array_literal_modify_err.out | 12 ++++++++++++ vlib/v/checker/tests/array_literal_modify_err.vv | 4 ++++ vlib/v/checker/tests/mut_arg.out | 2 +- 5 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/array_literal_modify_err.out create mode 100644 vlib/v/checker/tests/array_literal_modify_err.vv diff --git a/vlib/rand/mt19937/mt19937.v b/vlib/rand/mt19937/mt19937.v index f509642db4..36a8e571e9 100644 --- a/vlib/rand/mt19937/mt19937.v +++ b/vlib/rand/mt19937/mt19937.v @@ -59,7 +59,7 @@ const ( // MT19937RNG is generator that uses the Mersenne Twister algorithm with period 2^19937. pub struct MT19937RNG { mut: - state []u64 = calculate_state(seed.time_seed_array(2), mut []u64{len: mt19937.nn}) + state []u64 = []u64{len: mt19937.nn} mti int = mt19937.nn next_rnd u32 has_next bool @@ -83,6 +83,8 @@ pub fn (mut rng MT19937RNG) seed(seed_data []u32) { eprintln('mt19937 needs only two 32bit integers as seed: [lower, higher]') exit(1) } + // calculate 2 times because MT19937RNG init didn't call calculate_state. + rng.state = calculate_state(seed_data, mut rng.state) rng.state = calculate_state(seed_data, mut rng.state) rng.mti = mt19937.nn rng.next_rnd = 0 diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6f293f72b0..6f7c8275f7 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1151,6 +1151,7 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) { } } ast.ArrayInit { + c.error('array literal can not be modified', expr.pos) return '', pos } ast.StructInit { diff --git a/vlib/v/checker/tests/array_literal_modify_err.out b/vlib/v/checker/tests/array_literal_modify_err.out new file mode 100644 index 0000000000..7c8d8ebed5 --- /dev/null +++ b/vlib/v/checker/tests/array_literal_modify_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/array_literal_modify_err.vv:2:14: error: array literal can not be modified + 1 | fn main() { + 2 | mut nums := [1, 2, 3] << 4 + | ~~~~~~~~~ + 3 | println(nums) + 4 | } +vlib/v/checker/tests/array_literal_modify_err.vv:3:2: error: `println` can not print void expressions + 1 | fn main() { + 2 | mut nums := [1, 2, 3] << 4 + 3 | println(nums) + | ~~~~~~~~~~~~~ + 4 | } diff --git a/vlib/v/checker/tests/array_literal_modify_err.vv b/vlib/v/checker/tests/array_literal_modify_err.vv new file mode 100644 index 0000000000..79c7f97e27 --- /dev/null +++ b/vlib/v/checker/tests/array_literal_modify_err.vv @@ -0,0 +1,4 @@ +fn main() { + mut nums := [1, 2, 3] << 4 + println(nums) +} diff --git a/vlib/v/checker/tests/mut_arg.out b/vlib/v/checker/tests/mut_arg.out index c3d2f1b3b3..f18c285945 100644 --- a/vlib/v/checker/tests/mut_arg.out +++ b/vlib/v/checker/tests/mut_arg.out @@ -12,7 +12,7 @@ vlib/v/checker/tests/mut_arg.vv:8:3: error: `f` parameter `par` is `mut`, you ne | ^ 9 | 10 | g(mut [3,4]) -vlib/v/checker/tests/mut_arg.vv:10:7: error: `g` parameter `par` is not `mut`, `mut` is not needed` +vlib/v/checker/tests/mut_arg.vv:10:7: error: array literal can not be modified 8 | f(a) 9 | 10 | g(mut [3,4])