authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-14 13:07:51-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-14 13:07:51-05:00
log52c03de5c2495b369ae730ff203e5342e4f33a36
tree6e9431418a20422112836190eaabd42eacd5c84d
parente03c770145b5dc7b428d53b3cac97c2733fb84d8
signature Commit is signed but in an unrecognized format.

add missing compile error for OpaqueType inside structs/unions

closes #1862

2 files changed, 35 insertions(+), 0 deletions(-)

src/analyze.cpp+14
...@@ -2679,6 +2679,13 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {...@@ -2679,6 +2679,13 @@ static Error resolve_struct_zero_bits(CodeGen *g, ZigType *struct_type) {
2679 buf_sprintf("enums, not structs, support field assignment"));2679 buf_sprintf("enums, not structs, support field assignment"));
2680 }2680 }
26812681
2682 if (field_type->id == ZigTypeIdOpaque) {
2683 add_node_error(g, field_node->data.struct_field.type,
2684 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in structs"));
2685 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2686 continue;
2687 }
2688
2682 switch (type_requires_comptime(g, field_type)) {2689 switch (type_requires_comptime(g, field_type)) {
2683 case ReqCompTimeYes:2690 case ReqCompTimeYes:
2684 struct_type->data.structure.requires_comptime = true;2691 struct_type->data.structure.requires_comptime = true;
...@@ -2963,6 +2970,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {...@@ -2963,6 +2970,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
2963 }2970 }
2964 union_field->type_entry = field_type;2971 union_field->type_entry = field_type;
29652972
2973 if (field_type->id == ZigTypeIdOpaque) {
2974 add_node_error(g, field_node->data.struct_field.type,
2975 buf_sprintf("opaque types have unknown size and therefore cannot be directly embedded in unions"));
2976 union_type->data.unionation.is_invalid = true;
2977 continue;
2978 }
2979
2966 switch (type_requires_comptime(g, field_type)) {2980 switch (type_requires_comptime(g, field_type)) {
2967 case ReqCompTimeInvalid:2981 case ReqCompTimeInvalid:
2968 union_type->data.unionation.is_invalid = true;2982 union_type->data.unionation.is_invalid = true;
test/compile_errors.zig+21
...@@ -1,6 +1,27 @@...@@ -1,6 +1,27 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.addTest(
5 "directly embedding opaque type in struct and union",
6 \\const O = @OpaqueType();
7 \\const Foo = struct {
8 \\ o: O,
9 \\};
10 \\const Bar = union {
11 \\ One: i32,
12 \\ Two: O,
13 \\};
14 \\export fn a() void {
15 \\ var foo: Foo = undefined;
16 \\}
17 \\export fn b() void {
18 \\ var bar: Bar = undefined;
19 \\}
20 ,
21 ".tmp_source.zig:3:8: error: opaque types have unknown size and therefore cannot be directly embedded in structs",
22 ".tmp_source.zig:7:10: error: opaque types have unknown size and therefore cannot be directly embedded in unions",
23 );
24
4 cases.addTest(25 cases.addTest(
5 "implicit cast between C pointer and Zig pointer - bad const/align/child",26 "implicit cast between C pointer and Zig pointer - bad const/align/child",
6 \\export fn a() void {27 \\export fn a() void {