Swift enumeration value corrupted unless fully qualified with enumeration name
| Originator: | morten.heiberg | ||
| Number: | rdar://19597928 | Date Originated: | January 26, 2015 |
| Status: | Closed | Resolved: | February 11, 2015 |
| Product: | Developer Tools | Product Version: | swift-600.0.56.1 |
| Classification: | Reproducible: | Always |
RESOLVED: Swift 1.2 and Xcode 6.3 beta
Summary:
-----------------------
The attached Swift file (47 lines) contains an enumeration, "Foo", with 3 possible values (named One, Two and Three). The value Foo.One has an associated String.
The function makeFoo constructs a Foo value and returns it as an optional to a callback closure.
When the closure is called with .Two it will appear as nil inside the closure.
When the closure is called with .Three it will appear as an optional .One but without the associated value.
Steps to Reproduce:
-----------------------
1: Compile the attached Swift file and run it
2: Observe the 3 printouts listing expected and actual behavior.
3: Note that the expected results are not produced in the last two cases.
4: Change Foo.One to .One in line 23 and observe that the compiler unexpectedly aborts compilation
Expected Results:
-----------------------
The values .Two and .Three should be preserved when passed to the callback closure.
The compiler should not abort compilation when using .One instead of Foo.One
Actual Results:
Using .One (instead of Foo.One) in line 23 causes the compiler to abort compilation with the message "Bitcast requires types of same width
%.asUnsubstituted = bitcast i192 %165 to i200, !dbg !1053
LLVM ERROR: Broken function found, compilation aborted!"
The program as attached produces the following unexpected output:
---
Should make an optional One:
Optional(One with associated value HELLO)
Should make an optional Two:
nil
Should make an optional Three:
Optional(One with associated value )
Version:
-----------------------
Xcode Version 6.1.1 (6A2008a)
swiftc --version
Swift version 1.1 (swift-600.0.56.1)
Target: x86_64-apple-darwin14.0.0
OS X Yosemite (version 10.10.1)
Notes:
-----------------------
The following workarounds have been found:
- Changing .Two and .Three to Foo.Two and Foo.Three in lines 25 and 27, respectively
- Making the Foo parameter to the closure non-optional
- Removing the second NSError parameter to the closure
- Removing the associated String value for .One
Attachments:
-----------------------
// main.swift
import Foundation
public enum Foo: Printable {
case One(String)
case Two
case Three
public var description: String {
switch self {
case .One(let s):
return "One with associated value \(s)"
case .Two:
return "Two"
case .Three:
return "Three"
}
}
}
public func makeFoo(value: Int, callback: (Foo?, NSError?) -> Void) {
switch value {
case 1:
callback(Foo.One("HELLO"), nil)
case 2:
callback(.Two, nil)
default:
callback(.Three, nil)
}
}
println("\nShould make an optional One:")
makeFoo(1, { (response, error) in
println(response)
})
// ERROR: Returns a nil
println("\nShould make an optional Two:")
makeFoo(2, { (response, error) in
println(response)
})
// ERROR: Returns an optional .One without an associated value
println("\nShould make an optional Three:")
makeFoo(3, { (response, error) in
println(response)
})
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!