Skip to content

Type mismatched when a function returns a generic function #14540

@e0328eric

Description

@e0328eric

Zig Version

0.11.0-dev.1557+03cdb4fb5

Steps to Reproduce and Observed Behavior

const std = @import("std");

pub fn main() void {
    const foo = factory();
    const bar = factory();

    std.debug.print("{s}\n", .{@typeName(@TypeOf(foo))});
    std.debug.print("{s}\n", .{foo([]const u8, "Hello")});

    std.debug.print("{s}\n", .{@typeName(@TypeOf(bar))});
    std.debug.print("{s}\n", .{bar([]const u8, "Hello")});
}

fn factory() (fn (type, anytype) i32) {
    return struct {
        fn id(comptime T: type, bar: T) T {
            return bar;
        }
    }.id;
}

output:

fn(comptime type, anytype) anytype
Hello
fn(comptime type, anytype) i32
Hello

Expected Behavior

Two functions foo and bar should have a same type.
Also the compiler thinks that bar returns i32 at that point, but it successfully returns of type []const u8.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions