Skip to content

Commit

Permalink
checker: fix incorrect checks when struct fields are ref and option t…
Browse files Browse the repository at this point in the history
…ype(fix #19555) (#20195)
  • Loading branch information
shove70 committed Dec 16, 2023
1 parent 136193a commit 51aaf3c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion vlib/json/json_encode_recursive_ptr_test.v
Expand Up @@ -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{
Expand Down
5 changes: 3 additions & 2 deletions vlib/v/checker/struct.v
Expand Up @@ -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}`',
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/struct_field_init_option_ref_err.out
@@ -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 | }
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/struct_field_init_option_ref_err.vv
@@ -0,0 +1,14 @@
module main

struct Foo {}

struct Bar {
field ?&Foo
}

fn main() {
foo := ?Foo{}
_ := Bar{
field: foo
}
}

0 comments on commit 51aaf3c

Please sign in to comment.