authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2019-04-14 22:47:53-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-16 13:55:23-04:00
log611d4bc6a1a99f4364b690dea688e81dd599daae
treebf54f26b0e1a80dd8e26035db431e3585143d07b
parent52caf311bbcd2f7792d19cb11dbae26eb59873fe

stage1: const_values_equal support tagged union


2 files changed, 40 insertions(+), 4 deletions(-)

src/analyze.cpp+3-4
...@@ -5158,11 +5158,10 @@ bool const_values_equal(CodeGen *g, ConstExprValue *a, ConstExprValue *b) {...@@ -5158,11 +5158,10 @@ bool const_values_equal(CodeGen *g, ConstExprValue *a, ConstExprValue *b) {
5158 if (bigint_cmp(&union1->tag, &union2->tag) == CmpEQ) {5158 if (bigint_cmp(&union1->tag, &union2->tag) == CmpEQ) {
5159 TypeUnionField *field = find_union_field_by_tag(a->type, &union1->tag);5159 TypeUnionField *field = find_union_field_by_tag(a->type, &union1->tag);
5160 assert(field != nullptr);5160 assert(field != nullptr);
5161 if (type_has_bits(field->type_entry)) {5161 if (!type_has_bits(field->type_entry))
5162 zig_panic("TODO const expr analyze union field value for equality");
5163 } else {
5164 return true;5162 return true;
5165 }5163 assert(find_union_field_by_tag(a->type, &union2->tag) != nullptr);
5164 return const_values_equal(g, union1->payload, union2->payload);
5166 }5165 }
5167 return false;5166 return false;
5168 }5167 }
test/stage1/behavior/union.zig+37
...@@ -365,3 +365,40 @@ test "@enumToInt works on unions" {...@@ -365,3 +365,40 @@ test "@enumToInt works on unions" {
365 expect(@enumToInt(b) == 1);365 expect(@enumToInt(b) == 1);
366 expect(@enumToInt(c) == 2);366 expect(@enumToInt(c) == 2);
367}367}
368
369const Attribute = union(enum) {
370 A: bool,
371 B: u8,
372};
373
374fn setAttribute(attr: Attribute) void {}
375
376fn Setter(attr: Attribute) type {
377 return struct{
378 fn set() void {
379 setAttribute(attr);
380 }
381 };
382}
383
384test "comptime union field value equality" {
385 const a0 = Setter(Attribute{ .A = false });
386 const a1 = Setter(Attribute{ .A = true });
387 const a2 = Setter(Attribute{ .A = false });
388
389 const b0 = Setter(Attribute{ .B = 5 });
390 const b1 = Setter(Attribute{ .B = 9 });
391 const b2 = Setter(Attribute{ .B = 5 });
392
393 expect(a0 == a0);
394 expect(a1 == a1);
395 expect(a0 == a2);
396
397 expect(b0 == b0);
398 expect(b1 == b1);
399 expect(b0 == b2);
400
401 expect(a0 != b0);
402 expect(a0 != a1);
403 expect(b0 != b1);
404}