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:
parent
64558df764
commit
89aa695fba
@ -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_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 c.type_implements(expr_type, field_info.typ, field.pos) {
|
||||
if !c.inside_unsafe && expr_type_sym.kind != .interface_
|
||||
|
@ -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 | }
|
@ -0,0 +1,13 @@
|
||||
struct Foo {
|
||||
num int
|
||||
}
|
||||
|
||||
struct Foo2 {
|
||||
a voidptr
|
||||
}
|
||||
|
||||
fn main() {
|
||||
_ := Foo2{
|
||||
a: Foo{}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user