From 90598b4631e3b68565c7d62102a9e4615514a721 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 28 Feb 2018 00:51:22 +0100 Subject: [PATCH 1/2] 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. --- src/analyze.cpp | 4 +++- test/compile_errors.zig | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index c16a5d462a..9d5e7d77af 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1670,6 +1670,9 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { if (struct_type->data.structure.is_invalid) return; + if (struct_type->data.structure.zero_bits_loop_flag) + return; + AstNode *decl_node = struct_type->data.structure.decl_node; if (struct_type->data.structure.embedded_in_current) { @@ -1682,7 +1685,6 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { return; } - assert(!struct_type->data.structure.zero_bits_loop_flag); assert(struct_type->data.structure.fields); assert(decl_node->type == NodeTypeContainerDecl); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 940125711b..a3ac4e2344 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -3090,4 +3090,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) void { , ".tmp_source.zig:11:20: error: runtime cast to union 'Value' which has non-void fields", ".tmp_source.zig:3:5: note: field 'A' has type 'i32'"); + + cases.add("self-referencing function pointer field", + \\const S = struct { + \\ f: fn(_: S) void, + \\}; + \\fn f(_: S) void { + \\} + \\export fn entry() void { + \\ var _ = S { .f = f }; + \\} + , + ".tmp_source.zig:4:9: error: type 'S' is not copyable; cannot pass by value"); } From 556f22a751e47d572404992befe15c09c0f2eb0b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 28 Feb 2018 00:28:26 -0500 Subject: [PATCH 2/2] different way of fixing previous commit get_fn_type doesn't need the complete parameter type, it can just ensure zero bits known. --- src/analyze.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 9d5e7d77af..45e6780791 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -997,7 +997,7 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) { gen_param_info->src_index = i; gen_param_info->gen_index = SIZE_MAX; - ensure_complete_type(g, type_entry); + type_ensure_zero_bits_known(g, type_entry); if (type_has_bits(type_entry)) { TypeTableEntry *gen_type; if (handle_is_ptr(type_entry)) { @@ -1670,9 +1670,6 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { if (struct_type->data.structure.is_invalid) return; - if (struct_type->data.structure.zero_bits_loop_flag) - return; - AstNode *decl_node = struct_type->data.structure.decl_node; if (struct_type->data.structure.embedded_in_current) { @@ -1685,6 +1682,7 @@ static void resolve_struct_type(CodeGen *g, TypeTableEntry *struct_type) { return; } + assert(!struct_type->data.structure.zero_bits_loop_flag); assert(struct_type->data.structure.fields); assert(decl_node->type == NodeTypeContainerDecl); @@ -2131,6 +2129,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { if (enum_type->data.enumeration.zero_bits_loop_flag) { enum_type->data.enumeration.zero_bits_known = true; + enum_type->data.enumeration.zero_bits_loop_flag = false; return; } @@ -2285,6 +2284,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { // the alignment is pointer width, then assert that the first field is within that // alignment struct_type->data.structure.zero_bits_known = true; + struct_type->data.structure.zero_bits_loop_flag = false; if (struct_type->data.structure.abi_alignment == 0) { if (struct_type->data.structure.layout == ContainerLayoutPacked) { struct_type->data.structure.abi_alignment = 1;