Skip to content

Cannot use a struct literal in for loop else branch  #16010

@mehmet-yuksel

Description

@mehmet-yuksel

Zig Version

0.11.0-dev.3387+f04e65bc0

Steps to Reproduce and Observed Behavior

This doesn't compile

pub fn main() void {
    const S = struct { x: u8 };
    var x = for ([_]S{.{ .x = 42 }}) |s| {
        break s;
    } else S{ .x = 42 };
    _ = x;
}

and the compiler error is this

src\main.zig:3:13: error: incompatible types: 'main.main.S' and 'void'
    var x = for ([_]S{.{ .x = 42 }}) |s| {
            ^~~
src\main.zig:4:15: note: type 'main.main.S' here
        break s;

The weird thing is if we annotate x:

pub fn main() void {
    const S = struct { x: u8 };
    var x: S = for ([_]S{.{ .x = 42 }}) |s| {
        break s;
    } else S{ .x = 42 };
    _ = x;
}

It compiles fine.

Expected Behavior

Both of these code snippets should compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions