mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: disallow assigning anon struct
to typed struct
(#18017)
This commit is contained in:
parent
9fb52c4c9c
commit
30ac2e8763
@ -574,6 +574,14 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if field_type_sym.kind == .struct_ && !(field_type_sym.info as ast.Struct).is_anon
|
||||
&& mut field.expr is ast.StructInit {
|
||||
if field.expr.is_anon {
|
||||
c.error('cannot assign anonymous `struct` to a typed `struct`',
|
||||
field.expr.pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Check uninitialized refs/sum types
|
||||
// The variable `fields` contains two parts, the first part is the same as info.fields,
|
||||
|
@ -0,0 +1,7 @@
|
||||
vlib/v/checker/tests/assign_anon_struct_to_typed_struct_err.vv:11:18: error: cannot assign anonymous `struct` to a typed `struct`
|
||||
9 |
|
||||
10 | f := Fax{
|
||||
11 | message: struct {
|
||||
| ^
|
||||
12 | a: 'A'
|
||||
13 | }
|
@ -0,0 +1,16 @@
|
||||
struct Message {
|
||||
a string
|
||||
b string
|
||||
}
|
||||
|
||||
struct Fax {
|
||||
message Message
|
||||
}
|
||||
|
||||
f := Fax{
|
||||
message: struct {
|
||||
a: 'A'
|
||||
}
|
||||
}
|
||||
|
||||
dump(f)
|
Loading…
Reference in New Issue
Block a user