Skip to content

Commit 89aa695

Browse files
authored
checker: disallow non ptr struct values to voidptr fields (#16958)
1 parent 64558df commit 89aa695

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

vlib/v/checker/struct.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
467467
expr_type = c.check_expr_opt_call(field.expr, expr_type)
468468
}
469469
expr_type_sym := c.table.sym(expr_type)
470+
if field_type_sym.kind == .voidptr && expr_type_sym.kind == .struct_
471+
&& !expr_type.is_ptr() {
472+
c.error('allocate on the heap for use in other functions', field.pos)
473+
}
470474
if field_type_sym.kind == .interface_ {
471475
if c.type_implements(expr_type, field_info.typ, field.pos) {
472476
if !c.inside_unsafe && expr_type_sym.kind != .interface_
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/struct_voidptr_field_no_ptr_struct_value_err.vv:11:3: error: allocate on the heap for use in other functions
2+
9 | fn main() {
3+
10 | _ := Foo2{
4+
11 | a: Foo{}
5+
| ~~~~~~~~
6+
12 | }
7+
13 | }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
struct Foo {
2+
num int
3+
}
4+
5+
struct Foo2 {
6+
a voidptr
7+
}
8+
9+
fn main() {
10+
_ := Foo2{
11+
a: Foo{}
12+
}
13+
}

0 commit comments

Comments
 (0)