checker: fix incorrect checks when struct fields are ref and option type(fix #19555) (#20195)

This commit is contained in:
shove 2023-12-17 02:45:54 +08:00 committed by GitHub
parent 136193a3aa
commit 51aaf3c49f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 3 deletions

View File

@ -9,7 +9,7 @@ struct PostTag {
}
fn test_main() {
new_post_tag := PostTag{}
new_post_tag := &PostTag{}
assert json.encode(new_post_tag) == '{"id":"","visibility":"","createdAt":"","metadata":""}'
new_post_tag2 := PostTag{

View File

@ -704,8 +704,9 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
c.error('reference field must be initialized with reference',
init_field.pos)
}
} else if exp_type.is_pointer() && !got_type.is_any_kind_of_pointer()
&& !got_type.is_int() {
} else if exp_type.is_any_kind_of_pointer()
&& !got_type.is_any_kind_of_pointer() && !got_type.is_int()
&& (!exp_type.has_flag(.option) || got_type.idx() != ast.none_type_idx) {
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}`',

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_field_init_option_ref_err.vv:12:3: error: cannot assign to field `field`: expected a pointer `?&Foo`, but got `?Foo`
10 | foo := ?Foo{}
11 | _ := Bar{
12 | field: foo
| ~~~~~~~~~~
13 | }
14 | }

View File

@ -0,0 +1,14 @@
module main
struct Foo {}
struct Bar {
field ?&Foo
}
fn main() {
foo := ?Foo{}
_ := Bar{
field: foo
}
}