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

Zero size tuple field is implicitly comptime which leads to confusing error message #22323

Open
tomboehmer opened this issue Dec 26, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@tomboehmer
Copy link

tomboehmer commented Dec 26, 2024

Zig Version

0.14.0-dev.2563+af5e73172

Steps to Reproduce and Observed Behavior

const std = @import("std");

test "broken" {
    const I = struct {
        // Adding a field or making `self` a non-pointer fixes the error
        fn next(_: *@This()) i32 {
            return 2;
        }
    };

    var iters: struct { I, I } = .{ I{}, I{} };
    //@compileError(@typeName(@TypeOf(iters)));

    var result: struct { i32, i32 } = undefined;

    result[0] = iters[0].next(); // error: runtime value contains reference to comptime var
    result[1] = iters[1].next();

    try std.testing.expectEqual(result, .{ 2, 2 });
}

I get a compilation error. I find this very confusing as it refers to a "comptime var" when I have not used "comptime" anywhere in the code.

src/main.zig:38:25: error: runtime value contains reference to comptime var
    result[0] = iters[0].next(); // error: runtime value contains reference to comptime var
                ~~~~~~~~^~~~~
src/main.zig:38:25: note: comptime var pointers are not available at runtime

Printing the type of iters reveals that iters apparently has two comptime fields.

src/main.zig:30:5: error: struct { comptime main.test.broken.I = .{}, comptime main.test.broken.I = .{} }
    @compileError(@typeName(@TypeOf(iters)));

Expected Behavior

There are two behaviors i would think sensible.

  1. The code should compile successfully and the test should pass
  2. If it is invalid to have a runtime pointer to a zero bit type (is it?) this should be the content of the compilation error

Talking about a "comptime var" when there is no "comptime" in the code is not helpful.

Actually, I think option 1 should be correct, because the following also compiles:

const std = @import("std");

test "works" {
    const I = struct {
        fn next(_: *@This()) i32 {
            return 2;
        }
    };

    var iter0: I = I{};
    var iter1: I = I{};

    var result: struct { i32, i32 } = undefined;

    result[0] = iter0.next();
    result[1] = iter1.next();

    try std.testing.expectEqual(result, .{ 2, 2 });
}
@tomboehmer tomboehmer added the bug Observed behavior contradicts documented or intended behavior label Dec 26, 2024
@tomboehmer tomboehmer changed the title Zero size tuple field is implicitly comptime which leads confusing error message Zero size tuple field is implicitly comptime which leads to confusing error message Dec 26, 2024
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

1 participant