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-8083] Inherited Codable initialiser behaviour appears broken in Swift 4.2 #50616

Closed
swift-ci opened this issue Jun 22, 2018 · 11 comments
Closed

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Jun 22, 2018

Previous ID SR-8083
Radar rdar://problem/41426211
Original Reporter marcpalmer (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Xcode 10 beta 1 and 2.

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, 4.2Regression, Miscompile
Assignee @belkadan
Priority Medium

md5: 0d101064449b56477faf3d132af3b5a4

Issue Description:

It seems there is a regression in Swift 4.2 where subclasses that inherit automatic codable conformance do no construct the correct type or something else is a little bit off, but in a pretty scary way.

Take the following playground:

//: Playground - noun: a place where people can play

import UIKit

/// This type is just to help demonstrate the point in isolation
class DummyDecoder: Decoder {
    var codingPath: [CodingKey] = []
    var userInfo: [CodingUserInfoKey : Any] = [:]
    enum DummyError: Error {
        case fake
    }
  
    func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
        throw DummyError.fake
    }
    
    func unkeyedContainer() throws -> UnkeyedDecodingContainer {
        throw DummyError.fake
    }
    
    func singleValueContainer() throws -> SingleValueDecodingContainer {
        throw DummyError.fake
    }
}

let decoder = DummyDecoder()

/// Here are the actual problematic types

class A: Codable {
    var thing: String { return "Abstract" }
}

class B: A {
    override var thing: String { return "Yo" }
}

let b: B = try! B.init(from: decoder)
b is B // true
b.thing // "Abstract"

let b2 = B.init()
b2 is B // true
b2.thing // "Yo"

On Xcode 10 beta 1 and 2 this gives the output shown in the comments - basically polymorphism is broken on B when decoded.

When run on Xcode 9.4, it works as expected and outputs "Yo" in both cases. Hat tip to @robmaceachern for the isolated case.

Note that adding a "no-op" empty init(from: ) that calls try super.init(from: ) works around the problem.

@belkadan
Copy link
Contributor

@belkadan belkadan commented Jun 25, 2018

Yikes! Thanks for reporting.

@swift-ci create

@belkadan
Copy link
Contributor

@belkadan belkadan commented Jul 3, 2018

@belkadan
Copy link
Contributor

@belkadan belkadan commented Jul 4, 2018

4.2: #17732

@AnnaZaks
Copy link
Mannequin

@AnnaZaks AnnaZaks mannequin commented Aug 4, 2018

marcpalmer (JIRA User), Could you verify if the problem is fixed and if so move the JIRA to "Closed"?
Thanks!
Anna

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Aug 4, 2018

Comment by Marc Palmer (JIRA)

Definitely will check as soon as I get back from vacation! Many thanks for fixing.

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Aug 13, 2018

Comment by Marc Palmer (JIRA)

@AnnaZaks how do I know which Xcode 10 beta includes this fix? I am unsure how to determine this in order to validate the fix. Xcode 10 beta 5 is from some weeks ago, but how can I be sure it includes this change from 3rd July?

@belkadan
Copy link
Contributor

@belkadan belkadan commented Aug 13, 2018

There's no way to tell from just the GitHub repo, but Anna waited to ask her question until the fix was in a beta. In this case it was actually in beta 4.

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Aug 16, 2018

Comment by Marc Palmer (JIRA)

Thanks @belkadan I’ll try to retest my clients app minus workarounds tomorrow

@AnnaZaks
Copy link
Mannequin

@AnnaZaks AnnaZaks mannequin commented Sep 13, 2018

marcpalmer (JIRA User), Could you confirm that the fix works for you and "close" the JIRA?

Thanks!
Anna

@AnnaZaks
Copy link
Mannequin

@AnnaZaks AnnaZaks mannequin commented Oct 24, 2018

Marc Palmer,

Could you confirm that the fix works for you and "close" the JIRA?

Thanks!
Anna

@swift-ci
Copy link
Collaborator Author

@swift-ci swift-ci commented Oct 25, 2018

Comment by Marc Palmer (JIRA)

Thank you so much for fixing this! Apologies for the delay verifying.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants