checker: disallow struct init with mutable_field: const_array (fix #22862) (#22863)

This commit is contained in:
Swastik Baranwal 2024-11-15 15:27:27 +05:30 committed by GitHub
parent df51e840f9
commit 3954e05455
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 6 deletions

View File

@ -51,7 +51,7 @@ const test_cases = [
}
results: Chunk{
chunk_number: 0
chaining_value: iv
chaining_value: iv.clone()
block_words: [u32(0), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
flags: 0x0b
}
@ -69,7 +69,7 @@ const test_cases = [
}
results: Chunk{
chunk_number: 0
chaining_value: iv
chaining_value: iv.clone()
block_words: [u32(0x00000041), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
flags: 0x0b
}
@ -87,7 +87,7 @@ const test_cases = [
}
results: Chunk{
chunk_number: 0
chaining_value: iv
chaining_value: iv.clone()
block_words: [u32(0x00636261), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
flags: 0x0b
}
@ -105,7 +105,7 @@ const test_cases = [
}
results: Chunk{
chunk_number: 0
chaining_value: iv
chaining_value: iv.clone()
block_words: [u32(0x44434241), 0x48474645, 0x4c4b4a49, 0x504f4e4d, 0x54535251,
0x58575655, 0x61205a59, 0x65646362, 0x69686766, 0x6d6c6b6a, 0x71706f6e, 0x75747372,
0x79787776, 0x3130207a, 0x35343332, 0x00383736]
@ -125,7 +125,7 @@ const test_cases = [
}
results: Chunk{
chunk_number: 0
chaining_value: iv
chaining_value: iv.clone()
block_words: [u32(0x44434241), 0x48474645, 0x4c4b4a49, 0x504f4e4d, 0x54535251,
0x58575655, 0x61205a59, 0x65646362, 0x69686766, 0x6d6c6b6a, 0x71706f6e, 0x75747372,
0x79787776, 0x3130207a, 0x35343332, 0x39383736]
@ -269,7 +269,7 @@ const test_cases = [
}
results: Chunk{
chunk_number: 1
chaining_value: iv
chaining_value: iv.clone()
block_words: [u32(0x0000004f), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
flags: 0x0b
}

View File

@ -745,6 +745,13 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
got_type = c.expr(mut right)
node.init_fields[i].expr = right
}
// disallow `mut a: b`, when b is const array
if field_info.is_mut
&& (init_field.expr is ast.Ident && init_field.expr.obj is ast.ConstField)
&& !c.inside_unsafe {
c.error('cannot assign a const array to mut struct field, call `clone` method (or use `unsafe`)',
init_field.expr.pos())
}
}
if exp_type_sym.kind == .interface {
if c.type_implements(got_type, exp_type, init_field.pos) {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_arr_mut_field_arr_assign_err.vv:10:6: error: cannot assign a const array to mut struct field, call `clone` method (or use `unsafe`)
8 | fn main() {
9 | mut muta := Muta{
10 | a: arr
| ~~~
11 | }
12 | muta.a[0] = 52

View File

@ -0,0 +1,14 @@
const arr = [1, 2, 3]
struct Muta {
mut:
a []int
}
fn main() {
mut muta := Muta{
a: arr
}
muta.a[0] = 52
println(arr) // [52, 2, 3]
}