Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler crashes when using a struct T which contains a field of type ArrayList(T) #1832

Closed
bdoner opened this Issue Dec 15, 2018 · 8 comments

Comments

Projects
None yet
6 participants
@bdoner
Copy link

bdoner commented Dec 15, 2018

When running the code below zig fails without printing any information as to what went wrong (or any other information for that matter). No files are being created either.
I'm expecting the compiler to tell me if I do something wrong.

Zig version:

0.3.0+bbdc1289

Platform:

Windows 10 Pro N

Return Code:

-1073741819

Code:

const std = @import("std");

pub fn main() void {
    _ = Node {
        .subList = undefined
    };
}

const Node = struct {
    subList: std.ArrayList(Node)
};
@jayschwa

This comment has been minimized.

Copy link
Contributor

jayschwa commented Dec 15, 2018

I can reproduce with 0.3.0+e98ba5fc.

@andrewrk andrewrk added the bug label Dec 16, 2018

@andrewrk andrewrk added this to the 0.4.0 milestone Dec 16, 2018

@binary132

This comment has been minimized.

Copy link

binary132 commented Dec 16, 2018

I get:

zig: ../src/analyze.cpp:2695: Error resolve_struct_alignment(CodeGen*, ZigType*): Assertion `field->type_entry != nullptr' failed.
[1]    20068 abort (core dumped)  zig build-exe main.zig

on 0.3.0+82bf1eb7 built from source.

@bdoner

This comment has been minimized.

Copy link
Author

bdoner commented Dec 16, 2018

I get nio output using the pre-built binaries on windows. Different platforms might produce different output. But that do seem like valuable information!

@Hejsil

This comment has been minimized.

Copy link
Member

Hejsil commented Dec 17, 2018

Smaller test case:

test "" {
    var n: Node = undefined;
}

const Node = struct {
    nodes: []align(@alignOf(Node)) Node,
};
@kristate

This comment has been minimized.

Copy link
Contributor

kristate commented Dec 19, 2018

Funny enough, i am running into this too -- but I am wondering why the standard library is not failing given some of the JSON code relies on this.

Assertion failed: (field->type_entry != nullptr), function resolve_struct_alignment
@kristate

This comment has been minimized.

Copy link
Contributor

kristate commented Dec 19, 2018

callstack:

  * frame #4: 0x000000010001a2df zig`resolve_struct_alignment(g=0x000000010700b800, struct_type=0x0000000108a185b0) at analyze.cpp:2717
    frame #5: 0x0000000100009a80 zig`type_resolve(g=0x000000010700b800, ty=0x0000000108a185b0, status=ResolveStatusAlignmentKnown) at analyze.cpp:5550
    frame #6: 0x0000000100019203 zig`resolve_union_zero_bits(g=0x000000010700b800, union_type=0x00000001067535a0) at analyze.cpp:2917
    frame #7: 0x0000000100009a48 zig`type_resolve(g=0x000000010700b800, ty=0x00000001067535a0, status=ResolveStatusZeroBitsKnown) at analyze.cpp:5545
    frame #8: 0x0000000100015f82 zig`type_requires_comptime(g=0x000000010700b800, type_entry=0x00000001067535a0) at analyze.cpp:5169
    frame #9: 0x0000000100017a70 zig`resolve_struct_zero_bits(g=0x000000010700b800, struct_type=0x0000000108a29560) at analyze.cpp:2649
    frame #10: 0x0000000100009a04 zig`type_resolve(g=0x000000010700b800, ty=0x0000000108a29560, status=ResolveStatusZeroBitsKnown) at analyze.cpp:5541
    frame #11: 0x0000000100015f82 zig`type_requires_comptime(g=0x000000010700b800, type_entry=0x0000000108a29560) at analyze.cpp:5169
    frame #12: 0x0000000100017a70 zig`resolve_struct_zero_bits(g=0x000000010700b800, struct_type=0x0000000108a26f30) at analyze.cpp:2649
    frame #13: 0x0000000100009a04 zig`type_resolve(g=0x000000010700b800, ty=0x0000000108a26f30, status=ResolveStatusZeroBitsKnown) at analyze.cpp:5541
    frame #14: 0x000000010009333b zig`ir_analyze_instruction_slice_type(ira=0x0000000108a26c60, slice_type_instruction=0x0000000108a26950) at ir.cpp:15500
    frame #15: 0x000000010008b9cc zig`ir_analyze_instruction_nocast(ira=0x0000000108a26c60, instruction=0x0000000108a26950) at ir.cpp:21241
    frame #16: 0x000000010006335d zig`ir_analyze_instruction(ira=0x0000000108a26c60, old_instruction=0x0000000108a26950) at ir.cpp:21467
    frame #17: 0x0000000100062157 zig`ir_analyze(codegen=0x000000010700b800, old_exec=0x0000000108a26690, new_exec=0x0000000108a26b70, expected_type=0x0000000105607980, expected_type_source_node=0x00000001067618b0) at ir.cpp:21518
    frame #18: 0x0000000100061d49 zig`ir_eval_const_value(codegen=0x000000010700b800, scope=0x0000000108a186a0, node=0x00000001067618b0, expected_type=0x0000000105607980, backward_branch_count=0x00007ffeefbfded8, backward_branch_quota=1000, fn_entry=0x0000000000000000, c_import_buf=0x0000000000000000, source_node=0x00000001067618b0, exec_name=0x0000000000000000, parent_exec=0x0000000000000000) at ir.cpp:9760
    frame #19: 0x000000010000b465 zig`analyze_const_value(g=0x000000010700b800, scope=0x0000000108a186a0, node=0x00000001067618b0, type_entry=0x0000000105607980, type_name=0x0000000000000000) at analyze.cpp:1294
    frame #20: 0x000000010000b355 zig`analyze_type_expr(g=0x000000010700b800, scope=0x0000000108a186a0, node=0x00000001067618b0) at analyze.cpp:1300
    frame #21: 0x00000001000179e7 zig`resolve_struct_zero_bits(g=0x000000010700b800, struct_type=0x0000000108a185b0) at analyze.cpp:2639
    frame #22: 0x0000000100009a04 zig`type_resolve(g=0x000000010700b800, ty=0x0000000108a185b0, status=ResolveStatusZeroBitsKnown) at analyze.cpp:5541
    frame #23: 0x0000000100015f82 zig`type_requires_comptime(g=0x000000010700b800, type_entry=0x0000000108a185b0) at analyze.cpp:5169
    frame #24: 0x000000010001ef61 zig`analyze_fn_type(g=0x000000010700b800, proto_node=0x0000000106766ae0, child_scope=0x0000000108a1a5f0, fn_entry=0x0000000108a1a2a0) at analyze.cpp:1732
    frame #25: 0x000000010000e469 zig`resolve_decl_fn(g=0x000000010700b800, tld_fn=0x0000000108a18710) at analyze.cpp:3278
    frame #26: 0x000000010000c2f3 zig`resolve_top_level_decl(g=0x000000010700b800, tld=0x0000000108a18710, pointer_only=false, source_node=0x000000010621f260) at analyze.cpp:3798
    frame #27: 0x00000001000b818b zig`ir_analyze_decl_ref(ira=0x0000000106753180, source_instruction=0x0000000106752750, tld=0x0000000108a18710) at ir.cpp:14698
    frame #28: 0x000000010008fb65 zig`ir_analyze_instruction_field_ptr(ira=0x0000000106753180, field_ptr_instruction=0x0000000106752750) at ir.cpp:14885
    frame #29: 0x000000010008b8a0 zig`ir_analyze_instruction_nocast(ira=0x0000000106753180, instruction=0x0000000106752750) at ir.cpp:21217
    frame #30: 0x000000010006335d zig`ir_analyze_instruction(ira=0x0000000106753180, old_instruction=0x0000000106752750) at ir.cpp:21467
    frame #31: 0x0000000100062157 zig`ir_analyze(codegen=0x000000010700b800, old_exec=0x000000010622adb8, new_exec=0x000000010622aea8, expected_type=0x000000010622b040, expected_type_source_node=0x0000000000000000) at ir.cpp:21518
    frame #32: 0x00000001000126a0 zig`analyze_fn_ir(g=0x000000010700b800, fn_table_entry=0x000000010622ad40, return_type_node=0x0000000000000000) at analyze.cpp:4166
    frame #33: 0x00000001000125f1 zig`analyze_fn_body(g=0x000000010700b800, fn_table_entry=0x000000010622ad40) at analyze.cpp:4259
    frame #34: 0x0000000100013823 zig`semantic_analyze(g=0x000000010700b800) at analyze.cpp:4464
    frame #35: 0x000000010003a479 zig`gen_root_source(g=0x000000010700b800) at codegen.cpp:7657
    frame #36: 0x0000000100039623 zig`codegen_build_and_link(g=0x000000010700b800) at codegen.cpp:8358
    frame #37: 0x00000001000d3c9a zig`main(argc=3, argv=0x00007ffeefbff828) at main.cpp:1029
@kristate

This comment has been minimized.

Copy link
Contributor

kristate commented Dec 19, 2018

Seems like the call to if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) { is trying to resolve alignment, but type_entry doesn't get set until just after this call :-)

analyze.cpp

        ZigType *field_type;
        if (field_node->data.struct_field.type == nullptr) {
            if (decl_node->data.container_decl.auto_enum || decl_node->data.container_decl.init_arg_expr != nullptr) {
                field_type = g->builtin_types.entry_void;
            } else {
                add_node_error(g, field_node, buf_sprintf("union field missing type"));
                union_type->data.unionation.is_invalid = true;
                continue;
            }
        } else {
            field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type);
-->         if ((err = type_resolve(g, field_type, ResolveStatusAlignmentKnown))) {
                union_type->data.unionation.is_invalid = true;
                continue;
            }
        }
-->     union_field->type_entry = field_type;
@kristate

This comment has been minimized.

Copy link
Contributor

kristate commented Dec 20, 2018

Replacing align(@alignOf(Node)) with align(1) @Hejsil 's above triggers another related assert:

test "" {
    var n: Node = undefined;
}

const Node = struct {
    nodes: []align(1) Node,
};

Assertion failed: (type_is_resolved(type_entry, ResolveStatusAlignmentKnown)), function get_abi_alignment

kristate added a commit to kristate/zig that referenced this issue Dec 20, 2018

kristate added a commit to kristate/zig that referenced this issue Dec 20, 2018

mgxm added a commit to mgxm/zig that referenced this issue Dec 24, 2018

mgxm added a commit to mgxm/zig that referenced this issue Dec 24, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.