Skip to content

value with comptime-only type '@TypeOf(null)' depends on runtime control flow #16770

@ehaas

Description

@ehaas

Zig Version

0.12.0-dev.47+0461a64a9

Steps to Reproduce and Observed Behavior

fn foo(x: bool) error{OutOfMemory}![]const u8 {
    if (x) return "" else return error.OutOfMemory;
}

test {
    var x = true;
    const val = foo(x) catch |err| switch (err) {
        error.OutOfMemory => null,
    };
    _ = val;
}
~ % zig test test.zig
test.zig:7:36: error: value with comptime-only type '@TypeOf(null)' depends on runtime control flow
    const val = foo(x) catch |err| switch (err) {
                                   ^~~~~~
test.zig:7:24: note: runtime control flow here
    const val = foo(x) catch |err| switch (err) {
                ~~~~~~~^~~~~

Giving val an explicit type of ?[]const u8 prevents the error

Expected Behavior

Test passes

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions