Skip to content
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

Assigning to comptime field through pointer with a different value doesn't yield a compile error #22724

Open
tauoverpi opened this issue Feb 2, 2025 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@tauoverpi
Copy link
Contributor

Zig Version

0.14.0-dev.3020+c104e8644

Steps to Reproduce and Observed Behavior

The following hits the explicit @compileError and doesn't error on assignment.

comptime {
    var a = .{ .b = @as([3]u32, @splat(0)) };

    const p = &a.b;
    p[2] = 5;

    if (a.b[2] != 5) @compileError("hmm");
}

however:

test {
    var a = .{ .b = @as([3]u32, @splat(0)) };

    const p = &a.b;
    p[2] = 5;

    if (a.b[2] != 5) return error.Hmm;
}

gives:

anon.zig:5:10: error: value stored in comptime field does not match the default value of the field
    p[2] = 5;
    ~~~~~^~~

Expected Behavior

A compile error for assigning a non-default value to a comptime field at comptime.

@tauoverpi tauoverpi added the bug Observed behavior contradicts documented or intended behavior label Feb 2, 2025
@rohlem
Copy link
Contributor

rohlem commented Feb 2, 2025

Note that when removing the @compileError, the first snippet passes (for me on 0.14.0-dev.2987+183bb8b08),
so it's not just about analysis/error ordering/priority, the compile error is fully absent
in a comptime block (as opposed to a runtime block like test { ... }):

comptime {
    var a = .{ .b = @as([3]u32, @splat(0)) };
    const p = &a.b;
    p[2] = 5;
}

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

No branches or pull requests

2 participants