@@ -534,7 +534,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
534
534
&& c.table.cur_concrete_types.len == 0 {
535
535
pos := type_sym.name.last_index_u8 (`.` )
536
536
first_letter := type_sym.name[pos + 1 ]
537
- if ! first_letter.is_capital ()
537
+ if ! first_letter.is_capital () && type_sym.kind != . none
538
538
&& (type_sym.kind != .struct || ! (type_sym.info is ast.Struct && type_sym.info.is_anon))
539
539
&& type_sym.kind != .placeholder {
540
540
c.error ('cannot initialize builtin type `${type_sym.name} `' , node.pos)
@@ -850,6 +850,35 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
850
850
inited_fields)
851
851
}
852
852
}
853
+ .none {
854
+ // var := struct { name: "" }
855
+ mut init_fields := []ast.StructField{}
856
+ for init_field in node.init_fields {
857
+ mut expr := unsafe { init_field }
858
+ init_fields << ast.StructField{
859
+ name: init_field.name
860
+ typ: c.expr (mut expr.expr)
861
+ is_mut: c.anon_struct_should_be_mut
862
+ }
863
+ }
864
+ c.table.anon_struct_counter++
865
+ name := '_VAnonStruct${c.table.anon_struct_counter} '
866
+ sym_struct := ast.TypeSymbol{
867
+ kind: .struct
868
+ language: .v
869
+ name: name
870
+ cname: util.no_dots (name)
871
+ mod: c.mod
872
+ info: ast.Struct{
873
+ is_anon: true
874
+ fields: init_fields
875
+ }
876
+ is_pub: true
877
+ }
878
+ ret := c.table.register_sym (sym_struct)
879
+ c.table.register_anon_struct (name, ret)
880
+ node.typ = c.table.find_type_idx (name)
881
+ }
853
882
else {}
854
883
}
855
884
if node.has_update_expr {
@@ -1140,3 +1169,17 @@ fn (mut c Checker) check_ref_fields_initialized_note(struct_sym &ast.TypeSymbol,
1140
1169
}
1141
1170
}
1142
1171
}
1172
+
1173
+ fn (mut c Checker) is_anon_struct_compatible (s1 ast.Struct, s2 ast.Struct) bool {
1174
+ if ! (s1 .is_anon && s2 .is_anon && s1 .fields.len == s2 .fields.len) {
1175
+ return false
1176
+ }
1177
+ mut is_compatible := true
1178
+ for k, field in s1 .fields {
1179
+ if ! c.check_basic (field.typ, s2 .fields[k].typ) {
1180
+ is_compatible = false
1181
+ break
1182
+ }
1183
+ }
1184
+ return is_compatible
1185
+ }
0 commit comments