Home » Swift » inout parameter in closure crashes the Swift compiler

inout parameter in closure crashes the Swift compiler

Posted by: admin November 30, 2017 Leave a comment

Questions:

All I need to do is start a new project in Swift and add to main.swift

struct Foo {
  let bar: (inout baz: String) -> ()
}

When I try to build I get an error:
Command failed due to signal: Segmentation fault: 11

Am I doing anything wrong?

I thought that perhaps inout parameters in closures are not supported, but if I define a closure like so:

let baz: (inout baz: String) -> () = { baz in
  baz += "x"
  return
}

or even

var baz: (inout baz: String) -> ()?

it compiles and runs OK

Answers:

Just tested it in Swift 1.2 shipped with Xcode 6.3 beta, and it compiled successfully.
So it was definitely a bug on the compiler that they solved in the last release

Questions:
Answers:

@Bartek Chlebek. In the code you posted, there’s one ambiguous thing (goes in bold): “let bar: (inout baz: String) -> ()“. When defining your function like that, you tell the compiler that you are going to return an empty tuple. It seems like the earlier versions of the compiler didn’t distinguish (must be a bug in the type inferring engine) the Void type and the “empty tuple type” (which is denoted with “()“). Here is a couple of word about the syntax of tuples: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/doc/uid/TP40014097-CH31-ID448

Possible solution: explicitly specify the return value type (which is “Void” in your case). Hope this helps.