authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-04 00:56:27-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-04 00:56:27-05:00
log05d9f07541c8c5b788687046ad313c6cff211476
tree3d062505eca9d3cb18337066673a90bba70ca3f2
parentfce435db269162774106ce7e6ddf70871d5eeb49

more tests for unions

See #618

2 files changed, 115 insertions(+), 2 deletions(-)

src/ir.cpp+1-1
......@@ -12977,7 +12977,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1297712977 ErrorMsg *msg = ir_add_error(ira, target_value_ptr,
1297812978 buf_sprintf("switch on union which has no attached enum"));
1297912979 add_error_note(ira->codegen, msg, decl_node,
12980 buf_sprintf("union declared here"));
12980 buf_sprintf("consider 'union(enum)' here"));
1298112981 return ira->codegen->builtin_types.entry_invalid;
1298212982 }
1298312983 TypeTableEntry *tag_type = target_type->data.unionation.tag_type;
test/compile_errors.zig+114-1
......@@ -2479,7 +2479,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
24792479 \\ A: i32 = 20,
24802480 \\};
24812481 \\export fn entry() {
2482 \\ var x: MultipleChoice = undefined;
2482 \\ var x: MultipleChoice = undefined;
24832483 \\}
24842484 ,
24852485 ".tmp_source.zig:2:14: error: non-enum union field assignment",
......@@ -2493,6 +2493,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
24932493 ,
24942494 ".tmp_source.zig:1:13: error: enums must have 1 or more fields");
24952495
2496 cases.add("union with 0 fields",
2497 \\const Foo = union {};
2498 \\export fn entry() -> usize {
2499 \\ return @sizeOf(Foo);
2500 \\}
2501 ,
2502 ".tmp_source.zig:1:13: error: unions must have 1 or more fields");
2503
24962504 cases.add("enum value already taken",
24972505 \\const MultipleChoice = enum(u32) {
24982506 \\ A = 20,
......@@ -2571,4 +2579,109 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
25712579 ".tmp_source.zig:6:9: error: enum tag value 60 already taken",
25722580 ".tmp_source.zig:4:9: note: other occurrence here");
25732581
2582 cases.add("union enum field does not match enum",
2583 \\const Letter = enum {
2584 \\ A,
2585 \\ B,
2586 \\ C,
2587 \\};
2588 \\const Payload = union(Letter) {
2589 \\ A: i32,
2590 \\ B: f64,
2591 \\ C: bool,
2592 \\ D: bool,
2593 \\};
2594 \\export fn entry() {
2595 \\ var a = Payload {.A = 1234};
2596 \\}
2597 ,
2598 ".tmp_source.zig:10:5: error: enum field not found: 'D'",
2599 ".tmp_source.zig:1:16: note: enum declared here");
2600
2601 cases.add("field type supplied in an enum",
2602 \\const Letter = enum {
2603 \\ A: void,
2604 \\ B,
2605 \\ C,
2606 \\};
2607 \\export fn entry() {
2608 \\ var b = Letter.B;
2609 \\}
2610 ,
2611 ".tmp_source.zig:2:8: error: structs and unions, not enums, support field types",
2612 ".tmp_source.zig:1:16: note: consider 'union(enum)' here");
2613
2614 cases.add("struct field missing type",
2615 \\const Letter = struct {
2616 \\ A,
2617 \\};
2618 \\export fn entry() {
2619 \\ var a = Letter { .A = {} };
2620 \\}
2621 ,
2622 ".tmp_source.zig:2:5: error: struct field missing type");
2623
2624 cases.add("extern union field missing type",
2625 \\const Letter = extern union {
2626 \\ A,
2627 \\};
2628 \\export fn entry() {
2629 \\ var a = Letter { .A = {} };
2630 \\}
2631 ,
2632 ".tmp_source.zig:2:5: error: union field missing type");
2633
2634 cases.add("extern union given enum tag type",
2635 \\const Letter = enum {
2636 \\ A,
2637 \\ B,
2638 \\ C,
2639 \\};
2640 \\const Payload = extern union(Letter) {
2641 \\ A: i32,
2642 \\ B: f64,
2643 \\ C: bool,
2644 \\};
2645 \\export fn entry() {
2646 \\ var a = Payload { .A = { 1234 } };
2647 \\}
2648 ,
2649 ".tmp_source.zig:6:29: error: extern union does not support enum tag type");
2650
2651 cases.add("packed union given enum tag type",
2652 \\const Letter = enum {
2653 \\ A,
2654 \\ B,
2655 \\ C,
2656 \\};
2657 \\const Payload = packed union(Letter) {
2658 \\ A: i32,
2659 \\ B: f64,
2660 \\ C: bool,
2661 \\};
2662 \\export fn entry() {
2663 \\ var a = Payload { .A = { 1234 } };
2664 \\}
2665 ,
2666 ".tmp_source.zig:6:29: error: packed union does not support enum tag type");
2667
2668 cases.add("switch on union with no attached enum",
2669 \\const Payload = union {
2670 \\ A: i32,
2671 \\ B: f64,
2672 \\ C: bool,
2673 \\};
2674 \\export fn entry() {
2675 \\ const a = Payload { .A = { 1234 } };
2676 \\ foo(a);
2677 \\}
2678 \\fn foo(a: &const Payload) {
2679 \\ switch (*a) {
2680 \\ Payload.A => {},
2681 \\ else => unreachable,
2682 \\ }
2683 \\}
2684 ,
2685 ".tmp_source.zig:11:13: error: switch on union which has no attached enum",
2686 ".tmp_source.zig:1:17: note: consider 'union(enum)' here");
25742687}