mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
analyze: error for infinite size struct
This commit is contained in:
parent
431170d981
commit
fe3ad27d5f
@ -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) {
|
static void resolve_struct_type(CodeGen *g, ImportTableEntry *import, TypeTableEntry *struct_type) {
|
||||||
assert(struct_type->id == TypeTableEntryIdStruct);
|
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) {
|
if (struct_type->data.structure.fields) {
|
||||||
// we already resolved this type. skip
|
// we already resolved this type. skip
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
AstNode *decl_node = struct_type->data.structure.decl_node;
|
|
||||||
|
|
||||||
assert(struct_type->di_type);
|
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 first_field_align_in_bits = 0;
|
||||||
uint64_t offset_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) {
|
for (int i = 0; i < field_count; i += 1) {
|
||||||
AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
|
AstNode *field_node = decl_node->data.struct_decl.fields.at(i);
|
||||||
TypeStructField *type_struct_field = &struct_type->data.structure.fields[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;
|
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);
|
LLVMStructSetBody(struct_type->type_ref, element_types, field_count, false);
|
||||||
|
|
||||||
|
|||||||
@ -43,6 +43,9 @@ struct TypeTableEntryStruct {
|
|||||||
bool is_packed;
|
bool is_packed;
|
||||||
int field_count;
|
int field_count;
|
||||||
TypeStructField *fields;
|
TypeStructField *fields;
|
||||||
|
|
||||||
|
// set this flag temporarily to detect infinite loops
|
||||||
|
bool embedded_in_current;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TypeTableEntryNumLit {
|
struct TypeTableEntryNumLit {
|
||||||
|
|||||||
@ -828,6 +828,16 @@ fn f() {
|
|||||||
}
|
}
|
||||||
)SOURCE", 2, ".tmp_source.zig:3:21: error: expected type 'i32', got 'void'",
|
)SOURCE", 2, ".tmp_source.zig:3:21: error: expected type 'i32', got 'void'",
|
||||||
".tmp_source.zig:4:15: error: incompatible types: 'i32' and '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) {
|
static void print_compiler_invocation(TestCase *test_case) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user