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 segfault for unqualified enum value in some contexts. #4675

Closed
MortimerSnerd opened this issue Mar 7, 2020 · 2 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@MortimerSnerd
Copy link

Using commit abe7305, saw it also on commit from mid Feb.

Happens compiling the following code with zig build-exe:

const std = @import("std");

pub const FrameResponse = enum {
    Running,
    Finished,
    CreateCancelled,
};

const Something = enum {
    AA,
    BB,
};

pub fn handleFrame(dT: f32, st: Something) !FrameResponse {
    if (dT == 0) return error.BadTime;
    const rv = switch (st) {
        .AA => FrameResponse.Running,
        .BB => blk: {
            if (true) break :blk .Running;  // NOTE: Qualifying the enum here makes the compile succeed.
            break :blk .Finished;
        },
    };

    return rv;
}

pub fn main() !void {
    _ = try handleFrame(1, .AA);
}

Qualifying line .Running on line #19 makes the compile succeed. Small changes like removing the error union from the handleFrame function, or removing the enum type qualification from line #17 makes the compile not crash, giving a compile error instead: error: cannot store runtime value in type '(enum literal)'

The stack from the compile:

Segmentation fault at address 0x8
/home/pkelley/src/zig/src/buffer.cpp:68:5: 0x857300 in buf_hash (/home/pkelley/src/zig/src/buffer.cpp)
    assert(buf->list.length);
    ^
/home/pkelley/src/zig/src/hash_map.hpp:238:22: 0x8470e3 in key_to_index (/home/pkelley/src/zig/src/analyze.cpp)
        return (int)(HashFunction(key) % ((uint32_t)_capacity));
                     ^
/home/pkelley/src/zig/src/hash_map.hpp:223:27: 0x846f13 in internal_get (/home/pkelley/src/zig/src/analyze.cpp)
        int start_index = key_to_index(key);
                          ^
/home/pkelley/src/zig/src/hash_map.hpp:81:16: 0x84510c in maybe_get (/home/pkelley/src/zig/src/analyze.cpp)
        return internal_get(key);
               ^
/home/pkelley/src/zig/src/analyze.cpp:4299:61: 0x82b520 in find_enum_type_field (/home/pkelley/src/zig/src/analyze.cpp)
    auto entry = enum_type->data.enumeration.fields_by_name.maybe_get(name);
                                                            ^
/home/pkelley/src/zig/src/ir.cpp:12076:36: 0x899786 in ir_resolve_peer_types (/home/pkelley/src/zig/src/ir.cpp)
            TypeEnumField *field = find_enum_type_field(prev_type, cur_inst->value->data.x_enum_literal);
                                   ^
/home/pkelley/src/zig/src/ir.cpp:20218:42: 0x8c7bd0 in ir_analyze_instruction_phi (/home/pkelley/src/zig/src/ir.cpp)
            peer_parent->resolved_type = ir_resolve_peer_types(ira,
                                         ^
/home/pkelley/src/zig/src/ir.cpp:29270:20: 0x89753d in ir_analyze_instruction_base (/home/pkelley/src/zig/src/ir.cpp)
            return ir_analyze_instruction_phi(ira, (IrInstSrcPhi *)instruction);
                   ^
/home/pkelley/src/zig/src/ir.cpp:29572:38: 0x896846 in ir_analyze (/home/pkelley/src/zig/src/ir.cpp)
        IrInstGen *new_instruction = ir_analyze_instruction_base(ira, old_instruction);
                                     ^
/home/pkelley/src/zig/src/analyze.cpp:4810:34: 0x83ca9a in analyze_fn_ir (/home/pkelley/src/zig/src/analyze.cpp)
    ZigType *block_return_type = ir_analyze(g, fn->ir_executable,
                                 ^
/home/pkelley/src/zig/src/analyze.cpp:4926:5: 0x82c5eb in analyze_fn_body (/home/pkelley/src/zig/src/analyze.cpp)
    analyze_fn_ir(g, fn_table_entry, return_type_node);
    ^
/home/pkelley/src/zig/src/analyze.cpp:4578:13: 0x82c2a9 in resolve_inferred_error_set (/home/pkelley/src/zig/src/analyze.cpp)
            analyze_fn_body(g, infer_fn);
            ^
/home/pkelley/src/zig/src/ir.cpp:26740:18: 0x8d3e67 in ir_analyze_instruction_test_err (/home/pkelley/src/zig/src/ir.cpp)
            if (!resolve_inferred_error_set(ira->codegen, err_set_type, instruction->base.base.source_node)) {
                 ^
/home/pkelley/src/zig/src/ir.cpp:29386:20: 0x897ae7 in ir_analyze_instruction_base (/home/pkelley/src/zig/src/ir.cpp)
            return ir_analyze_instruction_test_err(ira, (IrInstSrcTestErr *)instruction);
                   ^
/home/pkelley/src/zig/src/ir.cpp:29572:38: 0x896846 in ir_analyze (/home/pkelley/src/zig/src/ir.cpp)
        IrInstGen *new_instruction = ir_analyze_instruction_base(ira, old_instruction);
                                     ^
/home/pkelley/src/zig/src/analyze.cpp:4810:34: 0x83ca9a in analyze_fn_ir (/home/pkelley/src/zig/src/analyze.cpp)
    ZigType *block_return_type = ir_analyze(g, fn->ir_executable,
                                 ^
/home/pkelley/src/zig/src/analyze.cpp:4926:5: 0x82c5eb in analyze_fn_body (/home/pkelley/src/zig/src/analyze.cpp)
    analyze_fn_ir(g, fn_table_entry, return_type_node);
    ^
/home/pkelley/src/zig/src/analyze.cpp:4578:13: 0x82c2a9 in resolve_inferred_error_set (/home/pkelley/src/zig/src/analyze.cpp)
            analyze_fn_body(g, infer_fn);
            ^
/home/pkelley/src/zig/src/ir.cpp:26740:18: 0x8d3e67 in ir_analyze_instruction_test_err (/home/pkelley/src/zig/src/ir.cpp)
            if (!resolve_inferred_error_set(ira->codegen, err_set_type, instruction->base.base.source_node)) {
                 ^
/home/pkelley/src/zig/src/ir.cpp:29386:20: 0x897ae7 in ir_analyze_instruction_base (/home/pkelley/src/zig/src/ir.cpp)
            return ir_analyze_instruction_test_err(ira, (IrInstSrcTestErr *)instruction);
                   ^
/home/pkelley/src/zig/src/ir.cpp:29572:38: 0x896846 in ir_analyze (/home/pkelley/src/zig/src/ir.cpp)
        IrInstGen *new_instruction = ir_analyze_instruction_base(ira, old_instruction);
                                     ^
/home/pkelley/src/zig/src/analyze.cpp:4810:34: 0x83ca9a in analyze_fn_ir (/home/pkelley/src/zig/src/analyze.cpp)
    ZigType *block_return_type = ir_analyze(g, fn->ir_executable,
                                 ^
/home/pkelley/src/zig/src/analyze.cpp:4926:5: 0x82c5eb in analyze_fn_body (/home/pkelley/src/zig/src/analyze.cpp)
    analyze_fn_ir(g, fn_table_entry, return_type_node);
    ^
/home/pkelley/src/zig/src/analyze.cpp:5038:13: 0x82d590 in semantic_analyze (/home/pkelley/src/zig/src/analyze.cpp)
            analyze_fn_body(g, fn_entry);
            ^
/home/pkelley/src/zig/src/codegen.cpp:9441:9: 0x861b73 in gen_root_source (/home/pkelley/src/zig/src/codegen.cpp)
        semantic_analyze(g);
        ^
/home/pkelley/src/zig/src/codegen.cpp:10504:13: 0x860473 in codegen_build_and_link (/home/pkelley/src/zig/src/codegen.cpp)
            gen_root_source(g);
            ^
/home/pkelley/src/zig/src/main.cpp:1256:17: 0x81e32b in main0 (/home/pkelley/src/zig/src/main.cpp)
                codegen_build_and_link(g);
                ^
/home/pkelley/src/zig/src/main.cpp:1406:19: 0x8193c3 in main (/home/pkelley/src/zig/src/main.cpp)
    auto result = main0(argc, argv);
                  ^
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Mar 8, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Mar 8, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@nektro
Copy link
Contributor

nektro commented Oct 12, 2021

and changing the switch to this trips an ICE

    return switch (st) {
        .AA => .Running,
        .BB => blk: {
            if (true) break :blk .Running;
            break :blk .Finished;
        },
    };

@Vexu
Copy link
Member

Vexu commented Dec 31, 2022

Works in self-hosted, similar enough tests exits in test/behavior/eval.zig.

@Vexu Vexu closed this as completed Dec 31, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Jan 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

4 participants