Type inference from default expression doesn't compile in free function

This compiles, taking advantage of SE-0347: Type inference from default expressions:

struct MyTask {
    func sleep<C: Clock>(
        until deadline: C.Instant,
        clock: C = ContinuousClock()
    ) async throws {}
}

But: the same function signature as a free function doesn't compile:

func sleep<C: Clock>(
    until deadline: C.Instant,
    // ❌: Cannot use default expression for inference of 'C' 
    // because it is inferrable from parameters #0, #1
    clock: C = ContinuousClock()
) async throws {}

Observed with Xcode 15.3 (Swift 5.10).

This looks like a bug, doesn't it? Or am I missing anything? /cc @xedin

2 Likes

It does look like a bug because C cannot actually be inferred from C.Instant

@xedin Thanks for the quick reply. I filed an issue: Type inference from default expression doesn’t compile in free function · Issue #72199 · apple/swift · GitHub

Thank you!