Skip to content

fails to compile return of optional in certain conditionals #10601

Closed
@MichaelBelousov

Description

@MichaelBelousov

Zig Version

0.10.0-dev.290+3901b6fb0

Steps to Reproduce

  1. write the following file (NOTE: an edit below contains a smaller reproduction):
// works if I make it return not optional
fn f(use: bool, param: u32) ?u32 {
    var val: u32 = 0;
    return switch (use) {
        true => (               // SwitchProng
            if (param == 1)
                @as(u32, 2)     // Then
            else if (val == 3)  // Else
                    @as(u32, 4) // Then1 ; here is where a return value address is created
                else
                    @as(u32, 5) // Else2 ; here is where the return value address from the other branch is mistakenly consumed
        ),
        else =>                 // SwitchElse
            @as(u32, 6)
    };
}

pub fn main() void {
    _ = f(true, 7);
}
  1. build it, I use:
zig build-exe file.zig

Expected Behavior

for it to compile as valid code

Actual Behavior

the compiler crashes:

broken LLVM module found: Instruction does not dominate all uses!
  %19 = getelementptr inbounds %"?u32", %"?u32"* %0, i32 0, i32 0
  store i32 5, i32* %19, align 4
Instruction does not dominate all uses!
  %19 = getelementptr inbounds %"?u32", %"?u32"* %0, i32 0, i32 0
  store i32 5, i32* %19, align 4
Instruction does not dominate all uses!
  %19 = getelementptr inbounds %"?u32", %"?u32"* %0, i32 0, i32 0
  store i32 %20, i32* %19, align 4

This is a bug in the Zig compiler.thread 2653385 panic: 
???:?:?: 0x55c02598cad7 in ??? (???)
???:?:?: 0x55c026924719 in ??? (???)
???:?:?: 0x55c02694b9e9 in ??? (???)
???:?:?: 0x55c026950b6f in ??? (???)
???:?:?: 0x55c026920273 in ??? (???)
???:?:?: 0x55c025d8fde4 in ??? (???)
???:?:?: 0x55c025d5068a in ??? (???)
???:?:?: 0x55c025b24205 in ??? (???)
???:?:?: 0x55c025b17432 in ??? (???)
???:?:?: 0x55c025b12fd1 in ??? (???)
???:?:?: 0x55c025aa15ff in ??? (???)
???:?:?: 0x55c0259bb486 in ??? (???)
???:?:?: 0x55c02598b097 in ??? (???)
???:?:?: 0x55c02598aaa9 in ??? (???)

EDIT: smaller reproduction:

fn f(param: u32) ?u32 {
    return if (param == 1) @as(u32, 2)
    else if (param == 2) @as(u32, 4)
    else if (param == 3) @as(u32, 5)
    else @as(u32, 6);
}

pub fn main() void { _ = f(7); }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions