| ... | ... | @@ -2603,16 +2603,16 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) { |
| 2603 | 2603 | if (decl_node->type == NodeTypeContainerDecl) { |
| 2604 | 2604 | assert(!enum_type->data.enumeration.fields); |
| 2605 | 2605 | field_count = (uint32_t)decl_node->data.container_decl.fields.length; |
| 2606 | | if (field_count == 0) { |
| 2607 | | add_node_error(g, decl_node, buf_sprintf("enums must have 1 or more fields")); |
| 2608 | | |
| 2609 | | enum_type->data.enumeration.src_field_count = field_count; |
| 2610 | | enum_type->data.enumeration.fields = nullptr; |
| 2611 | | enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; |
| 2612 | | return ErrorSemanticAnalyzeFail; |
| 2613 | | } |
| 2614 | 2606 | } else { |
| 2615 | | field_count = enum_type->data.enumeration.src_field_count; |
| 2607 | field_count = enum_type->data.enumeration.src_field_count + enum_type->data.enumeration.non_exhaustive; |
| 2608 | } |
| 2609 | |
| 2610 | if (field_count == 0) { |
| 2611 | add_node_error(g, decl_node, buf_sprintf("enums must have 1 or more fields")); |
| 2612 | enum_type->data.enumeration.src_field_count = field_count; |
| 2613 | enum_type->data.enumeration.fields = nullptr; |
| 2614 | enum_type->data.enumeration.resolve_status = ResolveStatusInvalid; |
| 2615 | return ErrorSemanticAnalyzeFail; |
| 2616 | 2616 | } |
| 2617 | 2617 | |
| 2618 | 2618 | Scope *scope = &enum_type->data.enumeration.decls_scope->base; |
| ... | ... | @@ -3072,18 +3072,19 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3072 | 3072 | if (decl_node->type == NodeTypeContainerDecl) { |
| 3073 | 3073 | assert(union_type->data.unionation.fields == nullptr); |
| 3074 | 3074 | field_count = (uint32_t)decl_node->data.container_decl.fields.length; |
| 3075 | | if (field_count == 0) { |
| 3076 | | add_node_error(g, decl_node, buf_sprintf("unions must have 1 or more fields")); |
| 3077 | | union_type->data.unionation.src_field_count = field_count; |
| 3078 | | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3079 | | return ErrorSemanticAnalyzeFail; |
| 3080 | | } |
| 3081 | 3075 | union_type->data.unionation.src_field_count = field_count; |
| 3082 | 3076 | union_type->data.unionation.fields = heap::c_allocator.allocate<TypeUnionField>(field_count); |
| 3083 | 3077 | union_type->data.unionation.fields_by_name.init(field_count); |
| 3084 | 3078 | } else { |
| 3085 | | assert(union_type->data.unionation.fields != nullptr); |
| 3086 | 3079 | field_count = union_type->data.unionation.src_field_count; |
| 3080 | assert(field_count == 0 || union_type->data.unionation.fields != nullptr); |
| 3081 | } |
| 3082 | |
| 3083 | if (field_count == 0) { |
| 3084 | add_node_error(g, decl_node, buf_sprintf("unions must have 1 or more fields")); |
| 3085 | union_type->data.unionation.src_field_count = field_count; |
| 3086 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3087 | return ErrorSemanticAnalyzeFail; |
| 3087 | 3088 | } |
| 3088 | 3089 | |
| 3089 | 3090 | Scope *scope = &union_type->data.unionation.decls_scope->base; |
| ... | ... | @@ -3217,47 +3218,47 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 3217 | 3218 | } |
| 3218 | 3219 | assert(field_type_val->special != ConstValSpecialRuntime); |
| 3219 | 3220 | union_field->type_val = field_type_val; |
| 3220 | | if (union_type->data.unionation.resolve_status == ResolveStatusInvalid) |
| 3221 | | return ErrorSemanticAnalyzeFail; |
| 3221 | } |
| 3222 | 3222 | |
| 3223 | | bool field_is_opaque_type; |
| 3224 | | if ((err = type_val_resolve_is_opaque_type(g, field_type_val, &field_is_opaque_type))) { |
| 3225 | | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3226 | | return ErrorSemanticAnalyzeFail; |
| 3227 | | } |
| 3228 | | if (field_is_opaque_type) { |
| 3229 | | add_node_error(g, field_node, |
| 3230 | | buf_create_from_str( |
| 3231 | | "opaque types have unknown size and therefore cannot be directly embedded in unions")); |
| 3232 | | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3233 | | return ErrorSemanticAnalyzeFail; |
| 3234 | | } |
| 3223 | if (field_node->data.struct_field.value != nullptr && !is_auto_enum) { |
| 3224 | ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.value, |
| 3225 | buf_create_from_str("untagged union field assignment")); |
| 3226 | add_error_note(g, msg, decl_node, buf_create_from_str("consider 'union(enum)' here")); |
| 3227 | } |
| 3228 | } |
| 3235 | 3229 | |
| 3236 | | switch (type_val_resolve_requires_comptime(g, field_type_val)) { |
| 3237 | | case ReqCompTimeInvalid: |
| 3238 | | if (g->trace_err != nullptr) { |
| 3239 | | g->trace_err = add_error_note(g, g->trace_err, field_node, |
| 3240 | | buf_create_from_str("while checking this field")); |
| 3241 | | } |
| 3242 | | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3243 | | return ErrorSemanticAnalyzeFail; |
| 3244 | | case ReqCompTimeYes: |
| 3245 | | union_type->data.unionation.requires_comptime = true; |
| 3246 | | break; |
| 3247 | | case ReqCompTimeNo: |
| 3248 | | break; |
| 3249 | | } |
| 3230 | if (union_field->type_val != nullptr) { |
| 3231 | bool field_is_opaque_type; |
| 3232 | if ((err = type_val_resolve_is_opaque_type(g, union_field->type_val, &field_is_opaque_type))) { |
| 3233 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3234 | return ErrorSemanticAnalyzeFail; |
| 3235 | } |
| 3236 | if (field_is_opaque_type) { |
| 3237 | add_node_error(g, union_field->decl_node, |
| 3238 | buf_create_from_str( |
| 3239 | "opaque types have unknown size and therefore cannot be directly embedded in unions")); |
| 3240 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3241 | return ErrorSemanticAnalyzeFail; |
| 3242 | } |
| 3250 | 3243 | |
| 3251 | | if ((err = type_val_resolve_zero_bits(g, field_type_val, union_type, nullptr, &is_zero_bits[i]))) { |
| 3244 | switch (type_val_resolve_requires_comptime(g, union_field->type_val)) { |
| 3245 | case ReqCompTimeInvalid: |
| 3246 | if (g->trace_err != nullptr) { |
| 3247 | g->trace_err = add_error_note(g, g->trace_err, union_field->decl_node, |
| 3248 | buf_create_from_str("while checking this field")); |
| 3249 | } |
| 3252 | 3250 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3253 | 3251 | return ErrorSemanticAnalyzeFail; |
| 3254 | | } |
| 3252 | case ReqCompTimeYes: |
| 3253 | union_type->data.unionation.requires_comptime = true; |
| 3254 | break; |
| 3255 | case ReqCompTimeNo: |
| 3256 | break; |
| 3255 | 3257 | } |
| 3256 | 3258 | |
| 3257 | | if (field_node->data.struct_field.value != nullptr && !is_auto_enum) { |
| 3258 | | ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.value, |
| 3259 | | buf_create_from_str("untagged union field assignment")); |
| 3260 | | add_error_note(g, msg, decl_node, buf_create_from_str("consider 'union(enum)' here")); |
| 3259 | if ((err = type_val_resolve_zero_bits(g, union_field->type_val, union_type, nullptr, &is_zero_bits[i]))) { |
| 3260 | union_type->data.unionation.resolve_status = ResolveStatusInvalid; |
| 3261 | return ErrorSemanticAnalyzeFail; |
| 3261 | 3262 | } |
| 3262 | 3263 | } |
| 3263 | 3264 | |