0

I'm a fresh new in Zig who test a few codes with "orelse". I tried to create 2 optional unsigned integer variables as following code and use orelse with them.

const std = @import("std");

pub fn main() !void {
    var value1: ?u32 = 123;
    var value2: ?u32 = 222;
    std.debug.print("value1 orelse value2: {}\n", .{value1 orelse value2});
}

My expectation, it should print "123". But I got an runtime error (or at least I understand it's):

zig run main.zig
broken LLVM module found: Instruction does not dominate all uses!
  %7 = getelementptr inbounds %"struct:26:52", %"struct:26:52"* %1, i32 0, i32 0, !dbg !2021
  %12 = getelementptr inbounds %"?u32", %"?u32"* %7, i32 0, i32 1, !dbg !2020
Instruction does not dominate all uses!
  %7 = getelementptr inbounds %"struct:26:52", %"struct:26:52"* %1, i32 0, i32 0, !dbg !2021
  %13 = getelementptr inbounds %"?u32", %"?u32"* %7, i32 0, i32 0, !dbg !2020

This is a bug in the Zig compiler.thread 1856201 panic:
Unable to dump stack trace: debug info stripped
make: *** [run] Abort trap: 6

So is it the correct expectation or it's a bug? If it's a bug, where should I post it (sorry I'm a newbie).

I'm using zig 0.9.1. Run on MacOS 12.5 (21G72)

Thanks for your taking time


UPDATE: Tried with suggestions:

  • v0.9.1: Define 3rd variable: value3 = value1 orelse value2; and print it
  • v0.10: upgrade and don't see this error anymore.
Le Dong Thuc
  • 195
  • 1
  • 6
  • Another note, I tried with 2 const optional integers, and it works as expected. I just got the error with 2 var optional integers. – Le Dong Thuc Sep 07 '22 at 21:37
  • It looks like you hit a bug in the compiler, try using a third variable `var value3: u32 = value1 orelse value2.?;` and use it in your print statement. – hdorio Sep 07 '22 at 22:57
  • @hdorio It's possible to use `var value3 = value1 orelse value2;` (`value3` would be `?u32`). – sigod Sep 07 '22 at 23:33
  • 1
    it's working with v0.10 (master). please don't post bugs here, or at least try with latest versions – Ali Chraghi Sep 08 '22 at 06:14
  • First, thanks all for the time to take a look. It works by defining 3rd variable and printing it with 0.9.1 It also works without 3rd variable and version 0.10 – Le Dong Thuc Sep 08 '22 at 07:34
  • 2
    @LeDongThuc you should put the update section of your question as an answer to your own question and then accept it. Please don't use "EDIT" and "UPDATE" on StackOverflow, the edit history is there for everyone to see. – hdorio Sep 09 '22 at 12:59

1 Answers1

0

Tried with suggestions:

v0.9.1: Define 3rd variable: value3 = value1 orelse value2; and print it

v0.10: upgrade and don't see this error anymore.

Le Dong Thuc
  • 195
  • 1
  • 6