checker: disallow arr = voidptr(0) (fix #23675) (#23687)

This commit is contained in:
Swastik Baranwal 2025-02-11 13:39:59 +05:30 committed by GitHub
parent 735046a3d4
commit a65d5ae10f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 0 deletions

View File

@ -801,6 +801,14 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
if !is_blank_ident && right_sym.kind != .placeholder && left_sym.kind != .interface if !is_blank_ident && right_sym.kind != .placeholder && left_sym.kind != .interface
&& ((!right_type.has_flag(.generic) && !left_type.has_flag(.generic)) && ((!right_type.has_flag(.generic) && !left_type.has_flag(.generic))
|| right_sym.kind != left_sym.kind) { || right_sym.kind != left_sym.kind) {
// Disallow `array = voidptr` assign
if left_sym.kind in [.array, .array_fixed]
&& (right_type_unwrapped.is_voidptr() || right.is_nil()) {
left_str := c.table.type_to_str(left_type_unwrapped)
right_str := c.table.type_to_str(right_type_unwrapped)
c.error('cannot assign to `${left}`: expected `${left_str}`, not `${right_str}`',
right.pos())
}
// Dual sides check (compatibility check) // Dual sides check (compatibility check)
c.check_expected(right_type_unwrapped, left_type_unwrapped) or { c.check_expected(right_type_unwrapped, left_type_unwrapped) or {
// allow literal values to auto deref var (e.g.`for mut v in values { v = 1.0 }`) // allow literal values to auto deref var (e.g.`for mut v in values { v = 1.0 }`)

View File

@ -0,0 +1,19 @@
vlib/v/checker/tests/array_voidptr_assign_err.vv:2:6: warning: unused variable: `a`
1 | fn main() {
2 | mut a := []int{}
| ^
3 | a = unsafe { nil }
4 |
vlib/v/checker/tests/array_voidptr_assign_err.vv:3:6: error: cannot assign to `a`: expected `[]int`, not `voidptr`
1 | fn main() {
2 | mut a := []int{}
3 | a = unsafe { nil }
| ~~~~~~
4 |
5 | // vfmt off
vlib/v/checker/tests/array_voidptr_assign_err.vv:6:6: error: cannot assign to `a`: expected `[]int`, not `voidptr`
4 |
5 | // vfmt off
6 | a = voidptr(0)
| ~~~~~~~~~~
7 | }

View File

@ -0,0 +1,7 @@
fn main() {
mut a := []int{}
a = unsafe { nil }
// vfmt off
a = voidptr(0)
}