Skip to content

Catch null in if-condition cails at comptime. #5462

Open
@pmwhite

Description

@pmwhite
const std = @import("std");

fn f(string: []const u8) ?f32 {
    if (std.fmt.parseFloat(f32, string) catch null) |decimal| {
        return decimal;
    }
    return null;
}

pub fn main() !void {
    if (comptime f("1")) |x| {
        std.debug.warn("x = {}\n", .{x});
    }
}

This code fails with the following compiler error:

Semantic Analysis [568/862] ./test.zig:4:41: error: expected optional type, found 'f32'
    if (std.fmt.parseFloat(f32, string) catch null) |decimal| {
                                        ^
./test.zig:11:19: note: called from here
    if (comptime f("1")) |x| {
                  ^
./test.zig:10:21: note: called from here
pub fn main() !void {
                    ^
./test.zig:4:5: note: referenced here
    if (std.fmt.parseFloat(f32, string) catch null) |decimal| {
    ^
./test.zig:11:19: note: referenced here
    if (comptime f("1")) |x| {
                  ^
/home/philip/Downloads/zig-linux-x86_64-latest/lib/zig/std/start.zig:252:40: note: referenced here
            const result = root.main() catch |err| {

Note that removing the comptime part from the call to f in main makes this code 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