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 Failed Assert Duing @bitCast of Packed Struct Literal #3042

Closed
iguessthislldo opened this issue Aug 10, 2019 · 4 comments
Closed

Compiler Failed Assert Duing @bitCast of Packed Struct Literal #3042

iguessthislldo opened this issue Aug 10, 2019 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@iguessthislldo
Copy link

Using master the following works:

const warn = @import("std").debug.warn;

pub fn main() anyerror!void {
    const Foo = packed struct {
        value: u8,
    };
    const foo_value = Foo{.value = 0xF};
    warn("{x}\n", @bitCast(u8, foo_value));
}

But if I try to use the struct literal directly in the @bitCast the compiler trips on an assert.

    warn("{x}\n", @bitCast(u8, Foo{.value = 0xF}));

This is similar to #3010, but when I built zig with #3022 it didn't fix the problem.

Here is the back trace right before it evaluates the assert.

#0  get_abi_alignment (g=0x555558387ad0, type_entry=0x55555e803b60)
    at /home/fred/src/zig/src/analyze.cpp:332
#1  0x0000555555e60bcd in get_pointer_to_type_extra (g=0x555558387ad0, child_type=0x55555e803b60, 
    is_const=false, is_volatile=false, ptr_len=PtrLenSingle, byte_alignment=1, 
    bit_offset_in_host=0, host_int_bytes=0, allow_zero=false)
    at /home/fred/src/zig/src/analyze.cpp:390
#2  0x0000555555df7dbe in ir_resolve_result_raw (ira=0x55555e805f60, 
    suspend_source_instr=0x55555e804f60, result_loc=0x55555e804b10, value_type=0x55555e803b60, 
    value=0x0, force_runtime=false, non_null_comptime=true) at /home/fred/src/zig/src/ir.cpp:15475
#3  0x0000555555df7f16 in ir_resolve_result (ira=0x55555e805f60, 
    suspend_source_instr=0x55555e804f60, result_loc_pass1=0x55555e804b10, 
    value_type=0x55555e803b60, value=0x0, force_runtime=false, non_null_comptime=true, 
    allow_discard=true) at /home/fred/src/zig/src/ir.cpp:15498
#4  0x0000555555df8245 in ir_analyze_instruction_resolve_result (ira=0x55555e805f60, 
    instruction=0x55555e804f60) at /home/fred/src/zig/src/ir.cpp:15551
#5  0x0000555555e1e00c in ir_analyze_instruction_base (ira=0x55555e805f60, 
    instruction=0x55555e804f60) at /home/fred/src/zig/src/ir.cpp:25714
#6  0x0000555555e1e726 in ir_analyze (codegen=0x555558387ad0, old_exec=0x55555e7dc1b8, 
    new_exec=0x55555e7dc2a8, expected_type=0x5555590b8240, expected_type_source_node=0x5555583d8460)
    at /home/fred/src/zig/src/ir.cpp:25855
#7  0x0000555555e6b6a7 in analyze_fn_ir (g=0x555558387ad0, fn_table_entry=0x55555e7dc150, 
    return_type_node=0x5555583d8460) at /home/fred/src/zig/src/analyze.cpp:3789
#8  0x0000555555e6bafe in analyze_fn_body (g=0x555558387ad0, fn_table_entry=0x55555e7dc150)
    at /home/fred/src/zig/src/analyze.cpp:3876
#9  0x0000555555e6c453 in semantic_analyze (g=0x555558387ad0)
    at /home/fred/src/zig/src/analyze.cpp:4008
#10 0x0000555555db5287 in gen_root_source (g=0x555558387ad0)
    at /home/fred/src/zig/src/codegen.cpp:8629
#11 0x0000555555db8bca in codegen_build_and_link (g=0x555558387ad0)
    at /home/fred/src/zig/src/codegen.cpp:9566
#12 0x0000555555d8c14d in main (argc=9, argv=0x7fffffffd688) at /home/fred/src/zig/src/main.cpp:1207

I tried to debug it myself but I couldn't get farther than figuring out that the type_entry->data->structure.resolve_status of Foo is ResolveStatusUnstarted and guessed that creating a named Foo value first would make it work:

    const foo_value = Foo{.value = 0xA};
    warn("{x}\n", @bitCast(u8, foo_value));
    warn("{x}\n", @bitCast(u8, Foo{.value = 0xF}));

This does compile, but the result isn't what I expected:

a
Foo{ .value = 15 }

Shouldn't the second one be a u8 as well? Is there something I'm missing here or is this a side effect of the bug above?

@daurnimator daurnimator added the bug Observed behavior contradicts documented or intended behavior label Aug 11, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Aug 11, 2019
@SamTebbs33
Copy link
Contributor

I tried to reproduce this and #3022 does fix this issue, could you try again @iguessthislldo ?

@iguessthislldo
Copy link
Author

could you try again @iguessthislldo ?

I tried again just now, actually fetched #3022's branch instead of just adding the ! and made sure I was using the right zig before building the my example. However it acts the same as master for me.

Just to make sure it's clear what code I'm talking about since I didn't list the whole thing above:

const warn = @import("std").debug.warn;

pub fn main() anyerror!void {
    const Foo = packed struct {
        value: u8,
    };
    warn("{x}\n", @bitCast(u8, Foo{.value = 0xF}));
}

@SamTebbs33
Copy link
Contributor

Ok, that's really weird. All I did when trying to reproduce it was replace the warn line with what you gave later on, without removing the foo_value declaration, and that worked:

const warn = @import("std").debug.warn;

pub fn main() anyerror!void {
    const Foo = packed struct {
        value: u8,
    };
    const foo_value = Foo{.value = 0xF};
    warn("{x}\n", @bitCast(u8, Foo{.value = 0xF}));
}

However using your latest example with the foo_value declaration, the assertion fails:

const warn = @import("std").debug.warn;

pub fn main() anyerror!void {
    const Foo = packed struct {
        value: u8,
    };
    warn("{x}\n", @bitCast(u8, Foo{.value = 0xF}));
}

@andrewrk
Copy link
Member

The assertion is fixed but I noticed and filed #3099

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
Projects
None yet
Development

No branches or pull requests

4 participants