1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: disallow non ptr struct values to voidptr fields (#16958)

This commit is contained in:
Swastik Baranwal 2023-01-13 20:35:18 +05:30 committed by GitHub
parent 64558df764
commit 89aa695fba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View File

@ -467,6 +467,10 @@ fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
expr_type = c.check_expr_opt_call(field.expr, expr_type) expr_type = c.check_expr_opt_call(field.expr, expr_type)
} }
expr_type_sym := c.table.sym(expr_type) expr_type_sym := c.table.sym(expr_type)
if field_type_sym.kind == .voidptr && expr_type_sym.kind == .struct_
&& !expr_type.is_ptr() {
c.error('allocate on the heap for use in other functions', field.pos)
}
if field_type_sym.kind == .interface_ { if field_type_sym.kind == .interface_ {
if c.type_implements(expr_type, field_info.typ, field.pos) { if c.type_implements(expr_type, field_info.typ, field.pos) {
if !c.inside_unsafe && expr_type_sym.kind != .interface_ if !c.inside_unsafe && expr_type_sym.kind != .interface_

View File

@ -0,0 +1,7 @@
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
9 | fn main() {
10 | _ := Foo2{
11 | a: Foo{}
| ~~~~~~~~
12 | }
13 | }

View File

@ -0,0 +1,13 @@
struct Foo {
num int
}
struct Foo2 {
a voidptr
}
fn main() {
_ := Foo2{
a: Foo{}
}
}