Commit fe3ad27d5f
Changed files (3)
src/analyze.cpp
@@ -273,12 +273,19 @@ static void preview_function_labels(CodeGen *g, AstNode *node, FnTableEntry *fn_
static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *struct_type) {
assert(struct_type->id == TypeTableEntryIdStruct);
+ AstNode *decl_node = struct_type->data.structure.decl_node;
+
+ if (struct_type->data.structure.embedded_in_current) {
+ add_node_error(g, decl_node,
+ buf_sprintf("struct has infinite size"));
+ return;
+ }
+
if (struct_type->data.structure.fields) {
// we already resolved this type. skip
return;
}
- AstNode *decl_node = struct_type->data.structure.decl_node;
assert(struct_type->di_type);
@@ -293,6 +300,9 @@ static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableE
uint64_t first_field_align_in_bits = 0;
uint64_t offset_in_bits = 0;
+ // this field should be set to true only during the recursive calls to resolve_struct_type
+ struct_type->data.structure.embedded_in_current = true;
+
for (int i = 0; i < field_count; i += 1) {
AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
TypeStructField *type_struct_field = &struct_type->data.structure.fields[i];
@@ -321,6 +331,7 @@ static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableE
offset_in_bits += type_struct_field->type_entry->size_in_bits;
}
+ struct_type->data.structure.embedded_in_current = false;
LLVMStructSetBody(struct_type->type_ref, element_types, field_count, false);
src/analyze.hpp
@@ -43,6 +43,9 @@ struct TypeTableEntryStruct {
bool is_packed;
int field_count;
TypeStructField *fields;
+
+ // set this flag temporarily to detect infinite loops
+ bool embedded_in_current;
};
struct TypeTableEntryNumLit {
test/run_tests.cpp
@@ -828,6 +828,16 @@ fn f() {
}
)SOURCE", 2, ".tmp_source.zig:3:21: error: expected type 'i32', got 'void'",
".tmp_source.zig:4:15: error: incompatible types: 'i32' and 'void'");
+
+ add_compile_fail_case("direct struct loop", R"SOURCE(
+struct A { a : A, }
+ )SOURCE", 1, ".tmp_source.zig:2:1: error: struct has infinite size");
+
+ add_compile_fail_case("indirect struct loop", R"SOURCE(
+struct A { b : B, }
+struct B { c : C, }
+struct C { a : A, }
+ )SOURCE", 1, ".tmp_source.zig:2:1: error: struct has infinite size");
}
static void print_compiler_invocation(TestCase *test_case) {