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-5628] inout argument of closure passed to generic function inferred as immutable #48198

Closed
werediver opened this issue Aug 4, 2017 · 3 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.0 type checker Area → compiler: Semantic analysis

Comments

@werediver
Copy link

Previous ID SR-5628
Radar rdar://problem/33725301
Original Reporter @werediver
Type Bug
Status Resolved
Resolution Done
Environment

Swift 4.0 (swiftlang-900.0.54.11 clang-900.0.31), Xcode 9.0 beta 4 9M189t, Mac OS X 10.12.5 16F73

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, 4.0Regression, TypeChecker
Assignee @xedin
Priority Medium

md5: 5f0a2256dc0a8c39b3a35e83526a0562

relates to:

  • SR-3520 Generic function taking closure with inout parameter can result in a variety of compiler errors or EXC_BAD_ACCESS

Issue Description:

Swift 4.0 compiler bundled with Xcode 9 beta 4 9M189t

$ sudo xcode-select -s /Applications/Xcode-9b4.app/ # Xcode 9.0 beta 4 9M189t
$ swift --version
Apple Swift version 4.0 (swiftlang-900.0.54.11 clang-900.0.31)
Target: x86_64-apple-macosx10.9

fails on this sample

$ cat mut.swift 
func mut<T, U>(_ some: T, _ body: (inout T) -> U) -> U {
    var _some = some
    return body(&_some)
}

let a = 1
mut(a) { $0 += 1; print($0) }

as shown below

$ swiftc mut.swift 
mut.swift:7:13: error: left side of mutating operator isn't mutable: '$0' is immutable
mut(a) { $0 += 1; print($0) }
         ~~ ^

This behavior is also reproducible in an Xcode Playground.
The issue is related to generic functions only. The following code does not produce any errors.

func muti(_ some: Int, _ body: (inout Int) -> ()) -> () {
    var _some = some
    return body(&_some)
}

let a = 1
muti(a) { $0 += 1; print($0) }

Swift 3.1 compiler bundled with Xcode 8.3.2 8E2002

$ sudo xcode-select -s /Applications/Xcode.app # Xcode 8.3.2 8E2002
$ swift --version
Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42)
Target: x86_64-apple-macosx10.9

compiles the problematic sample without any complains

$ swiftc mut.swift 
$ ./mut
2

This seems to be related to SR-3520, so this can be a regression.

@belkadan
Copy link
Contributor

belkadan commented Aug 4, 2017

Thanks, Raman!

@swift-ci create

@rudkx
Copy link
Member

rudkx commented Mar 7, 2018

@xedin I think you may have fixed this, do you recall?

It doesn't reproduce for me anymore.

@xedin
Copy link
Member

xedin commented Mar 7, 2018

@rudkx Yes, this should be fixed by #12072 and it's follow-up PRs which address corner cases.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 regression swift 4.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants