authorgravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-02-28 00:51:22+01:00
committergravatar for info@bnoordhuis.nlBen Noordhuis <info@bnoordhuis.nl> 2018-02-28 00:56:00+01:00
log90598b4631e3b68565c7d62102a9e4615514a721
tree01a2416c8fe22ee34b07e8e2feb3a32af799ed9a
parent439621e44a68b436f958a84fcdb0bdac83613aea

fix assert on self-referencing function ptr field

The construct `struct S { f: fn(S) void }` is not legal because structs are not copyable but it should not result in an ICE. Fixes #795.

2 files changed, 15 insertions(+), 1 deletions(-)

src/analyze.cpp+3-1
......@@ -1670,6 +1670,9 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
16701670 if (struct_type->data.structure.is_invalid)
16711671 return;
16721672
1673 if (struct_type->data.structure.zero_bits_loop_flag)
1674 return;
1675
16731676 AstNode *decl_node = struct_type->data.structure.decl_node;
16741677
16751678 if (struct_type->data.structure.embedded_in_current) {
......@@ -1682,7 +1685,6 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) {
16821685 return;
16831686 }
16841687
1685 assert(!struct_type->data.structure.zero_bits_loop_flag);
16861688 assert(struct_type->data.structure.fields);
16871689 assert(decl_node->type == NodeTypeContainerDecl);
16881690
test/compile_errors.zig+12
......@@ -3090,4 +3090,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
30903090 ,
30913091 ".tmp_source.zig:11:20: error: runtime cast to union 'Value' which has non-void fields",
30923092 ".tmp_source.zig:3:5: note: field 'A' has type 'i32'");
3093
3094 cases.add("self-referencing function pointer field",
3095 \\const S = struct {
3096 \\ f: fn(_: S) void,
3097 \\};
3098 \\fn f(_: S) void {
3099 \\}
3100 \\export fn entry() void {
3101 \\ var _ = S { .f = f };
3102 \\}
3103 ,
3104 ".tmp_source.zig:4:9: error: type 'S' is not copyable; cannot pass by value");
30933105}