Skip to content

Compiler Crash when Trying to Call a Call-by-Pointer Method on a Member of a Comptime Tuple #18683

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

Closed
1E0N4RD opened this issue Jan 25, 2024 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@1E0N4RD
Copy link

1E0N4RD commented Jan 25, 2024

Zig Version

0.12.0-dev.2334+aef1da163

Steps to Reproduce and Observed Behavior

const std = @import("std");

const A = struct {
    value: usize = 0,
    pub fn foo(self: A) void {
        _ = self;
    }
    pub fn bar(self: *A) void {
        _ = self;
    }
    pub fn baz(self: *const A) void {
        _ = self;
    }
};

pub fn B(comptime Child: type) type {
    return struct {
        children: Child,
    };
}

fn createB(children: anytype) B(@TypeOf(children)) {
    return .{ .children = children };
}

pub fn RuntimeTuple(comptime T: type) type {
    const info = @typeInfo(T);
    const len = info.Struct.fields.len;
    var types: [len]type = undefined;
    for (info.Struct.fields, &types) |field, *tp| {
        tp.* = field.type;
    }
    return std.meta.Tuple(&types);
}

fn createRuntimeB(children: anytype) B(RuntimeTuple(@TypeOf(children))) {
    return .{ .children = children }; 
}

pub fn main() !void {
    // creates a value with type B(struct{ comptime A = .{}, comptime A = .{} })
    var a = createB(.{ A{}, A{} });
    // c.children[0].value = 2; fails with reasonable error message.
    switch (.Ptr) {
        .Value => a.children[0].foo(), // Works
        .Ptr => a.children[0].bar(), // Semantically incorrect code. Crashes the compiler
        .ConstPtr => a.children[0].baz(), // Could maybe work?  Crashes the compiler
        else => unreachable,
    }

    // What I meant to do:
    // create a value with type B(struct{A, A})
    var b = createRuntimeB(.{ A{}, A{} });
    b.children[0].foo();
    b.children[0].bar();
    b.children[0].baz();
}

Note that the compiler is correct in rejecting the code but should do so with some kind of error message.

Expected Behavior

Compilation should fail with an error message similar to the one when you try to modify a field of a.

@1E0N4RD 1E0N4RD added the bug Observed behavior contradicts documented or intended behavior label Jan 25, 2024
@Vexu
Copy link
Member

Vexu commented Jan 25, 2024

Duplicate of #12963

@Vexu Vexu marked this as a duplicate of #12963 Jan 25, 2024
@Vexu Vexu closed this as not planned Won't fix, can't repro, duplicate, stale Jan 25, 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

2 participants