swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.49k stars 10.35k forks source link

[SR-7817] RawRepresentable decodable encodable inits not work on release #50353

Open swift-ci opened 6 years ago

swift-ci commented 6 years ago
Previous ID SR-7817
Radar None
Original Reporter aznix (JIRA User)
Type Bug

Attachment: Download

Environment Xcode 9.3, swift 4.1
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, Codable, OptimizedOnly | |Assignee | None | |Priority | Medium | md5: 5410ae8f3b6ce75c8a2255e9815b5f7c

relates to:

Issue Description:

JSON string

{
    "id": 5
    "name": "Name"
}

Must be struct with type safe id

struct User: Hashable, Codable {
    let id: GenericID<User>
    let name: String
}

Codable actions not fire errors for below code on debug mode. But on release mode there are codable errors

struct GenericID<T>: RawRepresentable, Hashable, Codable {
    let rawValue: Int

    init(rawValue: Int) { self.rawValue = rawValue }
}

Adding codable custom inits fix errors

struct GenericID<T>: RawRepresentable, Hashable, Codable {
    let rawValue: Int

    init(rawValue: Int) { self.rawValue = rawValue }

    init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        rawValue = try container.decode(Int.self)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(rawValue)
    }
}
belkadan commented 6 years ago

I can't reproduce this with the following code:

struct GenericID<T>: RawRepresentable, Hashable, Codable {
    let rawValue: Int

    init(rawValue: Int) { self.rawValue = rawValue }
}

struct User: Hashable, Codable {
    let id: GenericID<User>
    let name: String
}

let json = """
{
    "id": 5,
    "name": "Name"
}
"""

import Foundation
print(try! JSONDecoder().decode(User.self, from: json.data(using: .utf8)!))

Any idea what I'm doing differently from you?

(Note that your JSON is missing a comma.)

swift-ci commented 6 years ago

Comment by Elshad Yarmetov (JIRA)

@belkadan thank you for your reply.

I also couldn't reproduce this in separate project. But in my project i have this problem. Below i put video link to problem.

RawRepresentable codable bug?

Is there can be some relation with this problem?
https://bugs.swift.org/browse/SR-7315

belkadan commented 6 years ago

Probably not; that problem kicks in before the optimizer gets a chance to run. Can you share your project? If you don't want to share it publicly, you can send it just to Apple at https://bugreport.apple.com.

belkadan commented 6 years ago

@itaiferber, is this SR-5965?

swift-ci commented 6 years ago

Comment by Elshad Yarmetov (JIRA)

yoxla-ios.zip

P.S. project needs Pod install