| author | |
| committer | |
| log | 52c03de5c2495b369ae730ff203e5342e4f33a36 |
| tree | 6e9431418a20422112836190eaabd42eacd5c84d |
| parent | e03c770145b5dc7b428d53b3cac97c2733fb84d8 |
| signature |
closes #18622 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 | 2679 | buf_sprintf("enums, not structs, support field assignment")); |
| 2680 | 2680 | } |
| 2681 | 2681 | |
| 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 | 2689 | switch (type_requires_comptime(g, field_type)) { |
| 2683 | 2690 | case ReqCompTimeYes: |
| 2684 | 2691 | struct_type->data.structure.requires_comptime = true; |
| ... | ... | @@ -2963,6 +2970,13 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) { |
| 2963 | 2970 | } |
| 2964 | 2971 | union_field->type_entry = field_type; |
| 2965 | 2972 | |
| 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 | 2980 | switch (type_requires_comptime(g, field_type)) { |
| 2967 | 2981 | case ReqCompTimeInvalid: |
| 2968 | 2982 | union_type->data.unionation.is_invalid = true; |
test/compile_errors.zig+21| ... | ... | @@ -1,6 +1,27 @@ |
| 1 | 1 | const tests = @import("tests.zig"); |
| 2 | 2 | |
| 3 | 3 | pub 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 | 25 | cases.addTest( |
| 5 | 26 | "implicit cast between C pointer and Zig pointer - bad const/align/child", |
| 6 | 27 | \\export fn a() void { |