checker: check array literal modify error (#9243)
parent
f69cef397c
commit
a547e889af
|
@ -59,7 +59,7 @@ const (
|
||||||
// MT19937RNG is generator that uses the Mersenne Twister algorithm with period 2^19937.
|
// MT19937RNG is generator that uses the Mersenne Twister algorithm with period 2^19937.
|
||||||
pub struct MT19937RNG {
|
pub struct MT19937RNG {
|
||||||
mut:
|
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
|
mti int = mt19937.nn
|
||||||
next_rnd u32
|
next_rnd u32
|
||||||
has_next bool
|
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]')
|
eprintln('mt19937 needs only two 32bit integers as seed: [lower, higher]')
|
||||||
exit(1)
|
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.state = calculate_state(seed_data, mut rng.state)
|
||||||
rng.mti = mt19937.nn
|
rng.mti = mt19937.nn
|
||||||
rng.next_rnd = 0
|
rng.next_rnd = 0
|
||||||
|
|
|
@ -1151,6 +1151,7 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ast.ArrayInit {
|
ast.ArrayInit {
|
||||||
|
c.error('array literal can not be modified', expr.pos)
|
||||||
return '', pos
|
return '', pos
|
||||||
}
|
}
|
||||||
ast.StructInit {
|
ast.StructInit {
|
||||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,4 @@
|
||||||
|
fn main() {
|
||||||
|
mut nums := [1, 2, 3] << 4
|
||||||
|
println(nums)
|
||||||
|
}
|
|
@ -12,7 +12,7 @@ vlib/v/checker/tests/mut_arg.vv:8:3: error: `f` parameter `par` is `mut`, you ne
|
||||||
| ^
|
| ^
|
||||||
9 |
|
9 |
|
||||||
10 | g(mut [3,4])
|
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)
|
8 | f(a)
|
||||||
9 |
|
9 |
|
||||||
10 | g(mut [3,4])
|
10 | g(mut [3,4])
|
||||||
|
|
Loading…
Reference in New Issue