zig icon indicating copy to clipboard operation
zig copied to clipboard

stage2: returning undefined struct from function should set fields to 0xAAAAAAAA w/ safety checks

Open mitchellh opened this issue 3 years ago • 0 comments

Zig Version

0.10.0-dev.4342+8bb2e96ac

Steps to Reproduce

const std = @import("std");

const Rect = struct { x: f32 };

fn doAThing() Rect {
    return undefined;
}

test {
    var x: Rect = doAThing();
    try std.testing.expect(@bitCast(u32, x.x) == 0xAAAAAAAA);
}

Expected Behavior

Test to pass, fields should be 0xAAAAAAAA to help find undefined usage with safety checks.

Actual Behavior

Fields in the struct are set to 0, consistently.

Even if this is the intended behavior, it does not match the behavior when undefined is set to a variable. For example, the following DOES pass currently:

test {
    var x: Rect = undefined;
    try std.testing.expect(@bitCast(u32, x.x) == 0xAAAAAAAA);
}

So one of these behaviors seems correct, but not both.

mitchellh avatar Oct 15 '22 22:10 mitchellh