Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pmwhite opened this issue May 28, 2020 · 1 comment
Open

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

pmwhite opened this issue May 28, 2020 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@pmwhite
Copy link

pmwhite commented May 28, 2020

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.

@Vexu
Copy link
Member

Vexu commented May 29, 2020

This is another example of #5401. The result of parseFloat is known at compile time to not be an error so the catch branch is never evaluated. This skips the peer type resolution which would make it an optional type. You can avoid this by explicitly casting the condition @as(?[]const u8, ...).

@Vexu Vexu added the stage1 The process of building from source via WebAssembly and the C backend. label May 29, 2020
@Vexu Vexu added this to the 0.7.0 milestone May 29, 2020
@Vexu Vexu added the bug Observed behavior contradicts documented or intended behavior label May 29, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@Vexu Vexu removed the stage1 The process of building from source via WebAssembly and the C backend. label Dec 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

3 participants