Swift: Types declared in if block & used via higher-order functions fail to compile/link w/o error (in Xcode)

Originator:rix.rob
Number:rdar://18332184 Date Originated:14-Sep-2014 03:08 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode-Beta (6A1027)
Classification:Serious Bug Reproducible:Always
 
Summary:
When you use a higher-order function like compose to return a function which calls a function which uses a type defined in an if block, Swift refuses to compile the file. In Xcode, no error is shown (which is perhaps a separate bug). From the CLI, you get this message:

Resonance:Desktop rob$ ./Failure.swift 
invalid linkage type for global declaration
%swift.full_type* @_TMdV7FailureL_1S
LLVM ERROR: Broken module found, compilation aborted!


Steps to Reproduce:
1. Try to compile this code:

if true {
	struct S {
		let v: Int
	}
	let g = { S(v: $0) }
	let f = { 0 }
	func compose<T, U, V>(g: U -> V, f: T -> U)(x: T) -> V { return g(f(x)) }
	let h = compose(g, f)
}


Expected Results:
I expected it to succeed without issue.


Actual Results:
In Xcode, it failed and wouldn’t tell me the issue. From the CLI, it failed and told me something about the issue, but nothing about the source it was operating on, or why it’s a problem. Further, I don’t think it should be a problem at all.


Regression:
If you move the definition of struct S outside of the if statement, it succeeds.
If you let h = { g(f($0)) } (instead of using compose()), it succeeds.
struct, enum, class, all of them fail. There is no escape.
A variant on this using the venerable class Box had a slightly different error:

Global is external, but doesn't have external or weak linkage!

For my case the best workaround is not to define the type inside the if block.


Notes:
N/A

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!