aboutsummaryrefslogtreecommitdiff
path: root/src/analyze.cpp
diff options
context:
space:
mode:
authorVexu <git@vexu.eu>2020-01-15 20:23:49 +0200
committerVexu <git@vexu.eu>2020-01-15 20:24:59 +0200
commit6fd0dddf186f6435f422f2992f44ec9a35e09f20 (patch)
tree4b285418ef2d5f47262247fa21040a7b0fb01c86 /src/analyze.cpp
parent8d9d4a065820bfce0395465834cb9b7e01509a12 (diff)
downloadzig-6fd0dddf186f6435f422f2992f44ec9a35e09f20.tar.gz
zig-6fd0dddf186f6435f422f2992f44ec9a35e09f20.zip
implement non-exhaustive enums
Diffstat (limited to 'src/analyze.cpp')
-rw-r--r--src/analyze.cpp18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/analyze.cpp b/src/analyze.cpp
index b7838003c8..a62e0414e0 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -2572,6 +2572,7 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
enum_type->data.enumeration.src_field_count = field_count;
enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count);
enum_type->data.enumeration.fields_by_name.init(field_count);
+ enum_type->data.enumeration.non_exhaustive = false;
Scope *scope = &enum_type->data.enumeration.decls_scope->base;
@@ -2648,6 +2649,21 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
buf_sprintf("consider 'union(enum)' here"));
}
+ AstNode *tag_value = field_node->data.struct_field.value;
+
+ if (buf_eql_str(type_enum_field->name, "_")) {
+ if (field_i != field_count - 1) {
+ add_node_error(g, field_node, buf_sprintf("'_' field of non-exhaustive enum must be last"));
+ enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
+ }
+ if (tag_value != nullptr) {
+ add_node_error(g, field_node, buf_sprintf("value assigned to '_' field of non-exhaustive enum"));
+ enum_type->data.enumeration.resolve_status = ResolveStatusInvalid;
+ }
+ enum_type->data.enumeration.non_exhaustive = true;
+ continue;
+ }
+
auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field);
if (field_entry != nullptr) {
ErrorMsg *msg = add_node_error(g, field_node,
@@ -2657,8 +2673,6 @@ static Error resolve_enum_zero_bits(CodeGen *g, ZigType *enum_type) {
continue;
}
- AstNode *tag_value = field_node->data.struct_field.value;
-
if (tag_value != nullptr) {
// A user-specified value is available
ZigValue *result = analyze_const_value(g, scope, tag_value, tag_int_type,