Skip to content

Stage2: simple if expression randomly has wrong result when safety checks are enabled #11898

Closed
@Vexu

Description

@Vexu
test {
    @setRuntimeSafety(false);
    var self: []const u8 = "abcdef";
    var index: usize = 0;
    var left_index = (index << 1) + 1;
    var right_index = left_index + 1;
    var left = if (left_index < self.len) self[left_index] else null;
    var right = if (right_index < self.len) self[right_index] else null;
    std.debug.print("left {d} right {d}\n", .{ left_index < self.len, right_index < self.len });
    std.debug.print("left {d} right {d}\n", .{ left, right });
}

The same binary produced by zig2 test a.zig -femit-bin=a ran multiple times will sometimes print the correct result left 98 right 99 and on other times left null right null. When compiled in a release mode it always produces the correct result, @setRuntimeSafety does not work as the resulting AIR always has calls to panicOutOfBounds.

This is blocking std.PriorityQueue tests.

Metadata

Metadata

Assignees

Labels

backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behaviorfrontendTokenization, parsing, AstGen, Sema, and Liveness.miscompilationThe compiler reports success but produces semantically incorrect code.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions