ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.64k stars 2.53k forks source link

Compiler Segmentation fault on comptime expression referring to runtime variable #18058

Open trgwii opened 11 months ago

trgwii commented 11 months ago

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 commented 11 months ago

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;
}
Vexu commented 11 months ago

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

trgwii commented 11 months ago

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 commented 7 months ago

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