zig icon indicating copy to clipboard operation
zig copied to clipboard

Compiler Segmentation fault on comptime expression referring to runtime variable

Open trgwii opened this issue 2 years ago • 4 comments

Zig Version

0.12.0-dev.1664+8ca4a5240

Steps to Reproduce and Observed Behavior

pub fn main() void {
    var foo = true;
    foo = false;
    const x = comptime foo;
    _ = x;
}
$ zig build-exe bug.zig
Segmentation fault

Expected Behavior

I expect some kind of compile error

trgwii avatar Nov 21 '23 12:11 trgwii

Here are some of my earlier attempts before I managed to reduce this example (these also all segfault):

threadlocal var foo = true;
pub fn main() void {
    const x = comptime foo;
    _ = x;
}
var foo = true;
pub fn main() void {
    const x = comptime foo;
    _ = x;
}

trgwii avatar Nov 21 '23 12:11 trgwii

The assertion failure happens in Liveness.zig and using -fstrip or -fno-emit-bin avoids it.

Vexu avatar Nov 21 '23 12:11 Vexu

If I print x, it still segfaults on build with -fstrip:

const std = @import("std");

pub fn main() void {
    var foo = true;
    foo = false;
    const x = comptime foo;
    std.debug.print("{}\n", .{x});
}
$ zig build-exe -fstrip bug.zig
Segmentation fault

trgwii avatar Nov 21 '23 12:11 trgwii

All examples still segfault the compiler on 0.12.0-dev.3193+4ba4f94c9

trgwii avatar Mar 11 '24 09:03 trgwii