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

checker: disallow for mut letter in 'abc' { (#17234)

This commit is contained in:
yuyi 2023-02-06 17:58:03 +08:00 committed by GitHub
parent 858ce4e35d
commit 242d6fa1ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 8 deletions

View File

@ -113,6 +113,9 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
} }
else {} else {}
} }
} else if node.val_is_mut {
c.error('string type is immutable, it cannot be changed', node.pos)
return
} }
if sym.kind == .struct_ { if sym.kind == .struct_ {
// iterators // iterators
@ -139,8 +142,6 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) {
node.kind = sym.kind node.kind = sym.kind
node.val_type = val_type node.val_type = val_type
node.scope.update_var_type(node.val_var, 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 == .any { } else if sym.kind == .any {
node.cond_type = typ node.cond_type = typ
node.kind = sym.kind node.kind = sym.kind

View File

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

View File

@ -19,4 +19,9 @@ fn wrap_text(mut gv Grid) {
for mut ch in gv.header { for mut ch in gv.header {
println(ch) println(ch)
} }
for mut letter in 'abc' {
bit := letter - 1
println(bit)
}
} }