Skip to content

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

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

Open
rockssalt05 opened this issue May 1, 2025 · 1 comment · May be fixed by #23882
Open

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

rockssalt05 opened this issue May 1, 2025 · 1 comment · May be fixed by #23882
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@rockssalt05
Copy link

rockssalt05 commented May 1, 2025

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

@rockssalt05 rockssalt05 added the bug Observed behavior contradicts documented or intended behavior label May 1, 2025
@rockssalt05
Copy link
Author

This is similar to #4372, but using .{} in the initializer instead
So you can reduce the test program even further to this

test {
    var h: u8 = 'h';
    _ = &h;
    const arr: [2:'!']u8 = .{h, 'i'};
    const slice = arr[0.. :'!'];
    _ = slice;
}

@rockssalt05 rockssalt05 changed the title Sentinel value isn't set when returning sentinel terminated array from a function Sentinel value isn't set when array is initialized May 3, 2025
@mlugg mlugg added this to the 0.14.1 milestone May 15, 2025
@alexrp alexrp modified the milestones: 0.14.1, 0.15.0 May 18, 2025
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants