Skip to content

Type inference from default expression doesn’t compile in free function #72199

Closed
@ole

Description

@ole

Description

A function definition that relies on Type inference from default expression (SE-0347) compiles when it's defined inside a type (a method), but does't compile when it's defined as a free function.

Reproduction

As a method, this compiles:

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 {}

Expected behavior

The free function should compile.

Environment

macOS 14.3, Xcode 15.3 (Swift 5.10)

$ swift --version
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

Forum thread where this is being discussed: https://forums.swift.org/t/type-inference-from-default-expression-doesnt-compile-in-free-function/70521/3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.expressionsFeature: expressionstype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions