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

checker: check struct init with pointer field (fix #18485) (#18501)

This commit is contained in:
yuyi 2023-06-21 17:36:28 +08:00 committed by GitHub
parent 5006ffb304
commit 76ae9dba5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -544,6 +544,12 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
&& field.expr.str() != '0' && !exp_type.has_flag(.option) {
c.error('reference field must be initialized with reference',
field.pos)
} else if exp_type.is_pointer() && !got_type.is_any_kind_of_pointer()
&& !got_type.is_int() {
got_typ_str := c.table.type_to_str(got_type)
exp_typ_str := c.table.type_to_str(exp_type)
c.error('cannot assign to field `${field_info.name}`: expected a pointer `${exp_typ_str}`, but got `${got_typ_str}`',
field.pos)
}
}
node.fields[i].typ = got_type

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_voidptr_field_init_err.vv:7:3: error: cannot assign to field `example`: expected a pointer `voidptr`, but got `string`
5 | fn main() {
6 | println(Example{
7 | example: get()
| ~~~~~~~~~~~~~~
8 | })
9 | }

View File

@ -0,0 +1,13 @@
struct Example {
example voidptr
}
fn main() {
println(Example{
example: get()
})
}
fn get() string {
return 'hello'
}