Skip to content

Peer type resolution should only happen when result type is unspecified #2749

@hryx

Description

@hryx

Peer type resolution is useful and convenient, but currently it takes precedence over other forms of type resolution when it shouldn't. Peer type resolution should only occur when the result type (see #2602) is unspecified. For function returns, the result type is always known because return types are a mandatory part of the function.

Here are two examples which currently fail due to this eager resolution, but which should succeed given implicit casting rules. The error is error: incompatible types: '[]u8' and '[1]u8'.


In this example, the compiler should attempt to cast each prong expression to []const u8 because the result type is predetermined by the function's return type. No peer type resolution should occur.

test "peer type resolution blocks implicit cast to return type" {
    for ("hello") |c| _ = f(c);
}

fn f(c: u8) []const u8 {
    return switch (c) {
        'h', 'e' => [_]u8{c}, // should cast to slice
        'l', ' ' => [_]u8{ c, '.' }, // should cast to slice
        else => ([_]u8{c})[0..], // is a slice
    };
}

In this example, the compiler should attempt to cast each prong expression to []const u8 because the result type is predetermined by the explicit variable type. No peer type resolution should occur.

test "peer type resolution blocks implicit cast to variable type" {
    var x: []const u8 = undefined;
    for ("hello") |c| x = switch (c) {
        'h', 'e' => [_]u8{c}, // should cast to slice
        'l', ' ' => [_]u8{ c, '.' }, // should cast to slice
        else => ([_]u8{c})[0..], // is a slice
    };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions