1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: add error for for mut var in string { (fix #12998) (#13000)

This commit is contained in:
yuyi 2021-12-31 15:54:06 +08:00 committed by GitHub
parent a0a1807e2b
commit 6438512529
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 0 deletions

View File

@ -2590,6 +2590,8 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
node.kind = sym.kind
node.val_type = val_type
node.scope.update_var_type(node.val_var, val_type)
} else if sym.kind == .string && node.val_is_mut {
c.error('string type is immutable, it cannot be changed', node.pos)
} else {
if sym.kind == .map && !(node.key_var.len > 0 && node.val_var.len > 0) {
c.error(

View File

@ -0,0 +1,14 @@
vlib/v/checker/tests/for_in_mut_string.vv:19:6: error: string type is immutable, it cannot be changed
17 |
18 | fn wrap_text(mut gv Grid) {
19 | for mut ch in gv.header {
| ~~~
20 | println(ch)
21 | }
vlib/v/checker/tests/for_in_mut_string.vv:20:3: error: `println` can not print void expressions
18 | fn wrap_text(mut gv Grid) {
19 | for mut ch in gv.header {
20 | println(ch)
| ~~~~~~~~~~~
21 | }
22 | }

View File

@ -0,0 +1,22 @@
module main
[heap]
pub struct Grid {
pub mut:
header string
}
fn main() {
h := 'yo'
mut grid := Grid{
header: h
}
wrap_text(mut grid)
}
fn wrap_text(mut gv Grid) {
for mut ch in gv.header {
println(ch)
}
}