Skip to content

[SR-2324] Optional closure type is always considered @escaping #44931

@swift-ci

Description

@swift-ci
Previous ID SR-2324
Radar rdar://problem/27795901
Original Reporter ilijatovilo (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

OSX 10.11.6 (15G31)
MacBook Pro (Retina, Mid 2012)
Version 8.0 beta 5 (8S193k)
Toolchain: Swift Development Snapshot 2016-08-07 (a)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @milseman
Priority Medium

md5: ab4ee203bb7d77d3976562cd10a4342c

is duplicated by:

Issue Description:

The following code snippet doesn't work anymore in the current Swift 3 snapshot:

func takes(anOptionalClosure: (() -> Void)?) {
    if let aClosure = anOptionalClosure {
        aClosure()
    }
}

func takes(aClosure: () -> Void) {
    takes(anOptionalClosure: aClosure)
}

> main.swift:18:30: Cannot convert value of type '() -> Void' to expected argument type '(() -> Void)?'

My guess is that the type checker thinks that `(() -> Void)?` is interpreted as a tuple type. But the parens are only necessary to make the closure optional. Interestingly this only happens with function types. `(String)?` e.g. type checks correctly.

The following snippet works as expected:

func takes(anOptionalClosure: (() -> Void)?) {
    if let aClosure = anOptionalClosure {
        aClosure()
    }
}

// Hint: Added two surrounding parens to the argument type
func takes(aClosure: (() -> Void)) {
    takes(anOptionalClosure: aClosure)
}

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itself

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions