Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
swift-ci opened this issue May 31, 2018 · 5 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. Codable Area → standard library: `Codable` and co. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation

Comments

@swift-ci
Copy link
Collaborator

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:

  • SR-5965 Unexpected type-mismatch errors when parsing JSON in Release configuration

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
Copy link
Contributor

belkadan commented Jun 1, 2018

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
Copy link
Collaborator Author

swift-ci commented Jun 1, 2018

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
Copy link
Contributor

belkadan commented Jun 2, 2018

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
Copy link
Contributor

belkadan commented Jun 2, 2018

@itaiferber, is this SR-5965?

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 2, 2018

Comment by Elshad Yarmetov (JIRA)

yoxla-ios.zip

P.S. project needs Pod install

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. Codable Area → standard library: `Codable` and co. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation
Projects
None yet
Development

No branches or pull requests

2 participants