-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
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
};
}