commit 9aaafb78efda176bf64a18d2c2f1def0e4df3d01
parent 2a32144e15ac6e4cc1bca60b007b499a681fcbcf
Author: Armin Weigl <tb46305@gmail.com>
Date: Sun, 19 Jun 2022 23:01:38 +0200
resolve_enum_field: fix value calculation of first enum field
Signed-off-by: Armin Weigl <tb46305@gmail.com>
Diffstat:
2 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/src/check.c b/src/check.c
@@ -3686,6 +3686,8 @@ resolve_enum_field(struct context *ctx, const struct scope_object *obj)
if (obj && obj->otype == O_SCAN) {
// complete previous value first
obj = wrap_resolver(ctx, obj, resolve_enum_field);
+ } else {
+ obj = NULL;
}
value->type = EXPR_CONSTANT;
if (type_is_signed(type_dealias(type))) {
diff --git a/tests/15-enums.ha b/tests/15-enums.ha
@@ -129,6 +129,26 @@ fn aliases() void = {
assert(imported_double_alias::THREE == testmod::_enum::THREE);
};
+// Force T2 to be resolved before T1
+type t1 = enum {
+ I1 = t2::T1,
+ I2 = t2::T2,
+};
+
+type t2 = enum {
+ T1,
+ T2 = 1,
+ T3,
+};
+
+export fn resolution_order() void = {
+ assert(t2::T1 == 0);
+ assert(t2::T2 == 1);
+ assert(t2::T3 == 2);
+ assert(t1::I1 == 0);
+ assert(t1::I2 == 1);
+};
+
export fn main() void = {
implicit();
explicit();
@@ -136,4 +156,5 @@ export fn main() void = {
reject();
interdependent();
aliases();
+ resolution_order();
};