Skip to content

Bug with non-trivial usage of unnamed struct, optional types and switch over tagged union #10514

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

Closed
sivukhin opened this issue Jan 5, 2022 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@sivukhin
Copy link

sivukhin commented Jan 5, 2022

Zig Version

0.9.0-dev.1815+20e19e75f

Steps to Reproduce

Try to compile following code (godbolt link: https://godbolt.org/z/8MqKo6P9W):

const std = @import("std");
const Two = struct { a: ?i32, b: i32 };
const Complex = union(enum) { two: Two };

fn get() Complex {
    return Complex{ .two = Two{ .a = null, .b = 1 } };
}

pub fn main() void {
    const x = get();
    switch (x) {
        Complex.two => |two| std.debug.print("{}\n", .{two.a orelse two.b}),
    }
}

Expected Behavior

Code is perfectly valid and should output string 1

Actual Behavior

Zig compiler crashes with the following error:

broken LLVM module found: Instruction does not dominate all uses!
  %9 = getelementptr inbounds %"struct:12:55", %"struct:12:55"* %0, i32 0, i32 0, !dbg !16575
  store i32 %11, i32* %9, align 4, !dbg !16574

This is a bug in the Zig compiler.thread 11096 panic: 
Unable to dump stack trace: debug info stripped
@sivukhin sivukhin added the bug Observed behavior contradicts documented or intended behavior label Jan 5, 2022
@iacore
Copy link
Contributor

iacore commented Jan 7, 2022

Zig version 0.10.0-dev.40+303bad998, can reproduce.

When I changed the type of b to ?i32, it broke more.

const std = @import("std");
const Two = struct { a: ?i32, b: ?i32 };
const Complex = union(enum) { two: Two };

fn get() Complex {
    return Complex{ .two = Two{ .a = null, .b = 1 } };
}

pub fn main() void {
    const x = get();
    switch (x) {
        .two => |two| std.debug.print("{}\n", .{two.a orelse two.b}),
    }
}
broken LLVM module found: Instruction does not dominate all uses!
  %9 = getelementptr inbounds %"struct:12:48", %"struct:12:48"* %0, i32 0, i32 0, !dbg !17078
  %14 = getelementptr inbounds %"?i32", %"?i32"* %9, i32 0, i32 1, !dbg !17077
Instruction does not dominate all uses!
  %9 = getelementptr inbounds %"struct:12:48", %"struct:12:48"* %0, i32 0, i32 0, !dbg !17078
  %15 = getelementptr inbounds %"?i32", %"?i32"* %9, i32 0, i32 0, !dbg !17077

This is a bug in the Zig compiler.thread 5194 panic: 
Unable to dump stack trace: debug info stripped

@cryptocode
Copy link
Contributor

Quick note that this can be further reduced to:

const std = @import("std");

const Wrapper = struct { val: ?u8};
pub fn get() Wrapper {
    return .{ .val = null };
}

pub fn main() u8 {
    const x = get();
    var b = @as(u8,1);
    return .{x.val orelse b}[0];
}

Introducing a temporary for the array item "solves" the issue, which may give some indication as to where the culprit is.

var tmp = x.val orelse b;
return .{tmp}[0];

@Vexu Vexu added the stage1 The process of building from source via WebAssembly and the C backend. label Jan 9, 2022
@Vexu Vexu modified the milestones: 0.10.0, 0.11.0 Jan 9, 2022
@Vexu
Copy link
Member

Vexu commented Jan 16, 2023

Duplicate of #3882

@Vexu Vexu marked this as a duplicate of #3882 Jan 16, 2023
@Vexu Vexu closed this as completed Jan 16, 2023
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Jan 16, 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

5 participants