checker: check struct field reference type mismatch (#13575)

pull/13585/head
yuyi 2022-02-23 16:53:22 +08:00 committed by GitHub
parent 838a8f2183
commit c3ec738126
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -319,7 +319,7 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
}
} else {
if field_info.typ.is_ptr() && !expr_type.is_ptr() && !expr_type.is_pointer()
&& !expr_type.is_number() {
&& field.expr.str() != '0' {
c.error('reference field must be initialized with reference',
field.pos)
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/struct_field_reference_type_err.vv:17:3: error: reference field must be initialized with reference
15 |
16 | animal.duck = Duck{
17 | age: animal.ageee
| ~~~~~~~~~~~~~~~~~
18 | }
19 | dump(animal.duck.age)

View File

@ -0,0 +1,20 @@
struct Animal {
mut:
ageee int
duck Duck
}
struct Duck {
age &int
}
fn main() {
mut animal := Animal{
ageee: 20
}
animal.duck = Duck{
age: animal.ageee
}
dump(animal.duck.age)
}