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

[SR-14250] Using callAsFunction syntax on closure shorthand inout parameters do not compile #56610

Open
mattyoung opened this issue Feb 17, 2021 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@mattyoung
Copy link

Previous ID SR-14250
Radar rdar://problem/74616583
Original Reporter @mattyoung
Type Bug

Attachment: Download

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

md5: 5087e27f26c82f7087f2bb64b451af5f

Issue Description:

Xcode Version 12.5 beta 2 (12E5234g)

struct Accumulator {
    var value = 0

    mutating func doIt(_ n: Int) {
        value += n
    }

    mutating func callAsFunction(_ n: Int) {
        doIt(n)
    }
}


let result1 = [1, 2, 3, 4].reduce(into: Accumulator()) {
    $0.doIt($1)     // 1) call mutating is fine here
}
.value

// this should work, but do not
let result2 = [1, 2, 3, 4].reduce(into: Accumulator()) {
    // 2)
    // in Xcode 12.5b2 playground:
    // error: cannot use mutating member on immutable value: '$0' is immutable
    // in iPad Playground:
    // Type of expression is ambiguous without more context
    $0($1)
}
.value


let result3 = [1, 2, 3, 4].reduce(into: Accumulator()) { (a: inout Accumulator, n: Int) in
    a(n)          // 3) this is fine
}
.value

see: https://forums.swift.org/t/reduce-into-why-cannot-use-callasfunction-syntax-cannot-use-mutating-member-on-immutable-value-0-is-immutable/44861/5

@typesanitizer
Copy link

@swift-ci create

@mattyoung
Copy link
Author

Still do not compile in Xcode 13.0 beta 2. The odd thing is even with named closure parameter that was working in Xcode 13.0 beta 1 is now broken same error as the shorthand parameters.

But then Xcode 13.0 beta 2 came out and the named version now back to working.

And Xcode Version 13.0 beta 3 (13A5192i) breaks again

struct Accumulator {
    var value = 0
    mutating func doIt(_ n: Int)
    { value += n }
    mutating func callAsFunction(_ n: Int)
    { doIt(n) }
}

let result0 = [1, 2, 3, 4].reduce(into: Accumulator()) {
    $0($1)  // still compile error: Cannot use mutating member on immutable value: '$0' is immutable
}
.value

// what? It works now in Xcode Version 13.0 beta (13A5155e) (beta 2)
let result1 = [1, 2, 3, 4].reduce(into: Accumulator()) { (a: inout Accumulator, n: Int) in
    a(n)    // compile error in Xcode 13.0 beta 1
            // works fine now in beta 2
}
.value

let result2 = [1, 2, 3, 4].reduce(into: Accumulator()) { (a: inout Accumulator, n: Int) in
    a.doIt(n)
}
.value

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 in itself
Projects
None yet
Development

No branches or pull requests

2 participants