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

No error on comptime expression loading runtime value #22296

Open
kj4tmp opened this issue Dec 23, 2024 · 1 comment · May be fixed by #22305
Open

No error on comptime expression loading runtime value #22296

kj4tmp opened this issue Dec 23, 2024 · 1 comment · May be fixed by #22305
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@kj4tmp
Copy link
Contributor

kj4tmp commented Dec 23, 2024

Zig Version

0.14.0-dev.2540+f857bf72e

Steps to Reproduce and Observed Behavior

Create file test.zig with the following contents:

const std = @import("std");

test {
    var value = @as(u5, 0);
    const Foo = packed struct(u7) {
        val2: u7,
    };
    const foo: Foo = .{ .val2 = value };
    // if `comptime` is removed here, the segfault goes away
    std.debug.print("{}", .{comptime foo});
}

Observe segfault:

$ zig test test.zig 
Segmentation fault

Expected Behavior

Not sure, when I remove the comptime I get the following output:

test.zig:4:9: error: local variable is never mutated
    var value = @as(u5, 0);
        ^~~~~
test.zig:4:9: note: consider using 'const'
@kj4tmp kj4tmp added the bug Observed behavior contradicts documented or intended behavior label Dec 23, 2024
@Vexu
Copy link
Member

Vexu commented Dec 23, 2024

That error is correct but if you suppress it with _ = &value there should be an unable to evaluate comptime expression error.

Stack trace

thread 374045 panic: reached unreachable code
/home/vexu/.config/Code - OSS/User/globalStorage/ziglang.vscode-zig/zig/linux-x86_64-0.14.0-dev.2547+77c63ac36/lib/std/debug.zig:405:14: 0xa41c14c in assert (zig)
    if (!ok) unreachable; // assertion failure
             ^
/home/vexu/Documents/zig/zig/src/Liveness.zig:174:15: 0xb66c7ce in analyze (zig)
        assert(data.live_set.count() == 0);
              ^
/home/vexu/Documents/zig/zig/src/Zcu/PerThread.zig:907:40: 0xb26a4e7 in linkerUpdateFunc (zig)
    var liveness = try Liveness.analyze(gpa, air, ip);
                                       ^
/home/vexu/Documents/zig/zig/src/link.zig:1548:32: 0xae68ac1 in doTask (zig)
            pt.linkerUpdateFunc(func.func, func.air) catch |err| switch (err) {
                               ^
/home/vexu/Documents/zig/zig/src/Compilation.zig:3900:20: 0xaab2ec8 in dispatchCodegenTask (zig)
        link.doTask(comp, tid, link_task);
                   ^
/home/vexu/Documents/zig/zig/src/Compilation.zig:3677:37: 0xa8433a7 in processOneJob (zig)
            comp.dispatchCodegenTask(tid, .{ .codegen_func = func });
                                    ^
/home/vexu/Documents/zig/zig/src/Compilation.zig:3634:30: 0xa65fe92 in performAllTheWorkInner (zig)
            try processOneJob(@intFromEnum(Zcu.PerThread.Id.main), comp, job, main_progress_node);
                             ^
/home/vexu/Documents/zig/zig/src/Compilation.zig:3494:36: 0xa4f4cec in performAllTheWork (zig)
    try comp.performAllTheWorkInner(main_progress_node);
                                   ^
/home/vexu/Documents/zig/zig/src/Compilation.zig:2251:31: 0xa4ed1d6 in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/vexu/Documents/zig/zig/src/main.zig:4446:20: 0xa5285e7 in updateModule (zig)
    try comp.update(prog_node);
                   ^
/home/vexu/Documents/zig/zig/src/main.zig:3636:21: 0xa58f4d3 in buildOutputType (zig)
        updateModule(comp, color, root_prog_node) catch |err| switch (err) {
                    ^
/home/vexu/Documents/zig/zig/src/main.zig:273:31: 0xa41e4f1 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/home/vexu/Documents/zig/zig/src/main.zig:205:20: 0xa41b325 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/vexu/.config/Code - OSS/User/globalStorage/ziglang.vscode-zig/zig/linux-x86_64-0.14.0-dev.2547+77c63ac36/lib/std/start.zig:656:37: 0xa41ae2e in main (zig)
            const result = root.main() catch |err| {
                                    ^

@Vexu Vexu added the frontend Tokenization, parsing, AstGen, Sema, and Liveness. label Dec 23, 2024
@Vexu Vexu added this to the 0.15.0 milestone Dec 23, 2024
@Vexu Vexu changed the title Segfault on comptime value No error on comptime expression loading runtime value Dec 23, 2024
mlugg added a commit to mlugg/zig that referenced this issue Dec 24, 2024
Also, simplify the handling of this instruction; it was previously going
through a completely unnecessary runtime path. Who would write such
ridiculous code?! (It was me, I wrote it)

Resolves: ziglang#22296
@mlugg mlugg modified the milestones: 0.15.0, 0.14.0 Dec 24, 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 frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants