checker: check struct field reference type mismatch (#13575)
parent
838a8f2183
commit
c3ec738126
|
@ -319,7 +319,7 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if field_info.typ.is_ptr() && !expr_type.is_ptr() && !expr_type.is_pointer()
|
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',
|
c.error('reference field must be initialized with reference',
|
||||||
field.pos)
|
field.pos)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue