| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license that can be found in the LICENSE file. |
| 3 | module checker |
| 4 | |
| 5 | import v.ast |
| 6 | import v.util |
| 7 | import v.token |
| 8 | |
| 9 | fn (c &Checker) can_be_embedded_in_struct(typ ast.Type) bool { |
| 10 | return c.table.final_sym(typ).kind in [.struct, .function] |
| 11 | } |
| 12 | |
| 13 | fn (c &Checker) is_opaque_c_typedef_struct_alias(typ ast.Type) bool { |
| 14 | sym := c.table.sym(typ) |
| 15 | if sym.kind != .alias { |
| 16 | return false |
| 17 | } |
| 18 | final_sym := c.table.final_sym(typ) |
| 19 | if final_sym.language != .c || final_sym.kind != .struct { |
| 20 | return false |
| 21 | } |
| 22 | if final_sym.info is ast.Struct { |
| 23 | return final_sym.info.is_typedef && final_sym.info.is_empty_struct() |
| 24 | } |
| 25 | return false |
| 26 | } |
| 27 | |
| 28 | fn (mut c Checker) struct_decl(mut node ast.StructDecl) { |
| 29 | util.timing_start(@METHOD) |
| 30 | defer { |
| 31 | util.timing_measure_cumulative(@METHOD) |
| 32 | } |
| 33 | if node.language in [.c, .js] && node.generic_types.len > 0 { |
| 34 | lang := if node.language == .c { 'C' } else { 'JS' } |
| 35 | c.error('${lang} structs cannot be declared as generic', node.pos) |
| 36 | } |
| 37 | node_name := if node.scoped_name != '' { node.scoped_name } else { node.name } |
| 38 | mut struct_sym, struct_typ_idx := c.table.find_sym_and_type_idx(node_name) |
| 39 | node.idx = struct_typ_idx |
| 40 | mut has_generic_types := false |
| 41 | if mut struct_sym.info is ast.Struct { |
| 42 | for mut symfield in struct_sym.info.fields { |
| 43 | symfield.container_typ = struct_typ_idx |
| 44 | if struct_sym.info.is_union { |
| 45 | symfield.is_part_of_union = true |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if node.language == .v && !c.is_builtin_mod && !struct_sym.info.is_anon { |
| 50 | c.check_valid_pascal_case(node.name, 'struct name', node.pos) |
| 51 | } |
| 52 | if node.language == .v { |
| 53 | for embed in node.embeds { |
| 54 | embed_typ := c.table.unaliased_type(embed.typ) |
| 55 | if embed_typ.is_ptr() || embed_typ.has_flag(.option) { |
| 56 | continue |
| 57 | } |
| 58 | embed_sym := c.table.sym(embed_typ) |
| 59 | if embed_sym.name == struct_sym.name |
| 60 | || c.table.has_deep_child_no_ref(embed_sym, struct_sym.name) { |
| 61 | c.error('invalid recursive struct `${node.name}`', node.pos) |
| 62 | break |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | for embed in node.embeds { |
| 67 | // gotodef for embedded struct types |
| 68 | if c.pref.is_vls && c.pref.linfo.method == .definition { |
| 69 | if c.vls_is_the_node(embed.pos) { |
| 70 | embed_sym := c.table.sym(embed.typ) |
| 71 | pos := embed_sym.info.get_name_pos() or { token.Pos{} } |
| 72 | if pos.file_idx != -1 { |
| 73 | println('${c.table.filelist[pos.file_idx]}:${pos.line_nr + 1}:${pos.col}') |
| 74 | exit(0) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | embed_sym := c.table.sym(embed.typ) |
| 79 | embed_final_sym := c.table.final_sym(embed.typ) |
| 80 | if embed_sym.info is ast.Alias { |
| 81 | parent_sym := c.table.final_sym(embed_sym.info.parent_type) |
| 82 | if parent_sym.kind !in [.struct, .function] { |
| 83 | c.error('`${embed_sym.name}` (alias of `${parent_sym.name}`) is not a struct', |
| 84 | embed.pos) |
| 85 | } |
| 86 | } else if embed_sym.kind !in [.struct, .function] { |
| 87 | c.error('`${embed_sym.name}` is not a struct', embed.pos) |
| 88 | } else if embed_final_sym.kind == .struct |
| 89 | && (embed_final_sym.info as ast.Struct).is_heap && !embed.typ.is_ptr() { |
| 90 | struct_sym.info.is_heap = true |
| 91 | } |
| 92 | embed_is_generic := embed.typ.has_flag(.generic) |
| 93 | if embed_is_generic { |
| 94 | has_generic_types = true |
| 95 | } |
| 96 | // Ensure each generic type of the embed was declared in the struct's definition |
| 97 | if embed_is_generic && node.generic_types.len > 0 { |
| 98 | embed_generic_names := c.table.generic_type_names(embed.typ) |
| 99 | node_generic_names := node.generic_types.map(c.table.type_to_str(it)) |
| 100 | for name in embed_generic_names { |
| 101 | if name !in node_generic_names { |
| 102 | struct_generic_names := node_generic_names.join(', ') |
| 103 | c.error('generic type name `${name}` is not mentioned in struct `${node.name}[${struct_generic_names}]`', |
| 104 | embed.pos) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | if struct_sym.info.is_minify && !c.pref.output_cross_c { |
| 110 | node.fields.sort_with_compare(minify_sort_fn) |
| 111 | struct_sym.info.fields.sort_with_compare(minify_sort_fn) |
| 112 | } |
| 113 | for attr in node.attrs { |
| 114 | if node.language != .c && attr.name == 'typedef' { |
| 115 | c.error('`typedef` attribute can only be used with C structs', node.pos) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Evaluate the size of the unresolved fixed array |
| 120 | for mut field in node.fields { |
| 121 | sym := c.table.sym(field.typ) |
| 122 | if sym.info is ast.ArrayFixed && c.array_fixed_has_unresolved_size(sym.info) { |
| 123 | mut size_expr := unsafe { sym.info.size_expr } |
| 124 | old_typ := field.typ |
| 125 | field.typ = c.eval_array_fixed_sizes(mut size_expr, 0, sym.info.elem_type) |
| 126 | for mut symfield in struct_sym.info.fields { |
| 127 | if symfield.name == field.name { |
| 128 | symfield.typ = field.typ |
| 129 | } |
| 130 | } |
| 131 | // Overwrite the previously unresolved type symbol so that earlier |
| 132 | // expressions which captured its idx (e.g. IndexExpr.left_type from |
| 133 | // another file checked first) observe the resolved size. See #27078. |
| 134 | // Skip for generic structs: the size expression may reference a generic |
| 135 | // type parameter (e.g. `sizeof(T)`), which resolves to a placeholder size |
| 136 | // here; the correct per-instantiation size is computed later in struct_init. |
| 137 | if old_typ.idx() != field.typ.idx() && struct_sym.info.generic_types.len == 0 { |
| 138 | new_sym := c.table.sym(field.typ) |
| 139 | mut old_sym := c.table.type_symbols[old_typ.idx()] |
| 140 | old_sym.name = new_sym.name |
| 141 | old_sym.cname = new_sym.cname |
| 142 | old_sym.info = new_sym.info |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Update .default_expr_typ for all fields in the struct: |
| 148 | util.timing_start('Checker.struct setting default_expr_typ') |
| 149 | old_expected_type := c.expected_type |
| 150 | for mut field in node.fields { |
| 151 | // when the field has the same type that the struct itself (recursive) |
| 152 | if field.typ.clear_flag(.option).set_nr_muls(0) == struct_typ_idx { |
| 153 | for mut symfield in struct_sym.info.fields { |
| 154 | if symfield.name == field.name { |
| 155 | // only ?&Struct is allowed to be recursive |
| 156 | if field.typ.is_ptr() { |
| 157 | symfield.is_recursive = true |
| 158 | } else { |
| 159 | c.error('recursive struct is only possible with optional pointer (e.g. ?&${c.table.type_to_str(field.typ.clear_flag(.option))})', |
| 160 | field.pos) |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | // Do not allow uninitialized `fn` fields, or force `?fn` |
| 166 | // (allow them in `C.` structs) |
| 167 | if !c.is_builtin_mod && node.language == .v { |
| 168 | if !(c.file.is_translated || c.pref.translated) { |
| 169 | sym := c.table.sym(field.typ) |
| 170 | if sym.kind == .function && !field.typ.has_flag(.option) |
| 171 | && !field.has_default_expr && !field.attrs.contains('required') { |
| 172 | error_msg := 'uninitialized `fn` struct fields are not allowed, since they can result in segfaults; use `?fn` or `@[required]` or initialize the field with `=` (if you absolutely want to have unsafe function pointers, use `= unsafe { nil }`)' |
| 173 | c.note(error_msg, field.pos) |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if field.has_default_expr { |
| 179 | c.expected_type = field.typ |
| 180 | field.default_expr_typ = c.expr(mut field.default_expr) |
| 181 | if field.typ.is_ptr() != field.default_expr_typ.is_ptr() |
| 182 | && field.default_expr_typ.idx() !in ast.pointer_type_idxs { |
| 183 | default_pos := field.default_expr.pos() |
| 184 | if field.default_expr is ast.CallExpr { |
| 185 | err_desc := if field.typ.is_ptr() { 'is' } else { 'is not' } |
| 186 | val_desc := if field.default_expr_typ.is_ptr() { 'is' } else { 'is not' } |
| 187 | c.error('field ${err_desc} reference but default value ${val_desc} reference', |
| 188 | default_pos) |
| 189 | } else if field.default_expr is ast.StructInit |
| 190 | || (field.typ.is_ptr() && (field.default_expr is ast.Ident |
| 191 | || field.default_expr is ast.SelectorExpr)) { |
| 192 | c.error('reference field must be initialized with reference', default_pos) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if c.table.final_sym(field.typ).kind == .voidptr |
| 197 | && field.default_expr_typ !in [ast.nil_type, ast.voidptr_type, ast.byteptr_type] |
| 198 | && !field.default_expr_typ.is_ptr() |
| 199 | && (field.default_expr !is ast.IntegerLiteral |
| 200 | || (field.default_expr is ast.IntegerLiteral |
| 201 | && field.default_expr.val.int() != 0)) { |
| 202 | c.note('voidptr variables may only be assigned voidptr values (e.g. unsafe { voidptr(${field.default_expr.str()}) })', |
| 203 | field.default_expr.pos()) |
| 204 | } |
| 205 | |
| 206 | // disallow map `mut a = b` |
| 207 | field_sym := c.table.sym(field.typ) |
| 208 | expr_sym := c.table.sym(field.default_expr_typ) |
| 209 | if field_sym.kind == .map && expr_sym.kind == .map && field.default_expr.is_lvalue() |
| 210 | && field.is_mut |
| 211 | && (!field.default_expr_typ.is_ptr() || field.default_expr is ast.Ident) { |
| 212 | c.error('cannot copy map: call `clone` method (or use a reference)', |
| 213 | field.default_expr.pos()) |
| 214 | } |
| 215 | if c.is_nocopy_struct(field.typ) && c.is_nocopy_struct(field.default_expr_typ) |
| 216 | && field.default_expr !is ast.StructInit { |
| 217 | c.error('cannot copy @[nocopy] struct: use a reference instead', |
| 218 | field.default_expr.pos()) |
| 219 | } |
| 220 | for mut symfield in struct_sym.info.fields { |
| 221 | if symfield.name == field.name { |
| 222 | symfield.default_expr_typ = field.default_expr_typ |
| 223 | break |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | // check anon struct declaration |
| 228 | if field.anon_struct_decl.fields.len > 0 { |
| 229 | c.struct_decl(mut field.anon_struct_decl) |
| 230 | } |
| 231 | } |
| 232 | c.expected_type = old_expected_type |
| 233 | util.timing_measure_cumulative('Checker.struct setting default_expr_typ') |
| 234 | |
| 235 | for i, field in node.fields { |
| 236 | if field.typ.has_flag(.result) { |
| 237 | c.error('struct field does not support storing Result', field.option_pos) |
| 238 | } |
| 239 | if !c.ensure_type_exists(field.typ, field.type_pos) { |
| 240 | continue |
| 241 | } |
| 242 | if node.language == .v && !field.typ.is_ptr() |
| 243 | && c.is_opaque_c_typedef_struct_alias(field.typ) { |
| 244 | field_typ := c.table.type_to_str(field.typ) |
| 245 | ref_typ := c.table.type_to_str(field.typ.clear_option_and_result().set_nr_muls(1)) |
| 246 | c.error('cannot use opaque C struct `${field_typ}` as a non-reference struct field; use `${ref_typ}` instead', |
| 247 | field.type_pos) |
| 248 | continue |
| 249 | } |
| 250 | // gotodef for struct field types |
| 251 | if c.pref.is_vls && c.pref.linfo.method == .definition { |
| 252 | if c.vls_is_the_node(field.type_pos) { |
| 253 | sym := c.table.sym(field.typ) |
| 254 | elem_type := match sym.kind { |
| 255 | .array { |
| 256 | (sym.info as ast.Array).elem_type |
| 257 | } |
| 258 | .array_fixed { |
| 259 | (sym.info as ast.ArrayFixed).elem_type |
| 260 | } |
| 261 | else { |
| 262 | ast.Type(0) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if elem_type == 0 { |
| 267 | pos := sym.info.get_name_pos() or { token.Pos{} } |
| 268 | if pos.file_idx != -1 { |
| 269 | println('${c.table.filelist[pos.file_idx]}:${pos.line_nr + 1}:${pos.col}') |
| 270 | exit(0) |
| 271 | } |
| 272 | } else { |
| 273 | elem_sym := c.table.sym(elem_type) |
| 274 | if np := elem_sym.info.get_name_pos() { |
| 275 | if np.file_idx != -1 { |
| 276 | println('${c.table.filelist[np.file_idx]}:${np.line_nr + 1}:${np.col}') |
| 277 | exit(0) |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | field_is_generic := field.typ.has_flag(.generic) |
| 284 | if c.table.type_kind(field.typ) != .alias |
| 285 | && !c.ensure_generic_type_specify_type_names(field.typ, field.type_pos, c.table.final_sym(field.typ).kind in [.array, .array_fixed, .map], field_is_generic) { |
| 286 | continue |
| 287 | } |
| 288 | if field_is_generic { |
| 289 | has_generic_types = true |
| 290 | } |
| 291 | if node.language == .v { |
| 292 | c.check_valid_snake_case(field.name, 'field name', field.pos) |
| 293 | } |
| 294 | sym := c.table.sym(field.typ) |
| 295 | field_name, field_name_len := field.name, field.name.len |
| 296 | for j in 0 .. i { |
| 297 | if field_name_len == node.fields[j].name.len && field_name == node.fields[j].name { |
| 298 | c.error('field name `${field.name}` duplicate', field.pos) |
| 299 | } |
| 300 | } |
| 301 | if field.typ != 0 { |
| 302 | if !field.typ.is_ptr() { |
| 303 | if c.table.unaliased_type(field.typ) == struct_typ_idx { |
| 304 | c.error('field `${field.name}` is part of `${node.name}`, they can not both have the same type', |
| 305 | field.type_pos) |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | match sym.kind { |
| 310 | .struct { |
| 311 | info := sym.info as ast.Struct |
| 312 | if info.is_heap && !field.typ.is_ptr() { |
| 313 | struct_sym.info.is_heap = true |
| 314 | } |
| 315 | for ct in info.concrete_types { |
| 316 | ct_sym := c.table.sym(ct) |
| 317 | if ct_sym.kind == .placeholder { |
| 318 | c.error('unknown type `${ct_sym.name}`', field.type_pos) |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | .multi_return { |
| 323 | c.error('cannot use multi return as field type', field.type_pos) |
| 324 | } |
| 325 | .none { |
| 326 | c.error('cannot use `none` as field type', field.type_pos) |
| 327 | } |
| 328 | .map { |
| 329 | info := sym.map_info() |
| 330 | if info.value_type.has_flag(.result) { |
| 331 | c.error('cannot use Result type as map value type', field.type_pos) |
| 332 | } |
| 333 | } |
| 334 | .alias { |
| 335 | if sym.name == 'byte' { |
| 336 | c.error('byte is deprecated, use u8 instead', field.type_pos) |
| 337 | } |
| 338 | } |
| 339 | else { |
| 340 | c.check_any_type(field.typ, sym, field.type_pos) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if field.has_default_expr { |
| 345 | c.expected_type = field.typ |
| 346 | if !field.typ.has_option_or_result() { |
| 347 | c.check_expr_option_or_result_call(field.default_expr, field.default_expr_typ) |
| 348 | } |
| 349 | if sym.info is ast.ArrayFixed && field.typ == field.default_expr_typ { |
| 350 | if sym.info.size_expr is ast.ComptimeCall { |
| 351 | // field [$d('x' ,2)]int = [1 ,2]! |
| 352 | if sym.info.size_expr.kind == .d { |
| 353 | c.error('cannot initialize a fixed size array field that uses `\$d()` as size quantifier since the size may change via -d', |
| 354 | field.default_expr.pos()) |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | if field.default_expr is ast.ArrayInit { |
| 359 | if c.table.final_sym(field.default_expr_typ).kind in [.array, .array_fixed] |
| 360 | && c.table.value_type(field.default_expr_typ) == struct_typ_idx { |
| 361 | c.error('cannot initialize array of same struct type that is being defined (recursion detected)', |
| 362 | field.pos) |
| 363 | } |
| 364 | } |
| 365 | interface_implemented := sym.kind == .interface |
| 366 | && c.type_implements(field.default_expr_typ, field.typ, field.pos) |
| 367 | c.check_expected(field.default_expr_typ, field.typ) or { |
| 368 | if sym.kind == .interface && interface_implemented { |
| 369 | if !c.inside_unsafe && !field.default_expr_typ.is_any_kind_of_pointer() { |
| 370 | if c.table.sym(field.default_expr_typ).kind != .interface { |
| 371 | c.mark_as_referenced(mut &node.fields[i].default_expr, true) |
| 372 | } |
| 373 | } |
| 374 | } else if c.table.final_sym(field.typ).kind == .function |
| 375 | && field.default_expr_typ.is_pointer() { |
| 376 | continue |
| 377 | } else { |
| 378 | c.error('incompatible initializer for field `${field.name}`: ${err.msg()}', |
| 379 | field.default_expr.pos()) |
| 380 | } |
| 381 | } |
| 382 | if field.default_expr.is_nil() { |
| 383 | mut nil_field_typ := field.typ |
| 384 | mut nil_field_sym := c.table.sym(nil_field_typ) |
| 385 | // Preserve pointer indirections while resolving alias chains. |
| 386 | for { |
| 387 | if mut nil_field_sym.info is ast.Alias { |
| 388 | parent_typ := nil_field_sym.info.parent_type |
| 389 | nil_field_typ = parent_typ.set_nr_muls(parent_typ.nr_muls() + |
| 390 | nil_field_typ.nr_muls()) |
| 391 | nil_field_sym = c.table.sym(nil_field_typ) |
| 392 | } else { |
| 393 | break |
| 394 | } |
| 395 | } |
| 396 | if !nil_field_typ.is_any_kind_of_pointer() && nil_field_sym.kind != .function { |
| 397 | c.error('cannot assign `nil` to a non-pointer field', field.type_pos) |
| 398 | } |
| 399 | } |
| 400 | // Check for unnecessary inits like ` = 0` and ` = ''` |
| 401 | if field.typ.is_ptr() { |
| 402 | if field.default_expr is ast.IntegerLiteral { |
| 403 | if !c.inside_unsafe && !c.is_builtin_mod && field.default_expr.val == '0' { |
| 404 | c.error('default value of `0` for references can only be used inside `unsafe`', |
| 405 | field.default_expr.pos) |
| 406 | } |
| 407 | } |
| 408 | field_is_option := field.typ.has_flag(.option) |
| 409 | if field_is_option { |
| 410 | if field.default_expr is ast.None { |
| 411 | c.warn('unnecessary default value of `none`: struct fields are zeroed by default', |
| 412 | field.default_expr.pos) |
| 413 | } else if field.default_expr.is_nil() { |
| 414 | c.error('cannot assign `nil` to option value', field.default_expr.pos()) |
| 415 | } |
| 416 | } |
| 417 | continue |
| 418 | } |
| 419 | if field.typ in ast.unsigned_integer_type_idxs { |
| 420 | if field.default_expr is ast.IntegerLiteral { |
| 421 | if field.default_expr.val[0] == `-` { |
| 422 | c.error('cannot assign negative value to unsigned integer type', |
| 423 | field.default_expr.pos) |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | if field.typ.has_flag(.option) { |
| 428 | if field.default_expr is ast.None { |
| 429 | c.warn('unnecessary default value of `none`: struct fields are zeroed by default', |
| 430 | field.default_expr.pos) |
| 431 | } |
| 432 | } else if field.typ.has_flag(.result) { |
| 433 | // struct field does not support result. Nothing to do |
| 434 | } else { |
| 435 | match field.default_expr { |
| 436 | ast.IntegerLiteral { |
| 437 | if field.default_expr.val == '0' { |
| 438 | c.warn('unnecessary default value of `0`: struct fields are zeroed by default', |
| 439 | field.default_expr.pos) |
| 440 | } |
| 441 | } |
| 442 | ast.StringLiteral { |
| 443 | if field.default_expr.val == '' { |
| 444 | c.warn("unnecessary default value of '': struct fields are zeroed by default", |
| 445 | field.default_expr.pos) |
| 446 | } |
| 447 | } |
| 448 | ast.BoolLiteral { |
| 449 | if field.default_expr.val == false { |
| 450 | c.warn('unnecessary default value `false`: struct fields are zeroed by default', |
| 451 | field.default_expr.pos) |
| 452 | } |
| 453 | } |
| 454 | else {} |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | // Ensure each generic type of the field was declared in the struct's definition |
| 459 | if node.generic_types.len > 0 && field.typ.has_flag(.generic) { |
| 460 | field_generic_names := c.table.generic_type_names(field.typ) |
| 461 | node_generic_names := node.generic_types.map(c.table.type_to_str(it)) |
| 462 | for name in field_generic_names { |
| 463 | if name !in node_generic_names { |
| 464 | struct_generic_names := node_generic_names.join(', ') |
| 465 | c.error('generic type name `${name}` is not mentioned in struct `${node.name}[${struct_generic_names}]`', |
| 466 | field.type_pos) |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | if node.generic_types.len == 0 && has_generic_types { |
| 472 | c.error('generic struct `${node.name}` declaration must specify the generic type names, e.g. ${node.name}[T]', |
| 473 | node.pos) |
| 474 | } |
| 475 | } |
| 476 | // Handle `implements` if it's present |
| 477 | if node.is_implements { |
| 478 | // XTODO2 |
| 479 | // cgen error if I use `println(sym)` without handling the option with `or{}` |
| 480 | struct_type := c.table.find_type(node.name) // or { panic(err) } |
| 481 | mut names_used := []string{} |
| 482 | for t in node.implements_types { |
| 483 | t_sym := c.table.sym(t.typ) |
| 484 | if t_sym.info is ast.Interface { |
| 485 | if t_sym.info.is_generic { |
| 486 | if t_sym.generic_types.len == 0 && !t.typ.has_flag(.generic) { |
| 487 | c.error('missing generic type on ${t_sym.name}', t.pos) |
| 488 | } else { |
| 489 | struct_generic_letters := node.generic_types.map(c.table.type_to_str(it)) |
| 490 | unknown_letters := |
| 491 | t_sym.generic_types.filter(it.has_flag(.generic)).map(c.table.type_to_str(it)).filter(it !in struct_generic_letters) |
| 492 | if unknown_letters.len > 0 { |
| 493 | c.error('unknown generic type ${unknown_letters.first()}', t.pos) |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | variant_name := c.table.type_to_str(t.typ) |
| 498 | if variant_name in names_used { |
| 499 | c.error('struct type ${node.name} cannot implement interface `${t_sym.name} more than once`', |
| 500 | t.pos) |
| 501 | } |
| 502 | names_used << variant_name |
| 503 | } else { |
| 504 | c.error('`${t_sym.name}` is not an interface type', t.pos) |
| 505 | } |
| 506 | c.type_implements(struct_type, t.typ, node.pos) |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | fn minify_sort_fn(a &ast.StructField, b &ast.StructField) int { |
| 512 | if a.typ == b.typ { |
| 513 | return 0 |
| 514 | } |
| 515 | // push all bool fields to the end of the struct |
| 516 | if a.typ == ast.bool_type_idx { |
| 517 | if b.typ == ast.bool_type_idx { |
| 518 | return 0 |
| 519 | } |
| 520 | return 1 |
| 521 | } else if b.typ == ast.bool_type_idx { |
| 522 | return -1 |
| 523 | } |
| 524 | |
| 525 | mut t := global_table |
| 526 | a_sym := t.sym(a.typ) |
| 527 | b_sym := t.sym(b.typ) |
| 528 | |
| 529 | // push all non-flag enums to the end too, just before the bool fields |
| 530 | // TODO: support enums with custom field values as well |
| 531 | if a_sym.info is ast.Enum { |
| 532 | if !a_sym.info.is_flag && !a_sym.info.uses_exprs { |
| 533 | return if b_sym.info is ast.Enum { |
| 534 | match true { |
| 535 | a_sym.info.vals.len > b_sym.info.vals.len { -1 } |
| 536 | a_sym.info.vals.len < b_sym.info.vals.len { 1 } |
| 537 | else { 0 } |
| 538 | } |
| 539 | } else { |
| 540 | 1 |
| 541 | } |
| 542 | } |
| 543 | } else if b_sym.info is ast.Enum { |
| 544 | if !b_sym.info.is_flag && !b_sym.info.uses_exprs { |
| 545 | return -1 |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | a_size, a_align := t.type_size(a.typ) |
| 550 | b_size, b_align := t.type_size(b.typ) |
| 551 | return match true { |
| 552 | a_align > b_align { -1 } |
| 553 | a_align < b_align { 1 } |
| 554 | a_size > b_size { -1 } |
| 555 | a_size < b_size { 1 } |
| 556 | else { 0 } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | fn (mut c Checker) struct_init_selector_type_expr(mut expr ast.SelectorExpr) ast.Type { |
| 561 | if !is_array_init_type_expr_field(expr.field_name) { |
| 562 | return ast.void_type |
| 563 | } |
| 564 | base_type := c.struct_init_type_expr(mut expr.expr) |
| 565 | if base_type == ast.void_type { |
| 566 | return ast.void_type |
| 567 | } |
| 568 | return c.type_resolver.typeof_field_type(base_type, expr.field_name) |
| 569 | } |
| 570 | |
| 571 | fn (mut c Checker) struct_init_type_expr(mut expr ast.Expr) ast.Type { |
| 572 | return match mut expr { |
| 573 | ast.TypeNode { |
| 574 | expr.typ |
| 575 | } |
| 576 | ast.ParExpr { |
| 577 | c.struct_init_type_expr(mut expr.expr) |
| 578 | } |
| 579 | ast.TypeOf { |
| 580 | if expr.is_type { |
| 581 | c.recheck_concrete_type(expr.typ) |
| 582 | } else { |
| 583 | if expr.typ == 0 || expr.typ == ast.void_type || expr.typ == ast.no_type { |
| 584 | expr.typ = c.expr(mut expr.expr) |
| 585 | } |
| 586 | resolved_type := c.recheck_concrete_type(expr.typ) |
| 587 | if resolved_type != 0 && resolved_type != ast.void_type |
| 588 | && resolved_type != ast.no_type { |
| 589 | resolved_type |
| 590 | } else { |
| 591 | c.recheck_concrete_type(c.type_resolver.typeof_type(expr.expr, expr.typ)) |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | ast.Ident { |
| 596 | if c.is_generic_type_expr_ident(expr.name) { |
| 597 | c.table.find_type(expr.name).set_flag(.generic) |
| 598 | } else { |
| 599 | c.get_expr_type(expr) |
| 600 | } |
| 601 | } |
| 602 | ast.SelectorExpr { |
| 603 | c.struct_init_selector_type_expr(mut expr) |
| 604 | } |
| 605 | else { |
| 606 | ast.void_type |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | fn (c &Checker) struct_init_uses_comptime_type_accessor(expr ast.Expr) bool { |
| 612 | return match expr { |
| 613 | ast.ParExpr { |
| 614 | c.struct_init_uses_comptime_type_accessor(expr.expr) |
| 615 | } |
| 616 | ast.SelectorExpr { |
| 617 | mut is_base_type_expr := expr.expr is ast.TypeOf |
| 618 | || c.struct_init_uses_comptime_type_accessor(expr.expr) |
| 619 | if expr.expr is ast.Ident { |
| 620 | is_base_type_expr = is_base_type_expr |
| 621 | || c.is_generic_type_expr_ident(expr.expr.name) |
| 622 | } |
| 623 | expr.field_name in ['idx', 'typ', 'unaliased_typ', 'key_type', 'value_type', 'element_type', 'pointee_type', 'payload_type'] |
| 624 | && is_base_type_expr |
| 625 | } |
| 626 | else { |
| 627 | false |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_init bool, mut inited_fields []string) ast.Type { |
| 633 | util.timing_start(@METHOD) |
| 634 | old_expected_type := c.expected_type |
| 635 | defer { |
| 636 | c.expected_type = old_expected_type |
| 637 | util.timing_measure_cumulative(@METHOD) |
| 638 | } |
| 639 | short_syntax_expected_type_sym := c.table.sym(c.unwrap_generic(c.expected_type)) |
| 640 | short_syntax_infers_anon_from_generic_param := node.is_short_syntax && node.typ == ast.void_type |
| 641 | && c.expected_type != ast.void_type && c.expected_type.has_flag(.generic) |
| 642 | && short_syntax_expected_type_sym.kind == .any |
| 643 | && !short_syntax_expected_type_sym.is_builtin() |
| 644 | is_comptime_type_struct_init := !node.is_short_syntax && node.typ_expr !is ast.EmptyExpr |
| 645 | && c.struct_init_uses_comptime_type_accessor(node.typ_expr) |
| 646 | should_resolve_typ_expr := node.typ == ast.void_type |
| 647 | || (is_comptime_type_struct_init && c.has_active_generic_recheck_context()) |
| 648 | if should_resolve_typ_expr && !node.is_short_syntax && node.typ_expr !is ast.EmptyExpr { |
| 649 | if !is_comptime_type_struct_init { |
| 650 | c.expr(mut node.typ_expr) |
| 651 | } |
| 652 | node.typ = c.struct_init_type_expr(mut node.typ_expr) |
| 653 | if node.typ == ast.void_type || node.typ == ast.no_type { |
| 654 | c.error('cannot use `${node.typ_expr}` as a struct init type', node.typ_expr.pos()) |
| 655 | return ast.void_type |
| 656 | } |
| 657 | node.unresolved = !is_comptime_type_struct_init && node.typ.has_flag(.generic) |
| 658 | } |
| 659 | source_typ := if node.is_short_syntax && c.expected_type != ast.void_type |
| 660 | && !short_syntax_infers_anon_from_generic_param { |
| 661 | c.expected_type |
| 662 | } else if node.typ != ast.void_type { |
| 663 | node.typ |
| 664 | } else { |
| 665 | c.expected_type |
| 666 | } |
| 667 | mut source_generic_typ := source_typ |
| 668 | source_sym := c.table.sym(c.unwrap_generic(source_typ)) |
| 669 | if source_sym.kind == .generic_inst && source_sym.info is ast.GenericInst { |
| 670 | source_generic_typ = |
| 671 | ast.new_type(source_sym.info.parent_idx).derive(source_typ).set_flag(.generic) |
| 672 | } else if source_sym.info is ast.Struct && source_sym.info.parent_type != 0 |
| 673 | && source_sym.info.concrete_types.len > 0 { |
| 674 | source_generic_typ = source_sym.info.parent_type.derive(source_typ).set_flag(.generic) |
| 675 | } |
| 676 | if node.generic_typ == 0 && source_generic_typ != ast.void_type |
| 677 | && !short_syntax_infers_anon_from_generic_param && (source_generic_typ.has_flag(.generic) |
| 678 | || c.type_has_unresolved_generic_parts(source_generic_typ)) { |
| 679 | node.generic_typ = source_generic_typ |
| 680 | } |
| 681 | if c.has_active_generic_recheck_context() { |
| 682 | if !node.is_short_syntax && node.generic_typ != 0 { |
| 683 | // Only re-resolve if the node's current type still has unresolved generic parts. |
| 684 | // A concrete type like MultiLevel[int] should not be re-resolved when visited |
| 685 | // inside a different generic context (e.g., as a nested init in MultiLevel[MultiLevel[int]]). |
| 686 | if node.typ.has_flag(.generic) || node.typ == ast.void_type { |
| 687 | // Try resolving node.typ first — it may use the function's generic names |
| 688 | // (e.g., Item[A] where A is from the enclosing function), which are directly |
| 689 | // resolvable via cur_concrete_types. Only fall back to node.generic_typ |
| 690 | // (which uses the struct's own generic param names like T) if node.typ can't resolve. |
| 691 | resolved_typ := c.recheck_concrete_type(node.typ) |
| 692 | if resolved_typ != node.typ && resolved_typ != 0 && resolved_typ != ast.void_type |
| 693 | && !resolved_typ.has_flag(.generic) { |
| 694 | node.typ = resolved_typ |
| 695 | } else { |
| 696 | resolved_generic_typ := c.recheck_concrete_type(node.generic_typ) |
| 697 | if resolved_generic_typ != 0 && resolved_generic_typ != ast.void_type { |
| 698 | node.typ = resolved_generic_typ |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | if node.is_short_syntax && c.has_active_generic_recheck_context() { |
| 705 | node.typ = ast.void_type |
| 706 | } |
| 707 | if node.typ == ast.void_type { |
| 708 | // short syntax `foo(key:val, key2:val2)` |
| 709 | if c.expected_type == ast.void_type { |
| 710 | c.error('unexpected short struct syntax', node.pos) |
| 711 | return ast.void_type |
| 712 | } |
| 713 | if short_syntax_infers_anon_from_generic_param { |
| 714 | node.typ = ast.none_type |
| 715 | } else { |
| 716 | sym := c.table.sym(c.expected_type) |
| 717 | if sym.kind == .array { |
| 718 | node.typ = c.table.value_type(c.expected_type) |
| 719 | } else { |
| 720 | node.typ = c.expected_type |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | original_node_typ := node.typ |
| 725 | concrete_node_typ := c.recheck_concrete_type(node.typ) |
| 726 | $if trace_veb_guard ? { |
| 727 | if c.file.path.contains('/vlib/veb/veb.v') { |
| 728 | node_type_str := if node.typ == 0 { '<none>' } else { c.table.type_to_str(node.typ) } |
| 729 | concrete_type_str := if concrete_node_typ == 0 { |
| 730 | '<none>' |
| 731 | } else { |
| 732 | c.table.type_to_str(concrete_node_typ) |
| 733 | } |
| 734 | generic_type_str := if node.generic_typ == 0 { |
| 735 | '<none>' |
| 736 | } else { |
| 737 | c.table.type_to_str(node.generic_typ) |
| 738 | } |
| 739 | expected_type_str := if c.expected_type == 0 { |
| 740 | '<none>' |
| 741 | } else { |
| 742 | c.table.type_to_str(c.expected_type) |
| 743 | } |
| 744 | eprintln('struct_init typ=${node_type_str} concrete=${concrete_type_str} generic_typ=${generic_type_str} expected=${expected_type_str} short=${node.is_short_syntax} fields=${node.init_fields.map(it.name)}') |
| 745 | } |
| 746 | } |
| 747 | struct_sym := c.table.sym(concrete_node_typ) |
| 748 | mut old_inside_generic_struct_init := false |
| 749 | mut old_cur_struct_generic_types := []ast.Type{} |
| 750 | mut old_cur_struct_concrete_types := []ast.Type{} |
| 751 | if struct_sym.info is ast.Struct { |
| 752 | // check if the generic param types have been defined |
| 753 | for ct in struct_sym.info.concrete_types { |
| 754 | ct_sym := c.table.sym(ct) |
| 755 | if ct_sym.kind == .placeholder { |
| 756 | c.error('unknown type `${ct_sym.name}`', node.pos) |
| 757 | } |
| 758 | } |
| 759 | if struct_sym.info.generic_types.len > 0 && struct_sym.info.concrete_types.len == 0 |
| 760 | && !node.is_short_syntax && c.table.cur_concrete_types.len != 0 |
| 761 | && !is_field_zero_struct_init { |
| 762 | if node.generic_types.len == 0 { |
| 763 | c.error('generic struct init must specify type parameter, e.g. Foo[T]', node.pos) |
| 764 | } else if node.generic_types.len > 0 |
| 765 | && node.generic_types.len != struct_sym.info.generic_types.len { |
| 766 | c.error('generic struct init expects ${struct_sym.info.generic_types.len} generic parameter, but got ${node.generic_types.len}', |
| 767 | node.pos) |
| 768 | } else if node.generic_types.len > 0 && c.table.cur_fn != unsafe { nil } { |
| 769 | for gtyp in node.generic_types { |
| 770 | if !gtyp.has_flag(.generic) { |
| 771 | continue |
| 772 | } |
| 773 | gtyp_name := c.table.sym(gtyp).name |
| 774 | if gtyp_name.len == 1 && gtyp_name !in c.table.cur_fn.generic_names { |
| 775 | cur_generic_names := '(' + c.table.cur_fn.generic_names.join(',') + ')' |
| 776 | c.error('generic struct init type parameter `${gtyp_name}` must be within the parameters `${cur_generic_names}` of the current generic function', |
| 777 | node.pos) |
| 778 | break |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | if node.generic_types.len > 0 && struct_sym.info.generic_types.len == 0 { |
| 784 | c.error('a non generic struct `${node.typ_str}` used like a generic struct', |
| 785 | node.name_pos) |
| 786 | } |
| 787 | if struct_sym.info.generic_types.len > 0 |
| 788 | && struct_sym.info.generic_types.len == struct_sym.info.concrete_types.len { |
| 789 | old_inside_generic_struct_init = c.inside_generic_struct_init |
| 790 | old_cur_struct_generic_types = c.cur_struct_generic_types.clone() |
| 791 | old_cur_struct_concrete_types = c.cur_struct_concrete_types.clone() |
| 792 | c.inside_generic_struct_init = true |
| 793 | c.cur_struct_generic_types = struct_sym.info.generic_types.clone() |
| 794 | c.cur_struct_concrete_types = struct_sym.info.concrete_types.clone() |
| 795 | defer(fn) { |
| 796 | c.inside_generic_struct_init = old_inside_generic_struct_init |
| 797 | c.cur_struct_generic_types = old_cur_struct_generic_types |
| 798 | c.cur_struct_concrete_types = old_cur_struct_concrete_types |
| 799 | } |
| 800 | } |
| 801 | if struct_sym.info.is_union && node.init_fields.len > 1 { |
| 802 | c.error('union `${struct_sym.name}` can have only one field initialised', node.pos) |
| 803 | } |
| 804 | } else if struct_sym.info is ast.GenericInst { |
| 805 | // For generic_inst types (concrete generic structs like Seq[int]), |
| 806 | // set up the generic struct init context using the parent's generic types |
| 807 | // and the inst's concrete types. |
| 808 | parent_sym := c.table.sym(ast.new_type(struct_sym.info.parent_idx)) |
| 809 | if parent_sym.info is ast.Struct { |
| 810 | if parent_sym.info.generic_types.len > 0 |
| 811 | && parent_sym.info.generic_types.len == struct_sym.info.concrete_types.len { |
| 812 | old_inside_generic_struct_init = c.inside_generic_struct_init |
| 813 | old_cur_struct_generic_types = c.cur_struct_generic_types.clone() |
| 814 | old_cur_struct_concrete_types = c.cur_struct_concrete_types.clone() |
| 815 | c.inside_generic_struct_init = true |
| 816 | c.cur_struct_generic_types = parent_sym.info.generic_types.clone() |
| 817 | c.cur_struct_concrete_types = struct_sym.info.concrete_types.clone() |
| 818 | defer(fn) { |
| 819 | c.inside_generic_struct_init = old_inside_generic_struct_init |
| 820 | c.cur_struct_generic_types = old_cur_struct_generic_types |
| 821 | c.cur_struct_concrete_types = old_cur_struct_concrete_types |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | } else if struct_sym.info is ast.FnType { |
| 826 | c.error('functions must be defined, not instantiated like structs', node.pos) |
| 827 | } |
| 828 | // register generic struct type when current fn is generic fn |
| 829 | if c.table.cur_fn != unsafe { nil } && c.table.cur_fn.generic_names.len > 0 { |
| 830 | c.table.unwrap_generic_type_ex(node.typ, c.table.cur_fn.generic_names, |
| 831 | c.table.cur_concrete_types, true) |
| 832 | if c.pref.skip_unused && node.typ.has_flag(.generic) { |
| 833 | c.table.used_features.comptime_syms[c.unwrap_generic(node.typ)] = true |
| 834 | c.table.used_features.comptime_syms[node.typ] = true |
| 835 | } |
| 836 | } |
| 837 | type_sym := c.table.sym(concrete_node_typ) |
| 838 | is_generic_zero_struct_init := original_node_typ.has_flag(.generic) && node.init_fields.len == 0 |
| 839 | && !node.has_update_expr |
| 840 | is_comptime_type_zero_struct_init := node.init_fields.len == 0 && !node.has_update_expr |
| 841 | && is_comptime_type_struct_init |
| 842 | if is_generic_zero_struct_init { |
| 843 | // Don't early-return for single-letter types (like F{}) in non-generic functions — |
| 844 | // these are unknown structs that should be caught by ensure_type_exists below. |
| 845 | is_unknown_single_letter := type_sym.kind == .any && type_sym.name.len == 1 |
| 846 | && (c.table.cur_fn == unsafe { nil } || type_sym.name !in c.table.cur_fn.generic_names) |
| 847 | if !is_unknown_single_letter { |
| 848 | return concrete_node_typ |
| 849 | } |
| 850 | } |
| 851 | if !is_field_zero_struct_init && !is_comptime_type_zero_struct_init { |
| 852 | type_exists := c.ensure_type_exists(node.typ, node.pos) |
| 853 | if !type_exists && node.typ.idx() > 0 && c.table.sym(node.typ).kind == .placeholder { |
| 854 | return ast.void_type |
| 855 | } |
| 856 | } |
| 857 | if c.anon_struct_should_be_mut { |
| 858 | mut anon_type_sym := c.table.sym(node.typ) |
| 859 | if anon_type_sym.kind == .struct { |
| 860 | mut anon_info := anon_type_sym.info as ast.Struct |
| 861 | if anon_info.is_anon { |
| 862 | mut anon_fields := []ast.StructField{cap: anon_info.fields.len} |
| 863 | for field in anon_info.fields { |
| 864 | anon_fields << ast.StructField{ |
| 865 | ...field |
| 866 | is_mut: true |
| 867 | } |
| 868 | } |
| 869 | anon_info.fields = anon_fields |
| 870 | anon_type_sym.info = anon_info |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | // Make sure the first letter is capital, do not allow e.g. `x := string{}`, |
| 875 | // but `x := T{}` is ok. |
| 876 | if !c.is_builtin_mod && !c.inside_unsafe && type_sym.language == .v |
| 877 | && c.table.cur_concrete_types.len == 0 { |
| 878 | pos := type_sym.name.last_index_u8(`.`) |
| 879 | first_letter := type_sym.name[pos + 1] |
| 880 | if !first_letter.is_capital() && type_sym.kind != .none |
| 881 | && (type_sym.kind != .struct || !(type_sym.info is ast.Struct && type_sym.info.is_anon)) |
| 882 | && type_sym.kind != .placeholder { |
| 883 | c.error('cannot initialize builtin type `${type_sym.name}`', node.pos) |
| 884 | } |
| 885 | if type_sym.kind == .enum && !c.pref.translated && !c.file.is_translated { |
| 886 | c.error('cannot initialize enums', node.pos) |
| 887 | } |
| 888 | } |
| 889 | if type_sym.kind == .sum_type && node.init_fields.len == 1 { |
| 890 | sexpr := node.init_fields[0].expr.str() |
| 891 | c.error('cast to sum type using `${type_sym.name}(${sexpr})` not `${type_sym.name}{${sexpr}}`', |
| 892 | node.pos) |
| 893 | } |
| 894 | if type_sym.kind == .interface && type_sym.language != .js { |
| 895 | c.error('cannot instantiate interface `${type_sym.name}`', node.pos) |
| 896 | } |
| 897 | // allow init structs from generic if they're private except the type is from builtin module |
| 898 | is_generic_init := concrete_node_typ.has_flag(.generic) |
| 899 | || (node.generic_typ != 0 && node.generic_typ.has_flag(.generic)) |
| 900 | if !node.has_update_expr && !type_sym.is_pub && type_sym.kind != .placeholder |
| 901 | && type_sym.language != .c |
| 902 | && (type_sym.mod != c.mod && !(is_generic_init && type_sym.mod != 'builtin')) |
| 903 | && !is_field_zero_struct_init && !is_comptime_type_zero_struct_init { |
| 904 | c.error('type `${type_sym.name}` is private', node.pos) |
| 905 | } |
| 906 | if type_sym.info is ast.Struct && type_sym.mod != c.mod && !is_field_zero_struct_init { |
| 907 | for attr in type_sym.info.attrs { |
| 908 | match attr.name { |
| 909 | 'noinit' { |
| 910 | c.error( |
| 911 | 'struct `${type_sym.name}` is declared with a `@[noinit]` attribute, so ' + |
| 912 | 'it cannot be initialized with `${type_sym.name}{}`', node.pos) |
| 913 | } |
| 914 | 'deprecated' { |
| 915 | c.deprecate('struct', type_sym.name, type_sym.info.attrs, node.pos) |
| 916 | } |
| 917 | else {} |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | if type_sym.name.len == 1 && c.table.cur_fn != unsafe { nil } |
| 922 | && c.table.cur_fn.generic_names.len == 0 { |
| 923 | c.error('unknown struct `${type_sym.name}`', node.pos) |
| 924 | return ast.void_type |
| 925 | } |
| 926 | match type_sym.kind { |
| 927 | .placeholder { |
| 928 | c.error('unknown struct: ${type_sym.name}', node.pos) |
| 929 | return ast.void_type |
| 930 | } |
| 931 | .any { |
| 932 | // `T{ foo: 22 }` |
| 933 | for mut init_field in node.init_fields { |
| 934 | init_field.typ = c.expr(mut init_field.expr) |
| 935 | init_field.expected_type = init_field.typ |
| 936 | } |
| 937 | sym := c.table.sym(c.unwrap_generic(node.typ)) |
| 938 | if sym.info is ast.Struct { |
| 939 | if sym.mod != c.mod { |
| 940 | for attr in sym.info.attrs { |
| 941 | match attr.name { |
| 942 | 'noinit' { |
| 943 | c.error( |
| 944 | 'struct `${sym.name}` is declared with a `@[noinit]` attribute, so ' + |
| 945 | 'it cannot be initialized with `${sym.name}{}`', node.pos) |
| 946 | } |
| 947 | 'deprecated' { |
| 948 | c.deprecate('struct', sym.name, sym.info.attrs, node.pos) |
| 949 | } |
| 950 | else {} |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | if node.no_keys && node.init_fields.len != sym.info.fields.len { |
| 955 | fname := if sym.info.fields.len != 1 { 'fields' } else { 'field' } |
| 956 | c.error('initializing struct `${sym.name}` needs `${sym.info.fields.len}` ${fname}, but got `${node.init_fields.len}`', |
| 957 | node.pos) |
| 958 | } |
| 959 | } |
| 960 | } |
| 961 | // string & array are also structs but .kind of string/array |
| 962 | .struct, .string, .array, .alias, .generic_inst { |
| 963 | mut info := ast.Struct{} |
| 964 | if type_sym.kind == .alias { |
| 965 | info_t := type_sym.info as ast.Alias |
| 966 | sym := c.table.final_sym(info_t.parent_type) |
| 967 | if sym.kind == .placeholder { // pending import symbol did not resolve |
| 968 | c.error('unknown struct: ${type_sym.name}', node.pos) |
| 969 | return ast.void_type |
| 970 | } |
| 971 | match sym.kind { |
| 972 | .struct { |
| 973 | info = sym.info as ast.Struct |
| 974 | } |
| 975 | .array, .array_fixed, .map { |
| 976 | // we do allow []int{}, [10]int{}, map[string]int{} |
| 977 | } |
| 978 | .sum_type, .interface { |
| 979 | // allow alias-of-sumtype/interface init (e.g. SumAlias{}) |
| 980 | } |
| 981 | else { |
| 982 | if !((sym.is_number() || sym.kind == .string |
| 983 | || sym.kind == .bool || sym.kind == .enum) && node.init_fields.len == 0 |
| 984 | && !node.has_update_expr) && !c.has_active_generic_recheck_context() { |
| 985 | c.error('alias type name: ${sym.name} is not struct type', node.pos) |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | } else if type_sym.kind == .generic_inst && type_sym.info is ast.GenericInst { |
| 990 | parent_sym := c.table.sym(ast.new_type(type_sym.info.parent_idx)) |
| 991 | if parent_sym.info is ast.Struct { |
| 992 | info = parent_sym.info |
| 993 | } |
| 994 | } else { |
| 995 | info = type_sym.info as ast.Struct |
| 996 | } |
| 997 | if node.no_keys { |
| 998 | exp_len := info.fields.len |
| 999 | got_len := node.init_fields.len |
| 1000 | if exp_len != got_len && !c.pref.translated { |
| 1001 | // XTODO remove !translated check |
| 1002 | amount := if exp_len < got_len { 'many' } else { 'few' } |
| 1003 | c.error('too ${amount} fields in `${type_sym.name}` literal (expecting ${exp_len}, got ${got_len})', |
| 1004 | node.pos) |
| 1005 | } |
| 1006 | } |
| 1007 | mut info_fields_sorted := []ast.StructField{} |
| 1008 | if node.no_keys { |
| 1009 | info_fields_sorted = info.fields.clone() |
| 1010 | info_fields_sorted.sort(a.i < b.i) |
| 1011 | } |
| 1012 | for i, mut init_field in node.init_fields { |
| 1013 | mut field_info := ast.StructField{} |
| 1014 | mut field_name := '' |
| 1015 | if node.no_keys { |
| 1016 | if i >= info.fields.len { |
| 1017 | // It doesn't make sense to check for fields that don't exist. |
| 1018 | // We should just stop here. |
| 1019 | break |
| 1020 | } |
| 1021 | field_info = info_fields_sorted[i] |
| 1022 | field_name = field_info.name |
| 1023 | node.init_fields[i].name = field_name |
| 1024 | } else { |
| 1025 | field_name = init_field.name |
| 1026 | mut exists := true |
| 1027 | field_info = c.table.find_field_with_embeds(type_sym, field_name) or { |
| 1028 | exists = false |
| 1029 | ast.StructField{} |
| 1030 | } |
| 1031 | if !exists { |
| 1032 | existing_fields := c.table.struct_fields(type_sym).map(it.name) |
| 1033 | c.error(util.new_suggestion(init_field.name, existing_fields).say('unknown field `${init_field.name}` in struct literal of type `${type_sym.name}`'), |
| 1034 | init_field.pos) |
| 1035 | continue |
| 1036 | } |
| 1037 | if field_name in inited_fields { |
| 1038 | c.error('duplicate field name in struct literal: `${field_name}`', |
| 1039 | init_field.pos) |
| 1040 | continue |
| 1041 | } |
| 1042 | } |
| 1043 | mut got_type := ast.no_type |
| 1044 | mut exp_type := ast.no_type |
| 1045 | inited_fields << field_name |
| 1046 | exp_type = field_info.typ |
| 1047 | if c.inside_generic_struct_init && exp_type.has_flag(.generic) { |
| 1048 | generic_names := c.cur_struct_generic_types.map(c.table.sym(it).name) |
| 1049 | if unwrapped := c.table.convert_generic_type(exp_type, generic_names, |
| 1050 | c.cur_struct_concrete_types) |
| 1051 | { |
| 1052 | exp_type = unwrapped |
| 1053 | } |
| 1054 | } |
| 1055 | exp_type = c.recheck_concrete_type(exp_type) |
| 1056 | exp_type_sym := c.table.sym(exp_type) |
| 1057 | exp_final_sym := if exp_type_sym.kind == .generic_inst { |
| 1058 | c.table.sym(ast.new_type((exp_type_sym.info as ast.GenericInst).parent_idx)) |
| 1059 | } else { |
| 1060 | exp_type_sym |
| 1061 | } |
| 1062 | c.expected_type = exp_type |
| 1063 | nr_errors_before := c.nr_errors |
| 1064 | got_type = c.expr(mut init_field.expr) |
| 1065 | got_type_sym := c.table.sym(got_type) |
| 1066 | if got_type == ast.void_type && c.nr_errors == nr_errors_before { |
| 1067 | c.error('`${init_field.expr}` (no value) used as value', init_field.pos) |
| 1068 | } |
| 1069 | exp_type_is_option := exp_type.has_flag(.option) |
| 1070 | if !exp_type_is_option { |
| 1071 | got_type = c.check_expr_option_or_result_call(init_field.expr, got_type) |
| 1072 | init_field.typ = got_type |
| 1073 | has_or_block := match mut init_field.expr { |
| 1074 | ast.IndexExpr { init_field.expr.or_expr.kind != .absent } |
| 1075 | ast.CallExpr { init_field.expr.or_block.kind != .absent } |
| 1076 | ast.SelectorExpr { init_field.expr.or_block.kind != .absent } |
| 1077 | else { false } |
| 1078 | } |
| 1079 | |
| 1080 | if got_type.has_flag(.option) && !has_or_block { |
| 1081 | c.error('cannot assign an Option value to a non-option struct field', |
| 1082 | init_field.pos) |
| 1083 | } else if got_type.has_flag(.result) && !has_or_block { |
| 1084 | c.error('cannot assign a Result value to a non-option struct field', |
| 1085 | init_field.pos) |
| 1086 | } |
| 1087 | } |
| 1088 | if got_type.has_flag(.result) { |
| 1089 | c.check_expr_option_or_result_call(init_field.expr, init_field.typ) |
| 1090 | } |
| 1091 | if exp_type_is_option && got_type.is_ptr() && !exp_type.is_ptr() { |
| 1092 | c.error('cannot assign a pointer to option struct field', init_field.pos) |
| 1093 | } |
| 1094 | c.warn_if_integer_literal_overflow_for_known_type(exp_type, init_field.expr, |
| 1095 | init_field.pos) |
| 1096 | if exp_type_sym.kind == .voidptr { |
| 1097 | if got_type_sym.kind == .struct && !got_type.is_ptr() { |
| 1098 | c.error('allocate `${got_type_sym.name}` on the heap for use in other functions', |
| 1099 | init_field.pos) |
| 1100 | } else if got_type !in [ast.nil_type, ast.voidptr_type, ast.byteptr_type] |
| 1101 | && !got_type.is_ptr() && (init_field.expr !is ast.IntegerLiteral |
| 1102 | || (init_field.expr is ast.IntegerLiteral |
| 1103 | && init_field.expr.val.int() != 0)) { |
| 1104 | c.note('voidptr variables may only be assigned voidptr values (e.g. unsafe { voidptr(${init_field.expr.str()}) })', |
| 1105 | init_field.expr.pos()) |
| 1106 | } |
| 1107 | } |
| 1108 | // disallow `mut a: b`, when b is const map |
| 1109 | if exp_type_sym.kind == .map && got_type_sym.kind == .map && !got_type.is_ptr() |
| 1110 | && field_info.is_mut |
| 1111 | && (init_field.expr is ast.Ident && init_field.expr.obj is ast.ConstField) { |
| 1112 | c.error('cannot assign a const map to mut struct field, call `clone` method (or use a reference)', |
| 1113 | init_field.expr.pos()) |
| 1114 | } |
| 1115 | if c.is_nocopy_struct(exp_type) && c.is_nocopy_struct(got_type) |
| 1116 | && init_field.expr !is ast.StructInit { |
| 1117 | c.error('cannot copy @[nocopy] struct: use a reference instead', |
| 1118 | init_field.expr.pos()) |
| 1119 | } |
| 1120 | if exp_type_sym.kind == .array && got_type_sym.kind == .array { |
| 1121 | init_field_expr_pos := init_field.expr.pos() |
| 1122 | if init_field.expr is ast.IndexExpr && init_field.expr.left is ast.Ident |
| 1123 | && ((init_field.expr as ast.IndexExpr).left.is_mut() |
| 1124 | || field_info.is_mut) && init_field.expr.index is ast.RangeExpr |
| 1125 | && !c.inside_unsafe { |
| 1126 | // `a: arr[..]` auto add clone() -> `a: arr[..].clone()` |
| 1127 | c.add_error_detail_with_pos('To silence this notice, use either an explicit `a[..].clone()`, |
| 1128 | or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.', |
| 1129 | init_field_expr_pos) |
| 1130 | c.note('an implicit clone of the slice was done here', init_field_expr_pos) |
| 1131 | mut right := ast.CallExpr{ |
| 1132 | name: 'clone' |
| 1133 | kind: .clone |
| 1134 | left: init_field.expr |
| 1135 | left_type: got_type |
| 1136 | is_method: true |
| 1137 | receiver_type: got_type.ref() |
| 1138 | return_type: got_type |
| 1139 | scope: c.fn_scope |
| 1140 | is_return_used: true |
| 1141 | } |
| 1142 | got_type = c.expr(mut right) |
| 1143 | node.init_fields[i].expr = right |
| 1144 | } |
| 1145 | // disallow `mut a: b`, when b is const array |
| 1146 | if field_info.is_mut |
| 1147 | && (init_field.expr is ast.Ident && init_field.expr.obj is ast.ConstField) |
| 1148 | && !c.inside_unsafe { |
| 1149 | c.error('cannot assign a const array to mut struct field, call `clone` method (or use `unsafe`)', |
| 1150 | init_field.expr.pos()) |
| 1151 | } |
| 1152 | } |
| 1153 | if exp_final_sym.kind == .interface { |
| 1154 | if c.type_implements(got_type, exp_type, init_field.pos) { |
| 1155 | if !c.inside_unsafe && got_type_sym.kind != .interface |
| 1156 | && !got_type.is_any_kind_of_pointer() { |
| 1157 | c.mark_as_referenced(mut &init_field.expr, true) |
| 1158 | } |
| 1159 | } |
| 1160 | } else if got_type != ast.void_type && got_type_sym.kind != .placeholder |
| 1161 | && !exp_type.has_flag(.generic) { |
| 1162 | mut needs_sum_type_cast := false |
| 1163 | if exp_type_sym.kind == .placeholder { |
| 1164 | base_type := c.table.find_type(exp_type_sym.ngname) |
| 1165 | if base_type != 0 { |
| 1166 | base_sym := c.table.sym(base_type) |
| 1167 | if base_sym.kind == .sum_type && base_sym.info is ast.SumType { |
| 1168 | base_info := base_sym.info as ast.SumType |
| 1169 | for variant in base_info.variants { |
| 1170 | if c.table.sym(variant).ngname == got_type_sym.ngname { |
| 1171 | needs_sum_type_cast = true |
| 1172 | break |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | if needs_sum_type_cast { |
| 1179 | init_field.expr = ast.CastExpr{ |
| 1180 | expr: init_field.expr |
| 1181 | typ: exp_type |
| 1182 | typname: c.table.type_to_str(exp_type) |
| 1183 | pos: init_field.expr.pos() |
| 1184 | } |
| 1185 | init_field.typ = exp_type |
| 1186 | } else if c.has_direct_numeric_alias_struct_init_mismatch(init_field.expr, |
| 1187 | got_type, exp_type) |
| 1188 | { |
| 1189 | c.error('cannot assign to field `${field_info.name}`: ${c.expected_msg(got_type, |
| 1190 | exp_type)}', init_field.pos) |
| 1191 | } else { |
| 1192 | c.check_expected(c.unwrap_generic(got_type), c.unwrap_generic(exp_type)) or { |
| 1193 | // For generic types, the same concrete type may have been |
| 1194 | // registered with different indices through different code |
| 1195 | // paths. Compare by type string as a fallback. |
| 1196 | got_unwrapped := c.unwrap_generic(got_type) |
| 1197 | exp_unwrapped := c.unwrap_generic(exp_type) |
| 1198 | if c.table.type_to_str(got_unwrapped) == c.table.type_to_str(exp_unwrapped) { |
| 1199 | // Same concrete type by name, different index - accept |
| 1200 | } else if field_info.typ.has_flag(.generic) |
| 1201 | || exp_type != field_info.typ { |
| 1202 | c.error('cannot assign `${c.table.type_to_str(got_unwrapped)}` to struct field `${field_info.name}` with type `${c.table.type_to_str(exp_unwrapped)}`', |
| 1203 | init_field.expr.pos()) |
| 1204 | } else { |
| 1205 | c.error('cannot assign to field `${field_info.name}`: ${err.msg()}', |
| 1206 | init_field.pos) |
| 1207 | } |
| 1208 | } |
| 1209 | } |
| 1210 | } |
| 1211 | if exp_type.has_flag(.shared_f) { |
| 1212 | if !got_type.has_flag(.shared_f) && got_type.is_ptr() { |
| 1213 | c.error('`shared` field must be initialized with `shared` or value', |
| 1214 | init_field.pos) |
| 1215 | } |
| 1216 | } else { |
| 1217 | if !c.inside_unsafe && type_sym.language == .v && !(c.file.is_translated |
| 1218 | || c.pref.translated) && exp_type.is_ptr() |
| 1219 | && !got_type.is_any_kind_of_pointer() && !exp_type_is_option |
| 1220 | && !(init_field.expr is ast.UnsafeExpr && init_field.expr.expr.str() == '0') { |
| 1221 | if init_field.expr.str() == '0' { |
| 1222 | c.error('assigning `0` to a reference field is only allowed in `unsafe` blocks', |
| 1223 | init_field.pos) |
| 1224 | } else { |
| 1225 | c.error('reference field must be initialized with reference', |
| 1226 | init_field.pos) |
| 1227 | } |
| 1228 | } else if exp_type.is_any_kind_of_pointer() |
| 1229 | && !got_type.is_any_kind_of_pointer() && !got_type.is_int() |
| 1230 | && (!exp_type_is_option || got_type.idx() != ast.none_type_idx) { |
| 1231 | got_typ_str := c.table.type_to_str(got_type) |
| 1232 | exp_typ_str := c.table.type_to_str(exp_type) |
| 1233 | c.error('cannot assign to field `${field_info.name}`: expected a pointer `${exp_typ_str}`, but got `${got_typ_str}`', |
| 1234 | init_field.pos) |
| 1235 | } |
| 1236 | } |
| 1237 | node.init_fields[i].typ = got_type |
| 1238 | node.init_fields[i].expected_type = exp_type |
| 1239 | |
| 1240 | if got_type.is_ptr() && exp_type.is_ptr() && mut init_field.expr is ast.Ident |
| 1241 | && !info.is_heap { |
| 1242 | c.fail_if_stack_struct_action_outside_unsafe(mut init_field.expr, 'assigned') |
| 1243 | } |
| 1244 | if c.table.unaliased_type(exp_type) in ast.unsigned_integer_type_idxs |
| 1245 | && mut init_field.expr is ast.IntegerLiteral |
| 1246 | && (init_field.expr as ast.IntegerLiteral).val[0] == `-` { |
| 1247 | c.error('cannot assign negative value to unsigned integer type', |
| 1248 | init_field.expr.pos) |
| 1249 | } |
| 1250 | |
| 1251 | if exp_type_sym.info is ast.Struct && !exp_type_sym.info.is_anon |
| 1252 | && mut init_field.expr is ast.StructInit && init_field.expr.is_anon { |
| 1253 | c.error('cannot assign anonymous `struct` to a typed `struct`', |
| 1254 | init_field.expr.pos) |
| 1255 | } |
| 1256 | |
| 1257 | // all the fields of initialized embedded struct are ignored, they are considered initialized |
| 1258 | sym := c.table.sym(init_field.typ) |
| 1259 | if init_field.is_embed && sym.kind == .struct && sym.language == .v { |
| 1260 | struct_fields := c.table.struct_fields(sym) |
| 1261 | for struct_field in struct_fields { |
| 1262 | inited_fields << struct_field.name |
| 1263 | } |
| 1264 | } |
| 1265 | expected_type_sym := c.table.final_sym(init_field.expected_type) |
| 1266 | if expected_type_sym.kind in [.string, .array, .map, .array_fixed, .chan, .struct] |
| 1267 | && init_field.expr.is_nil() && !init_field.expected_type.is_ptr() |
| 1268 | && mut init_field.expr is ast.UnsafeExpr { |
| 1269 | c.error('cannot assign `nil` to struct field `${init_field.name}` with type `${expected_type_sym.name}`', |
| 1270 | init_field.expr.pos.extend(init_field.expr.expr.pos())) |
| 1271 | } |
| 1272 | if mut init_field.expr is ast.CallExpr |
| 1273 | && init_field.expr.return_type.has_flag(.generic) { |
| 1274 | expected_type := c.unwrap_generic(init_field.expected_type) |
| 1275 | mut got_type_ret := c.unwrap_generic(init_field.expr.return_type) |
| 1276 | if init_field.expr.or_block.kind != .absent { |
| 1277 | got_type_ret = got_type_ret.clear_option_and_result() |
| 1278 | } |
| 1279 | if expected_type != got_type_ret { |
| 1280 | c.error('cannot assign `${c.table.type_to_str(got_type_ret)}` to struct field `${init_field.name}` with type `${c.table.type_to_str(expected_type)}`', |
| 1281 | init_field.expr.pos) |
| 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | if !node.has_update_expr { |
| 1286 | c.check_uninitialized_struct_fields_and_embeds(node, type_sym, mut info, mut |
| 1287 | inited_fields) |
| 1288 | } |
| 1289 | // println('>> checked_types.len: ${checked_types.len} | checked_types: ${checked_types} | type_sym: ${type_sym.name} ') |
| 1290 | } |
| 1291 | .sum_type { |
| 1292 | first_typ := (type_sym.info as ast.SumType).variants[0] |
| 1293 | first_sym := c.table.final_sym(first_typ) |
| 1294 | if first_sym.kind == .struct { |
| 1295 | mut info := first_sym.info as ast.Struct |
| 1296 | c.check_uninitialized_struct_fields_and_embeds(node, first_sym, mut info, mut |
| 1297 | inited_fields) |
| 1298 | } |
| 1299 | } |
| 1300 | .none { |
| 1301 | // var := struct { name: "" } |
| 1302 | mut init_fields := []ast.StructField{} |
| 1303 | for mut init_field in node.init_fields { |
| 1304 | mut expr := unsafe { init_field } |
| 1305 | init_field.typ = ast.mktyp(c.expr(mut expr.expr)) |
| 1306 | init_field.expected_type = init_field.typ |
| 1307 | init_fields << ast.StructField{ |
| 1308 | name: init_field.name |
| 1309 | typ: init_field.typ |
| 1310 | is_mut: c.anon_struct_should_be_mut |
| 1311 | } |
| 1312 | } |
| 1313 | c.table.anon_struct_counter++ |
| 1314 | name := '_VAnonStruct${c.table.anon_struct_counter}' |
| 1315 | sym_struct := ast.TypeSymbol{ |
| 1316 | kind: .struct |
| 1317 | language: .v |
| 1318 | name: name |
| 1319 | cname: util.no_dots(name) |
| 1320 | mod: c.mod |
| 1321 | info: ast.Struct{ |
| 1322 | is_anon: true |
| 1323 | fields: init_fields |
| 1324 | } |
| 1325 | is_pub: true |
| 1326 | } |
| 1327 | ret := c.table.register_sym(sym_struct) |
| 1328 | c.table.register_anon_struct(name, ret) |
| 1329 | node = ast.StructInit{ |
| 1330 | ...node |
| 1331 | typ: c.table.find_type_idx(name) |
| 1332 | typ_str: name |
| 1333 | is_anon: true |
| 1334 | is_short_syntax: false |
| 1335 | } |
| 1336 | } |
| 1337 | else {} |
| 1338 | } |
| 1339 | |
| 1340 | if node.has_update_expr { |
| 1341 | update_type := c.recheck_concrete_type(c.expr(mut node.update_expr)) |
| 1342 | node.update_expr_type = update_type |
| 1343 | expr_sym := c.table.final_sym(c.unwrap_generic(update_type)) |
| 1344 | if node.update_expr is ast.ComptimeSelector { |
| 1345 | c.error('cannot use struct update syntax in compile time expressions', |
| 1346 | node.update_expr_pos) |
| 1347 | } else if expr_sym.kind != .struct { |
| 1348 | s := c.table.type_to_str(update_type) |
| 1349 | c.error('expected struct, found `${s}`', node.update_expr.pos()) |
| 1350 | } else if update_type != concrete_node_typ { |
| 1351 | from_sym := c.table.final_sym(update_type) |
| 1352 | to_sym := c.table.final_sym(concrete_node_typ) |
| 1353 | from_info := from_sym.info as ast.Struct |
| 1354 | to_info := to_sym.info as ast.Struct |
| 1355 | // TODO: this check is too strict |
| 1356 | if !c.check_struct_signature(from_info, to_info) |
| 1357 | || !c.check_struct_signature_init_fields(from_info, to_info, node) { |
| 1358 | c.error('struct `${from_sym.name}` is not compatible with struct `${to_sym.name}`', |
| 1359 | node.update_expr.pos()) |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | if struct_sym.info is ast.Struct && struct_sym.info.generic_types.len > 0 |
| 1364 | && c.table.cur_concrete_types.len == 0 { |
| 1365 | if struct_sym.info.concrete_types.len == 0 { |
| 1366 | concrete_types := c.infer_struct_generic_types(node.typ, node) |
| 1367 | if concrete_types.len > 0 { |
| 1368 | idx := c.table.find_or_register_generic_inst(node.typ, concrete_types) |
| 1369 | if idx > 0 { |
| 1370 | node.typ = ast.new_type(idx) |
| 1371 | c.table.generic_insts_to_concrete() |
| 1372 | } |
| 1373 | } |
| 1374 | } else if struct_sym.info.generic_types.len == struct_sym.info.concrete_types.len { |
| 1375 | parent_type := struct_sym.info.parent_type |
| 1376 | parent_sym := c.table.sym(parent_type) |
| 1377 | if c.inside_generic_struct_init { |
| 1378 | mut st := unsafe { struct_sym.info } |
| 1379 | for mut field in st.fields { |
| 1380 | sym := c.table.sym(field.typ) |
| 1381 | if sym.info is ast.ArrayFixed && c.array_fixed_has_unresolved_size(sym.info) { |
| 1382 | mut size_expr := unsafe { sym.info.size_expr } |
| 1383 | field.typ = c.eval_array_fixed_sizes(mut size_expr, 0, sym.info.elem_type) |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | for method in parent_sym.methods { |
| 1388 | generic_names := struct_sym.info.generic_types.map(c.table.sym(it).name) |
| 1389 | for i, param in method.params { |
| 1390 | if i == 0 || !param.typ.has_flag(.generic) { |
| 1391 | continue |
| 1392 | } |
| 1393 | param_sym := c.table.sym(param.typ) |
| 1394 | if param_sym.kind in [.struct, .interface, .sum_type] { |
| 1395 | c.table.unwrap_generic_type(param.typ, generic_names, |
| 1396 | struct_sym.info.concrete_types) |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | return node.typ |
| 1403 | } |
| 1404 | |
| 1405 | fn (c &Checker) has_direct_numeric_alias_struct_init_mismatch(expr ast.Expr, got ast.Type, expected ast.Type) bool { |
| 1406 | if expr.remove_par() !is ast.Ident && expr.remove_par() !is ast.SelectorExpr { |
| 1407 | return false |
| 1408 | } |
| 1409 | got_sym := c.table.sym(got) |
| 1410 | if got_sym.kind != .alias || got_sym.info !is ast.Alias { |
| 1411 | return false |
| 1412 | } |
| 1413 | got_num_type := c.table.unalias_num_type(got).clear_flags() |
| 1414 | expected_num_type := c.table.unalias_num_type(expected).clear_flags() |
| 1415 | if !got_num_type.is_number() || !expected_num_type.is_number() { |
| 1416 | return false |
| 1417 | } |
| 1418 | return c.promote_num(expected_num_type, got_num_type) != expected_num_type |
| 1419 | } |
| 1420 | |
| 1421 | // Check uninitialized refs/sum types |
| 1422 | // The variable `fields` contains two parts, the first part is the same as info.fields, |
| 1423 | // and the second part is all fields embedded in the structure |
| 1424 | // If the return value data composition form in `c.table.struct_fields()` is modified, |
| 1425 | // need to modify here accordingly. |
| 1426 | fn (mut c Checker) check_uninitialized_struct_fields_and_embeds(node ast.StructInit, type_sym ast.TypeSymbol, mut info ast.Struct, mut inited_fields []string) { |
| 1427 | mut fields := c.table.struct_fields(type_sym) |
| 1428 | mut checked_types := []ast.Type{} |
| 1429 | for i, mut field in fields { |
| 1430 | if field.name in inited_fields { |
| 1431 | if c.mod != type_sym.mod { |
| 1432 | if !field.is_pub { |
| 1433 | parts := type_sym.name.split('.') |
| 1434 | for init_field in node.init_fields { |
| 1435 | if field.name == init_field.name { |
| 1436 | mod_type := if parts.len > 1 { |
| 1437 | parts#[-2..].join('.') |
| 1438 | } else { |
| 1439 | parts.last() |
| 1440 | } |
| 1441 | if !c.inside_unsafe && !(c.is_js_backend |
| 1442 | && mod_type.starts_with('Promise')) { |
| 1443 | c.error('cannot access private field `${field.name}` on `${mod_type}`', |
| 1444 | init_field.pos) |
| 1445 | |
| 1446 | break |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | if field.is_deprecated { |
| 1452 | for init_field in node.init_fields { |
| 1453 | if field.name == init_field.name { |
| 1454 | c.deprecate('field', field.name, field.attrs, init_field.pos) |
| 1455 | break |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | } |
| 1460 | continue |
| 1461 | } |
| 1462 | sym := c.table.sym(field.typ) |
| 1463 | if field.is_embed && sym.info is ast.Struct { |
| 1464 | // struct embeds |
| 1465 | continue |
| 1466 | } |
| 1467 | if field.has_default_expr { |
| 1468 | if i < info.fields.len && field.default_expr_typ == 0 { |
| 1469 | if mut field.default_expr is ast.StructInit { |
| 1470 | idx := c.table.find_type(field.default_expr.typ_str) |
| 1471 | if idx != 0 { |
| 1472 | info.fields[i].default_expr_typ = ast.new_type(int(idx)) |
| 1473 | } |
| 1474 | } else if field.default_expr.is_nil() { |
| 1475 | if field.typ.is_any_kind_of_pointer() { |
| 1476 | info.fields[i].default_expr_typ = field.typ |
| 1477 | } |
| 1478 | } else { |
| 1479 | is_ident_fn_default := field.default_expr is ast.Ident |
| 1480 | && field.default_expr.info is ast.IdentFn |
| 1481 | if is_ident_fn_default { |
| 1482 | mut default_expr := field.default_expr |
| 1483 | c.expr(mut default_expr) |
| 1484 | field.default_expr = default_expr |
| 1485 | } else if const_field := c.table.global_scope.find_const('${field.default_expr}') { |
| 1486 | info.fields[i].default_expr_typ = const_field.typ |
| 1487 | } else if type_sym.info is ast.Struct && type_sym.info.is_anon { |
| 1488 | c.expected_type = field.typ |
| 1489 | field.default_expr_typ = c.expr(mut field.default_expr) |
| 1490 | info.fields[i].default_expr_typ = field.default_expr_typ |
| 1491 | } |
| 1492 | } |
| 1493 | } |
| 1494 | continue |
| 1495 | } |
| 1496 | field_is_option := field.typ.has_flag(.option) |
| 1497 | if field.typ.is_ptr() && !field.typ.has_flag(.shared_f) && !field_is_option |
| 1498 | && !node.has_update_expr && !c.pref.translated && !c.file.is_translated { |
| 1499 | // Skip this check during generic recheck (concrete instantiation), |
| 1500 | // because generic code like `T{}` or `Struct[V]{}` cannot provide |
| 1501 | // initializers for reference fields that only appear after type substitution. |
| 1502 | if !c.has_active_generic_recheck_context() { |
| 1503 | c.error('reference field `${type_sym.name}.${field.name}` must be initialized', |
| 1504 | node.pos) |
| 1505 | continue |
| 1506 | } |
| 1507 | } |
| 1508 | if !field_is_option && !c.has_active_generic_recheck_context() { |
| 1509 | if sym.kind == .struct { |
| 1510 | c.check_ref_fields_initialized(sym, mut checked_types, |
| 1511 | '${type_sym.name}.${field.name}', node.pos) |
| 1512 | } else if sym.kind == .alias { |
| 1513 | parent_sym := c.table.sym((sym.info as ast.Alias).parent_type) |
| 1514 | if parent_sym.kind == .struct { |
| 1515 | c.check_ref_fields_initialized(parent_sym, mut checked_types, |
| 1516 | '${type_sym.name}.${field.name}', node.pos) |
| 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | // Do not allow empty uninitialized interfaces |
| 1521 | if sym.kind == .interface && !node.has_update_expr && !field_is_option |
| 1522 | && sym.language != .js && !field.attrs.contains('noinit') { |
| 1523 | // TODO: should be an error instead, but first `ui` needs updating. |
| 1524 | c.note('interface field `${type_sym.name}.${field.name}` must be initialized', node.pos) |
| 1525 | } |
| 1526 | // Do not allow empty uninitialized sum types |
| 1527 | /* |
| 1528 | sym := c.table.sym(field.typ) |
| 1529 | if sym.kind == .sum_type { |
| 1530 | c.warn('sum type field `${type_sym.name}.${field.name}` must be initialized', |
| 1531 | node.pos) |
| 1532 | } |
| 1533 | */ |
| 1534 | // Check for `@[required]` struct attr |
| 1535 | if !node.no_keys && !node.has_update_expr && field.attrs.contains('required') |
| 1536 | && node.init_fields.all(it.name != field.name) |
| 1537 | && !c.has_active_generic_recheck_context() { |
| 1538 | c.error('field `${type_sym.name}.${field.name}` must be initialized', node.pos) |
| 1539 | } |
| 1540 | if !node.has_update_expr && !field.has_default_expr && !field.typ.is_ptr() |
| 1541 | && !field_is_option { |
| 1542 | field_final_sym := c.table.final_sym(field.typ) |
| 1543 | if field_final_sym.kind == .struct { |
| 1544 | mut zero_struct_init := ast.StructInit{ |
| 1545 | pos: node.pos |
| 1546 | typ: field.typ |
| 1547 | } |
| 1548 | if field.is_part_of_union { |
| 1549 | if field.name in inited_fields { |
| 1550 | // fields that are part of an union, should only be checked, when they are explicitly initialised |
| 1551 | c.struct_init(mut zero_struct_init, true, mut inited_fields) |
| 1552 | } |
| 1553 | } else { |
| 1554 | c.struct_init(mut zero_struct_init, true, mut inited_fields) |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | for embed in info.embeds { |
| 1561 | embed_sym := c.table.final_sym(embed) |
| 1562 | if embed_sym.kind != .struct { |
| 1563 | continue |
| 1564 | } |
| 1565 | if embed_sym.info is ast.Struct { |
| 1566 | if embed_sym.info.is_union { |
| 1567 | mut embed_union_fields := c.table.struct_fields(embed_sym) |
| 1568 | mut found := false |
| 1569 | for init_field in inited_fields { |
| 1570 | for union_field in embed_union_fields { |
| 1571 | if init_field == union_field.name && found { |
| 1572 | c.error('embed union `${embed_sym.name}` can have only one field initialised', |
| 1573 | node.pos) |
| 1574 | } |
| 1575 | if init_field == union_field.name { |
| 1576 | found = true |
| 1577 | } |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | mut zero_struct_init := ast.StructInit{ |
| 1583 | pos: node.pos |
| 1584 | typ: embed |
| 1585 | } |
| 1586 | c.struct_init(mut zero_struct_init, true, mut inited_fields) |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | // Recursively check whether the struct type field is initialized |
| 1591 | fn (mut c Checker) check_ref_fields_initialized(struct_sym &ast.TypeSymbol, mut checked_types []ast.Type, |
| 1592 | linked_name string, pos &token.Pos) { |
| 1593 | if (c.pref.translated || c.file.is_translated) || struct_sym.language == .c { |
| 1594 | return |
| 1595 | } |
| 1596 | for field in c.table.struct_fields(struct_sym) { |
| 1597 | if field.typ in checked_types { |
| 1598 | continue |
| 1599 | } |
| 1600 | if field.has_default_expr { |
| 1601 | // The field is already initialized by its default expression. |
| 1602 | // Its nested reference fields should not be treated as missing. |
| 1603 | continue |
| 1604 | } |
| 1605 | if field.typ.is_ptr() && !field.typ.has_flag(.shared_f) && !field.typ.has_flag(.option) { |
| 1606 | c.error('reference field `${linked_name}.${field.name}` must be initialized (part of struct `${struct_sym.name}`)', |
| 1607 | pos) |
| 1608 | continue |
| 1609 | } |
| 1610 | sym := c.table.sym(field.typ) |
| 1611 | if sym.info is ast.Struct { |
| 1612 | if sym.language == .c { |
| 1613 | continue |
| 1614 | } |
| 1615 | if field.is_embed && sym.language == .v { |
| 1616 | // an embedded struct field |
| 1617 | continue |
| 1618 | } |
| 1619 | if field.typ.has_flag(.option) { |
| 1620 | // defaults to `none` |
| 1621 | continue |
| 1622 | } |
| 1623 | checked_types << field.typ |
| 1624 | c.check_ref_fields_initialized(sym, mut checked_types, '${linked_name}.${field.name}', |
| 1625 | pos) |
| 1626 | } else if sym.info is ast.Alias { |
| 1627 | psym := c.table.sym(sym.info.parent_type) |
| 1628 | if psym.kind == .struct { |
| 1629 | checked_types << field.typ |
| 1630 | c.check_ref_fields_initialized(psym, mut checked_types, |
| 1631 | '${linked_name}.${field.name}', pos) |
| 1632 | } |
| 1633 | } |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | // Recursively check whether the struct type field is initialized |
| 1638 | // NOTE: |
| 1639 | // This method is temporary and will only be called by the do_check_elements_ref_fields_initialized() method. |
| 1640 | // The goal is to give only a notice, not an error, for now. After a while, |
| 1641 | // when we change the notice to error, we can remove this temporary method. |
| 1642 | fn (mut c Checker) check_ref_fields_initialized_note(struct_sym &ast.TypeSymbol, mut checked_types []ast.Type, |
| 1643 | linked_name string, pos &token.Pos) { |
| 1644 | if (c.pref.translated || c.file.is_translated) || struct_sym.language == .c { |
| 1645 | return |
| 1646 | } |
| 1647 | for field in c.table.struct_fields(struct_sym) { |
| 1648 | if field.typ in checked_types { |
| 1649 | continue |
| 1650 | } |
| 1651 | if field.has_default_expr { |
| 1652 | // The field is already initialized by its default expression. |
| 1653 | // Its nested reference fields should not be treated as missing. |
| 1654 | continue |
| 1655 | } |
| 1656 | if field.typ.is_ptr() && !field.typ.has_flag(.shared_f) && !field.typ.has_flag(.option) { |
| 1657 | c.note('reference field `${linked_name}.${field.name}` must be initialized (part of struct `${struct_sym.name}`)', |
| 1658 | pos) |
| 1659 | continue |
| 1660 | } |
| 1661 | sym := c.table.sym(field.typ) |
| 1662 | if sym.info is ast.Struct { |
| 1663 | if sym.language == .c { |
| 1664 | continue |
| 1665 | } |
| 1666 | if field.is_embed && sym.language == .v { |
| 1667 | // an embedded struct field |
| 1668 | continue |
| 1669 | } |
| 1670 | if field.typ.has_flag(.option) { |
| 1671 | // defaults to `none` |
| 1672 | continue |
| 1673 | } |
| 1674 | checked_types << field.typ |
| 1675 | c.check_ref_fields_initialized(sym, mut checked_types, '${linked_name}.${field.name}', |
| 1676 | pos) |
| 1677 | } else if sym.info is ast.Alias { |
| 1678 | psym := c.table.sym(sym.info.parent_type) |
| 1679 | if psym.kind == .struct { |
| 1680 | checked_types << field.typ |
| 1681 | c.check_ref_fields_initialized(psym, mut checked_types, |
| 1682 | '${linked_name}.${field.name}', pos) |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | fn (mut c Checker) is_anon_struct_compatible(s1 ast.Struct, s2 ast.Struct) bool { |
| 1689 | if !(s1.is_anon && s2.is_anon && s1.fields.len == s2.fields.len) { |
| 1690 | return false |
| 1691 | } |
| 1692 | mut is_compatible := true |
| 1693 | for k, field in s1.fields { |
| 1694 | if !c.check_basic(field.typ, s2.fields[k].typ) { |
| 1695 | is_compatible = false |
| 1696 | break |
| 1697 | } |
| 1698 | } |
| 1699 | return is_compatible |
| 1700 | } |
| 1701 | |