Hi there,
I am the author of SE-0320 and did the implementation too.
As Xcode 13.3 beta 1 just dropped, I was excited to try out my own feature. Unfortunately it doesn't work. :-/
I get different behaviors in an app target and in a Playground, but the issues may be related.
In a Playground I do:
import Foundation
struct Wrapper: RawRepresentable, Codable, Hashable, CodingKeyRepresentable {
var rawValue: String
}
let test: [Wrapper: Int] = [
.init(rawValue: "a"): 1,
.init(rawValue: "b"): 2
]
let encoded = try! JSONEncoder().encode(test)
print("Encoded", String(data: encoded, encoding: .utf8))
And in the output I get:
expression failed to parse:
error: Couldn't lookup symbols:
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Swift.RawRepresentable< where τ_0_0: Swift.CodingKeyRepresentable, τ_0_0.Swift.RawRepresentable.RawValue == Swift.String>.codingKey.getter : Swift.CodingKey
Using the same code in an app target compiles, but the output is:
Encoded ["a",1,"b",2]
Where I was expecting:
{"a":1,"b":2}
Does anyone have a suggestion about how to start debugging such an issue?
The presence of the protocol tells me that the implementation must be present in the version of Swift in the toolchain, because all of the implementation was done in one PR.
2 Likes
Follow-up:
In a Playground, implementing the CodingKeyRepresentable
conformance 'manually' rather than relying on the built-in conformance when your type is RawRepresentable
gives a different error:
import Foundation
struct Wrapper: Codable, Hashable {
var wrapped: String
}
struct MyCodingKey: CodingKey {
var stringValue: String
init(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
nil
}
}
extension Wrapper: CodingKeyRepresentable {
var codingKey: CodingKey {
MyCodingKey(stringValue: wrapped)
}
init?<T>(codingKey: T) where T : CodingKey {
self.wrapped = codingKey.stringValue
}
}
let test: [Wrapper: Int] = [
.init(wrapped: "a"): 1,
.init(wrapped: "b"): 2
]
let encoded = try! JSONEncoder().encode(test)
print("Encoded", String(data: encoded, encoding: .utf8))
Output:
expression failed to parse:
error: Couldn't lookup symbols:
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
protocol descriptor for Swift.CodingKeyRepresentable
1 Like
Regarding the Playground errors - this was of course due to running it on macOS that is not the latest beta. Switching to iOS (simulated) fixed those errors.
I know I ought to fix this issue myself, as this is basically my own bug
, but I don't really know where to start debugging the swift standard library.
The tests included with the implementation work, so the best guess I have about this not working are all the availability annotations that were needed to make this feature available only with swift stdlib 5.6.
Like: if #available(SwiftStdlib 5.6, *)
and
public protocol CodingKeyRepresentable {
@available(SwiftStdlib 5.6, *)
var codingKey: CodingKey { get }
@available(SwiftStdlib 5.6, *)
init?<T: CodingKey>(codingKey: T)
}
Might there be something about this that is not fully working?
Ok, I can see that the stdlib 5.6 availability checks in the standard library have been converted to availability checks for the special 99999 version (meaning ‘next release’ I guess).
On a beta release, shouldn’t these actually be replaced with the platform version of the beta?
Like iOS 15.4?
Not certain who to direct this question to - I’ll tag you, @tomerd since you were review manager for SE-0320
Followup: the error appeared to be incorrectly translated availability annotations and it’s fixed in the tool chain shipping with Xcode 13.3 beta 2. 
2 Likes