Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default expression for inference works for static methods but not functions #67680

Open
adamkuipers opened this issue Aug 2, 2023 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself declarations Feature: declarations default arguments Feature: default arguments for value parameters func Feature → declarations: Functions generics Feature: generic declarations and types swift 5.9 type checker Area → compiler: Semantic analysis type inference Feature: type inference unexpected error Bug: Unexpected error

Comments

@adamkuipers
Copy link

adamkuipers commented Aug 2, 2023

Description
A default expression for type inference of a protocol when another parameter is an associated type of the protocol works for static methods but not functions.

Consider Task.sleep:

static func sleep<C>(
    until deadline: C.Instant,
    tolerance: C.Instant.Duration? = nil,
    clock: C = ContinuousClock()
) async throws where C : Clock

This method has a generic type C constrained to Clock, and it's parameters use both the types C and C.Instant. C is inferred from the clock parameter's the default expression ContinousClock().

Task.sleep is inexpressible as a function, even though no type information from Task is required—Task is strictly used as a namespace. As far as I know, static methods should be expressible as functions as long as they don't depend on anything from the type.

Steps to reproduce
Here's a minimal example using a default expression for inference of a protocol in a static method and as a function, with the function failing to compile:

protocol Parent {
    associatedtype Child
}

struct Default: Parent {
    struct Child {}
}

enum Namespace {
    static func defaulting<P: Parent>(
        child: P.Child,
        parent: P = Default()
    ) {}
}

func defaulting<P: Parent>(
    child: P.Child,
    parent: P = Default()
) {}

The function defaulting fails to compile with the message, Cannot use default expression for inference of 'P' because it is inferrable from parameters #0, #1.

Expected behavior
I'd expect to be able to express functions the same as static methods such as Task.sleep and, from the example above, Namespace.defaulting.

Environment

  • Swift compiler version info
    swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
    Target: arm64-apple-macosx13.0
  • Xcode version info Xcode 14.3.1 (14E300c)
  • Deployment target: N/A, any
@adamkuipers adamkuipers added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Aug 2, 2023
@tbkka
Copy link
Contributor

tbkka commented Aug 2, 2023

I also see this with Xcode 15 beta 4.

@tbkka
Copy link
Contributor

tbkka commented Aug 2, 2023

CC: @xedin

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself type checker Area → compiler: Semantic analysis generics Feature: generic declarations and types type inference Feature: type inference default arguments Feature: default arguments for value parameters unexpected error Bug: Unexpected error declarations Feature: declarations func Feature → declarations: Functions swift 5.9 and removed triage needed This issue needs more specific labels labels Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself declarations Feature: declarations default arguments Feature: default arguments for value parameters func Feature → declarations: Functions generics Feature: generic declarations and types swift 5.9 type checker Area → compiler: Semantic analysis type inference Feature: type inference unexpected error Bug: Unexpected error
Projects
None yet
Development

No branches or pull requests

3 participants