1
0
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:
Swastik Baranwal 2023-04-27 20:24:26 +05:30 committed by GitHub
parent 9fb52c4c9c
commit 30ac2e8763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View File

@ -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,

View File

@ -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 | }

View File

@ -0,0 +1,16 @@
struct Message {
a string
b string
}
struct Fax {
message Message
}
f := Fax{
message: struct {
a: 'A'
}
}
dump(f)