Skip to content

callAsFunction mistaken for a trailing closure when using a static initializer on same line #74853

Open
@davepaul0

Description

@davepaul0

Description

callAsFunction can be used in the same statement as an object's initializer call - this is put to good use with SwiftUI's Layout protocol.

However, if an object that implements callAsFunction is initialized with a free function or a static initializer, then trying to invoke the callAsFunction implementation without binding to a variable first will fail to compile.

Reproduction

struct MyStruct {
    static func foo() -> MyStruct { MyStruct(label: "foo") }

    let label: String

    func callAsFunction(_ work: () -> Void) -> MyStruct {
        self
    }
}

// initializer and callAsFunction argument on same line - no error
let ex1 = MyStruct(label: "foo") { _ = 1 + 1 }

// static init and callAsFunction on separate lines - no error
let ex2 = MyStruct.foo()
_ = ex2 { _ = 1 + 1 }

// static init and callAsFunction on same line - "Extra trailing closure passed in call"
let ex3 = MyStruct.foo() { _ = 1 + 1 }

Expected behavior

I would expect the third example in the code block above to compile without errors.

Environment

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

(also tested with Xcode 16 Beta 2)

Additional information

No response

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