Skip to content

compiler: a labeled for/while expression returns an unspecified value when break has no value #14686

@perillo

Description

@perillo

Zig Version

0.11.0-dev.6417+0bb178bbb

Steps to Reproduce and Observed Behavior

const std = @import("std");
const debug = std.debug;

pub fn main() void {
    var a: *isize = blk: for (0..10) |_| {
        break :blk;
    };
    debug.print("a: {}\n", .{a});

    var b: *isize = blk: while (true) {
        break :blk;
    };
    debug.print("b: {}\n", .{b});
}
$ zig run main.zig
a: isize@0
b: isize@7ffc30760750

NOTE: the output depends on the order of statements.
I got a consistent output when moving the b variable definition before a:

b: isize@0
a: isize@20

Expected Behavior

The compiler should report an error instead of causing an undefined behavior.

NOTE: I used a pointer variable to check if the value was the same as when using undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    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