cknns
(Christos Koninis)
1
The following code produces different results when compiled with swift 5 compiler with -swift-version 4.2, and when compiled with either a swift 4.2 compiler or a swift 5 compiler(not using -swift-version).
Basically when in source code compatibility mode the casting from T to Any?(Optional) fails when T resolves to Any?.
func testfunc() -> T {
let anynil: Any? = nil
if let castedanynil = anynil as? T {
print("casting successful")
}
else {
print("casting failed")
}
fatalError()
}
let lala: Any? = testfunc()
Prints "casting failed" when build with swift 5 compiler with input sources version 4.2 e.g .swift -swift-version 4.2 test.swift
Prints "casting successful" in any other case
for example when complied with Apple Swift version 4.2.1 (swift-4.2.1-RELEASE), also prints "casting successful"
Is this a known bug? Can you suggest a good workaround?
xwu
(Xiaodi Wu)
2
Correct. The behavior changed in Swift 5 to address SR-4248.
As described in that bug:
...the change was initially made to Swift 4.2, however that accidentally caused a compatibility regression (SR-8704) so in Swift 5 the behaviour was limited to Swift 5 mode (with Swift 4.x modes using the old behaviour), which is the change described by the changelog.
Therefore, deliberately,
...the behavior of Swift 5 with -swift-version 4.2 is not compatible with Swift 4.2.x.
1 Like