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-2863] Calling closures with inout parameter inside a generic function causes runtime error #45457

Closed
hujunfeng opened this issue Oct 5, 2016 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression runtime The Swift Runtime swift 3.0

Comments

@hujunfeng
Copy link

Previous ID SR-2863
Radar None
Original Reporter @hujunfeng
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

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

md5: 0704b664287f1c92d42927ae3bb5eb89

duplicates:

  • SR-2773 function abnormally releasing instance.

Issue Description:

I was using the `with` function [introduced by Erica Sadun and Brent Royal-Gordon](https://gist.github.com/brentdax/ce3272e3d35f5ccac56483666f86b8fb). It works perfectly fine in Swift 2.2, but stops working in Swift 3.0 now.

Here is a minimum version of `with` function that still can reproduce the error:

func with<T>(_ item: T, update: (inout T) -> Void) -> T {
    var this = item
    update(&this)
    return this
}

It throws the following error if I use it to initialize an object:

let view = with(UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))) {
    $0.backgroundColor = .white
}
// error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

Screenshot of showing this error in Playground (swift-DEVELOPMENT-SNAPSHOT-2016-10-02-a):

However a similar function works if the generic parameter is removed, like this one:

public func withView(_ item: UIView, update: (inout UIView) throws -> Void) rethrows -> UIView {
    var this = item
    try update(&this)
    return this
}

let view = withView(UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))) {
    $0.backgroundColor = .black
}
// No error

The original function also works if a constant or variable is passed in instead:

let newView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let view = with(newView) {
    $0.backgroundColor = .white
}
// No error

Is this a bug? Is the original `with` function supposed to work in Swift 3.0?

@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 runtime The Swift Runtime swift 3.0
Projects
None yet
Development

No branches or pull requests

2 participants