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

stage2: panic from packedStructToInt #12468

Closed
scheibo opened this issue Aug 17, 2022 · 2 comments
Closed

stage2: panic from packedStructToInt #12468

scheibo opened this issue Aug 17, 2022 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@scheibo
Copy link
Contributor

scheibo commented Aug 17, 2022

Zig Version

0.10.0-dev.3590+a12abc6d6

Steps to Reproduce

const std = @import("std");

pub const Foo = packed struct {
    type: Foo.Type = .A,
    _: u2 = 0,
    data: u4 = 0,

    pub const Type = enum(u2) {
        A,
        B,
        C,
    };

    comptime {
        std.debug.assert(@sizeOf(Foo) == 1);
    }
};

test "Foo" {
    const foo: Foo = .{ .type = .B, .data = 4 };
    try std.testing.expectEqual(0b0100_0001, @bitCast(u8, foo));
}
zig test -fno-stage1 main.zig

Expected Behavior

$ zig test -fno-stage1 main.zig 
All 1 tests passed.

I expect this code to either pass like on stage 1 or to provide an error message indicating what should be changed.

Actual Behavior

With -fno-stage1 the self hosted compiler panics:

$ zig test -fno-stage1 main.zig 
thread 11257471 panic: reached unreachable code
/Users/kjs/Code/src/github.com/scheibo/zig/src/value.zig:1313:25: 0x10654b3ef in value.Value.packedStructToInt (zig1)
                else => unreachable,
                        ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/value.zig:1274:55: 0x1065480ea in value.Value.writeToMemory (zig1)
                    const host_int = packedStructToInt(val, ty, target, &buf);
                                                      ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:24894:22: 0x1065a5289 in Sema.bitCastVal (zig1)
    val.writeToMemory(old_ty, sema.mod, buffer);
                     ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:24828:47: 0x1064d887e in Sema.bitCast (zig1)
        const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty, 0);
                                              ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:8526:24: 0x1062776ce in Sema.zirBitcast (zig1)
    return sema.bitCast(block, dest_ty, operand, operand_src);
                       ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:713:65: 0x1060f7df4 in Sema.analyzeBodyInner (zig1)
            .bitcast                      => try sema.zirBitcast(block, inst),
                                                                ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:621:45: 0x1060f31fd in Sema.analyzeBodyBreak (zig1)
    const break_inst = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                                            ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:586:50: 0x1064d8ae1 in Sema.resolveBody (zig1)
    const break_data = (try sema.analyzeBodyBreak(block, body)) orelse
                                                 ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:5563:56: 0x10627ace8 in Sema.zirCall (zig1)
        resolved_args[arg_index] = try sema.resolveBody(block, args_body[arg_start..arg_end], inst);
                                                       ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:719:62: 0x1060f82cb in Sema.analyzeBodyInner (zig1)
            .call                         => try sema.zirCall(block, inst),
                                                             ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Sema.zig:604:30: 0x1060ec5cb in Sema.analyzeBody (zig1)
    _ = sema.analyzeBodyInner(block, body) catch |err| switch (err) {
                             ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Module.zig:5501:21: 0x105f1e16e in Module.analyzeFnBody (zig1)
    sema.analyzeBody(&inner_block, fn_info.body) catch |err| switch (err) {
                    ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Module.zig:4212:40: 0x105efeb04 in Module.ensureFuncBodyAnalyzed (zig1)
            var air = mod.analyzeFnBody(func, sema_arena) catch |err| switch (err) {
                                       ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Compilation.zig:2966:42: 0x105c546bb in Compilation.processOneJob (zig1)
            module.ensureFuncBodyAnalyzed(func) catch |err| switch (err) {
                                         ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Compilation.zig:2898:30: 0x105c43f6e in Compilation.performAllTheWork (zig1)
            try processOneJob(comp, work_item);
                             ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/Compilation.zig:2238:31: 0x105c3c766 in Compilation.update (zig1)
    try comp.performAllTheWork(main_progress_node);
                              ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/main.zig:3327:20: 0x105bd4790 in main.updateModule (zig1)
    try comp.update();
                   ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/main.zig:3016:17: 0x105b2a8e7 in main.buildOutputType (zig1)
    updateModule(gpa, comp, hook) catch |err| switch (err) {
                ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/main.zig:236:31: 0x105ad65cd in main.mainArgs (zig1)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/Users/kjs/Code/src/github.com/scheibo/zig/src/stage1.zig:48:24: 0x105ad5d5a in main (zig1)
        stage2.mainArgs(gpa, arena, args) catch unreachable;
                       ^
???:?:?: 0x117cba52d in ??? (???)
???:?:?: 0x0 in ??? (???)
Abort trap: 6
@scheibo scheibo added the bug Observed behavior contradicts documented or intended behavior label Aug 17, 2022
@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Aug 17, 2022
@Vexu Vexu added this to the 0.10.0 milestone Aug 17, 2022
@ThreeFx
Copy link
Contributor

ThreeFx commented Aug 20, 2022

The same problem occurs in intToPackedStruct in the other conversion direction.

@scheibo
Copy link
Contributor Author

scheibo commented Oct 29, 2022

I have verified that this bug no longer reproduces in stage 2 as of 0.10.0-dev.4640+20925b2f5 (I think maybe thanks to #13221?) Thanks!

@scheibo scheibo closed this as completed Oct 29, 2022
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 frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants