mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: add a check for str[i] = a
(#6107)
This commit is contained in:
parent
99798b83b4
commit
9fdb1701e0
@ -3076,6 +3076,9 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) table.Type {
|
|||||||
typ_sym.name.ends_with('ptr')) && !typ.has_flag(.variadic) { // byteptr, charptr etc
|
typ_sym.name.ends_with('ptr')) && !typ.has_flag(.variadic) { // byteptr, charptr etc
|
||||||
c.error('type `$typ_sym.name` does not support indexing', node.pos)
|
c.error('type `$typ_sym.name` does not support indexing', node.pos)
|
||||||
}
|
}
|
||||||
|
if typ_sym.kind == .string && !typ.is_ptr() && node.is_setter {
|
||||||
|
c.error('cannot assign to s[i] (strings are immutable)', node.pos)
|
||||||
|
}
|
||||||
if !c.inside_unsafe && (typ.is_ptr() || typ.is_pointer()) {
|
if !c.inside_unsafe && (typ.is_ptr() || typ.is_pointer()) {
|
||||||
mut is_ok := false
|
mut is_ok := false
|
||||||
if node.left is ast.Ident {
|
if node.left is ast.Ident {
|
||||||
|
6
vlib/v/checker/tests/string_index_assign_error.out
Normal file
6
vlib/v/checker/tests/string_index_assign_error.out
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
vlib/v/checker/tests/string_index_assign_error.v:3:6: error: cannot assign to s[i] (strings are immutable)
|
||||||
|
1 | fn main() {
|
||||||
|
2 | mut a := 'V is great!!'
|
||||||
|
3 | a[0] = `R`
|
||||||
|
| ~~~
|
||||||
|
4 | }
|
4
vlib/v/checker/tests/string_index_assign_error.vv
Normal file
4
vlib/v/checker/tests/string_index_assign_error.vv
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
fn main() {
|
||||||
|
mut a := 'V is great!!'
|
||||||
|
a[0] = `R`
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user