ziglang / zig

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

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

Open trgwii opened 1 year ago

trgwii commented 1 year 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 1 year 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 1 year ago

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

trgwii commented 1 year 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 8 months ago

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