Skip to content

Commit a65d5ae

Browse files
authored
checker: disallow arr = voidptr(0) (fix #23675) (#23687)
1 parent 735046a commit a65d5ae

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

vlib/v/checker/assign.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,14 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
801801
if !is_blank_ident && right_sym.kind != .placeholder && left_sym.kind != .interface
802802
&& ((!right_type.has_flag(.generic) && !left_type.has_flag(.generic))
803803
|| right_sym.kind != left_sym.kind) {
804+
// Disallow `array = voidptr` assign
805+
if left_sym.kind in [.array, .array_fixed]
806+
&& (right_type_unwrapped.is_voidptr() || right.is_nil()) {
807+
left_str := c.table.type_to_str(left_type_unwrapped)
808+
right_str := c.table.type_to_str(right_type_unwrapped)
809+
c.error('cannot assign to `${left}`: expected `${left_str}`, not `${right_str}`',
810+
right.pos())
811+
}
804812
// Dual sides check (compatibility check)
805813
c.check_expected(right_type_unwrapped, left_type_unwrapped) or {
806814
// allow literal values to auto deref var (e.g.`for mut v in values { v = 1.0 }`)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
vlib/v/checker/tests/array_voidptr_assign_err.vv:2:6: warning: unused variable: `a`
2+
1 | fn main() {
3+
2 | mut a := []int{}
4+
| ^
5+
3 | a = unsafe { nil }
6+
4 |
7+
vlib/v/checker/tests/array_voidptr_assign_err.vv:3:6: error: cannot assign to `a`: expected `[]int`, not `voidptr`
8+
1 | fn main() {
9+
2 | mut a := []int{}
10+
3 | a = unsafe { nil }
11+
| ~~~~~~
12+
4 |
13+
5 | // vfmt off
14+
vlib/v/checker/tests/array_voidptr_assign_err.vv:6:6: error: cannot assign to `a`: expected `[]int`, not `voidptr`
15+
4 |
16+
5 | // vfmt off
17+
6 | a = voidptr(0)
18+
| ~~~~~~~~~~
19+
7 | }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
mut a := []int{}
3+
a = unsafe { nil }
4+
5+
// vfmt off
6+
a = voidptr(0)
7+
}

0 commit comments

Comments
 (0)