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

Segmentation fault on accessing fields of tagged unions #18518

Closed
timfayz opened this issue Jan 11, 2024 · 1 comment · Fixed by #19182
Closed

Segmentation fault on accessing fields of tagged unions #18518

timfayz opened this issue Jan 11, 2024 · 1 comment · Fixed by #19182
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@timfayz
Copy link

timfayz commented Jan 11, 2024

Zig Version

0.12.0-dev.2127+fcc0c5ddc

Steps to Reproduce and Observed Behavior

const std = @import("std");

const Union = union(enum) {
    Foo: u8,
    Bar: f64,
};

pub fn main() !void {
    std.log.debug("{any}", .{Union.Foo});
}

Gives:

zig run bugAccessTaggedUnion.zig 
LLVM Emit Object... zsh: segmentation fault  zig run bugAccessTaggedUnion.zig

System: MacOS Monterey, 12.0.1, Apple M1

Expected Behavior

Using @compilerLog(Union.Foo) instead, gives:

@as(@typeInfo(bugAccessTaggedUnion.Union).Union.tag_type.?, .Foo)
@timfayz timfayz added the bug Observed behavior contradicts documented or intended behavior label Jan 11, 2024
@Vexu Vexu added the backend-llvm The LLVM backend outputs an LLVM IR Module. label Jan 11, 2024
@Vexu Vexu added this to the 0.13.0 milestone Jan 11, 2024
@Vexu
Copy link
Member

Vexu commented Jan 11, 2024

-fstrip can be used as a workaround.

/home/vexu/Documents/zig/zig/lib/std/debug.zig:342:14: 0x9d8c71c in assert (zig)
    if (!ok) unreachable; // assertion failure
             ^
/home/vexu/Documents/zig/zig/src/Module.zig:6191:11: 0xa258cdd in getUnionLayout (zig)
    assert(u.haveLayout(ip));
          ^
/home/vexu/Documents/zig/zig/src/codegen/llvm.zig:2653:50: 0x9f00521 in lowerDebugTypeImpl (zig)
                const layout = mod.getUnionLayout(union_obj);
                                                 ^
/home/vexu/Documents/zig/zig/src/codegen/llvm.zig:1210:48: 0x9ef7171 in emit (zig)
                _ = try self.lowerDebugTypeImpl(entry, .full, annotated.toDIType());
                                               ^
/home/vexu/Documents/zig/zig/src/Compilation.zig:2660:25: 0x9f03ac3 in emitLlvmObject (zig)
    try llvm_object.emit(.{
                        ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
2 participants