Skip to content

Sentinel value isn't set when array is initialized #23739

@rockssalt05

Description

@rockssalt05

Zig Version

0.15.0-dev.386+2e35fdd03

Steps to Reproduce and Observed Behavior

Run this program with zig test

// this doesn't set the sentinel
fn func1(yes: bool) [2:'!']u8 {
    return .{
        'h',
        if (yes) 'i' else 'a'
    };
}

// this does
fn func2(yes: bool) [2:'!']u8 {
    return @as([2:'!']u8, .{
        'h',
        if (yes) 'i' else 'a'
    });
}

test {
    const arr = func1(true);
    const slice = arr[0.. :'!'];
    _ = slice;
}

Whether the @as() is there or not shouldn't matter if I'm not mistaken
It works as expected if you remove the if statement

Using func1, it crashes with a garbage sentinel value

thread 5079 panic: sentinel mismatch: expected 33, found 252
./test.zig:21:22: 0x1048abf in test_0 (test)
    const slice = arr[0.. :'!'];
                     ^
/usr/local/zig-linux-x86_64-0.15.0-dev.386+2e35fdd03/lib/compiler/test_runner.zig:214:25: 0x10efc59 in mainTerminal (test)
        if (test_fn.func()) |_| {
                        ^
/usr/local/zig-linux-x86_64-0.15.0-dev.386+2e35fdd03/lib/compiler/test_runner.zig:62:28: 0x10e7ebd in main (test)
        return mainTerminal();
                           ^
/usr/local/zig-linux-x86_64-0.15.0-dev.386+2e35fdd03/lib/std/start.zig:662:22: 0x10e7360 in posixCallMainAndExit (test)
            root.main();
                     ^
/usr/local/zig-linux-x86_64-0.15.0-dev.386+2e35fdd03/lib/std/start.zig:282:5: 0x10e6f2d in _start (test)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
./test --seed=0xa2a79302

Expected Behavior

func1 and func2 behave the same

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions