Skip to content

stage2: comptime fields are not always checked for equality when initialized #12049

@topolarity

Description

@topolarity

Zig Version

0.10.0-dev.2882+556f12174

Steps to Reproduce

This fails as expected:

test {
    const T = struct {
        comptime x: i32 = 0,
        comptime y: u8 = 123,
    };
    var a: T = .{.x = -1234, .y = 0};
    _ = a;
}

However, both of these currently compile when they should not:

pub const U = struct {
    comptime foo: u32 = 1,
    bar: u32,
};
fn foo(x: U) void {
    _ = x;
}
test {
    _ = foo(U{ .foo = 2, .bar = 2 });
}
test {
    var x: u32 = 15;
    const T = @TypeOf(.{ @as(i32, -1234), @as(u32, 5678), x });
    var a: T = .{ -1234, 5678, x + 1 };
    _ = a;
}

Expected Behavior

All three tests above should fail with the error message "value stored in comptime field does not match the default value of the field"

Actual Behavior

The second test compiles successfully. The third test panics due to generating a runtime store to a comptime field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions