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-3450] Closure with an 'inout' closure argument crashes the compiler when supplied to generic function #46038

Closed
swift-ci opened this issue Dec 18, 2016 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-3450
Radar None
Original Reporter dfrib (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Xcode 8.1 (8B62), Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1)

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

md5: 2f32d2c1df386af295fa1b28177baf01

is duplicated by:

  • SR-3987 Compiler crashes in generic function with inout tuple parameter holding function type

Issue Description:

We may successfully supply a closure with an inout closure as argument to a non-generic function:

func test(next: (inout () -> ()) -> ()) {
    var overwriteThisClosure = { print("bar") }
    next(&overwriteThisClosure)
    overwriteThisClosure()
}

test(next: {
    (foo: inout () -> ()) -> () in
    foo = { print("foo") }
}) // "foo"

If we define a generic function with a closure parameter that has an inout argument (using the generic typeholder),

func testGeneric<State>(next: (inout State) -> ()) {
    // ...
}

the compiler crashes if we call this generic function using a closure argument where the inout argument type of the supplied closure is a closure itself:

testGeneric(next: {
    (foo: inout () -> ()) -> () in
    foo = { print("foo") }
}) // "foo"

The crash produces the following crash dump/error:

Command failed due to signal: Illegal instruction: 4
...
inout writeback in abstraction difference thunk not yet implemented
input value   %0 = argument of bb0 : $*@callee_owned (@in ()) -> @out ()
output type $*@callee_owned () -> ()

The bug(limitation?) above limits a use case for the global sequence(state:next: ) function; the case where one would like to use a closure as the mutable state parameter.

// dummy example (not even making use of the state 'foo') yielding the same compiler crash
var i = 0
let seq = sequence(
    state: { () -> () in () },
    next: { (foo: inout () -> ()) -> Int? in
        i += 1
        guard i < 5 else { return nil }
        return i
})
@palimondo
Copy link
Mannequin

palimondo mannequin commented Feb 17, 2017

I think it is relevant to mention that you ran into this issue trying to use closure as state in

@slavapestov
Copy link
Member

@rjmccall fixed this in #18891 Thanks John!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 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 crash Bug: A crash, i.e., an abnormal termination of software
Projects
None yet
Development

No branches or pull requests

3 participants