checker: add immutable_builtin_modify.vv test (#9702)

pull/9708/head
Enzo 2021-04-13 05:55:41 +02:00 committed by GitHub
parent c3ccb58450
commit 66294e359a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 3 deletions

View File

@ -1245,9 +1245,7 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
}
.array, .string {
// This should only happen in `builtin`
// TODO Remove `crypto.rand` when possible (see vlib/crypto/rand/rand.v,
// if `c_array_to_bytes_tmp` doesn't exist, then it's safe to remove it)
if c.file.mod.name !in ['builtin', 'crypto.rand'] {
if c.file.mod.name != 'builtin' {
c.error('`$typ_sym.kind` can not be modified', expr.pos)
}
}

View File

@ -0,0 +1,11 @@
vlib/v/checker/tests/immutable_builtin_modify.vv:2:3: error: `string` can not be modified
1 | s := ''
2 | s.len = 123
| ~~~
3 | //
4 | b := []byte{}
vlib/v/checker/tests/immutable_builtin_modify.vv:5:3: error: `array` can not be modified
3 | //
4 | b := []byte{}
5 | b.len = 34
| ~~~

View File

@ -0,0 +1,5 @@
s := ''
s.len = 123
//
b := []byte{}
b.len = 34