Swift: Vindictive crash when capturing local function in a closure

Originator:rix.rob
Number:rdar://19152922 Date Originated:04-Dec-2014 11:20 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 6.1.1 (6A2006)
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:
Swift doesn’t let you define recursive local functions ( ಠ_ಠ — seriously grumpymaking), and if you try to trick it, it crashes.


Steps to Reproduce:
1. Compile this elaborate code!

func f() {
	func g() {
		{ g() }()
	}
}


Expected Results:
I expected it to allow me to capture g, or at least to shout at me with the usual “local functions cannot reference themselves” error.


Actual Results:
Swift noticed my subterfuge and crashed just to spite me.


Regression:
If you don’t use the closure it just says:

./spurious-local-nested-function-error-of-doom.swift:5:3: error: local functions cannot reference themselves

You can get a variation on this crash by returning the function from the closure instead of calling it and binding that to a constant, but it has a pretty similar stack trace. For completeness’ sake, the code:

func f() {
	func g() {
		let h = { g }()
	}
}


Notes:
I was delighted to encounter this one in the reduction of a spurious compiler error rather than in my actual code ✨

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!