Skip to content

Remove overlapping exclusive accesses in encoders #10819

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

Conversation

itaiferber
Copy link
Contributor

What's in this pull request?
{JSON,Plist}{Encoder,Decoder} had overlapping accesses which were supposed to be mutually exclusive in their with(pushedKey:) methods.

Removes those methods and the overlapping accesses.

{JSON,Plist}{Encoder,Decoder} had overlapping accesses which were supposed to be mutually exclusive in their `with(pushedKey:)` methods.

Removes those methods and the overlapping accesses.
@itaiferber itaiferber requested review from parkera and phausler July 7, 2017 20:45
@itaiferber
Copy link
Contributor Author

@swift-ci Please test macOS

@itaiferber
Copy link
Contributor Author

@swift-ci Please smoke test Linux

@itaiferber itaiferber merged commit acadd6c into swiftlang:master Jul 7, 2017
@itaiferber itaiferber deleted the encoders-eliminate-overlapping-exclusive-accesses branch July 7, 2017 23:08
self.currentIndex += 1
return decoded
guard let decoded = try self.decoder.unbox(self.container[self.currentIndex], as: Int.self) else {
throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath + [_JSONKey(index: self.currentIndex)], debugDescription: "Expected \(type) but found null instead."))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks DecodingError.valueNotFound has duplicated _JSONKey(index: self.currentIndex) in codingPath.
Reproducing code:

import Foundation

struct A: Encodable {
    var field1: String?

    public func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        try container.encode(field1)
    }
}

struct B: Decodable {
    var field1: Int = 0

    public init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        field1 = try container.decode(Int.self)
    }
}

do {
    let b = try JSONDecoder().decode(B.self, from: JSONEncoder().encode(A()))
    print(b)
} catch {
    print(error)
}

valueNotFound(Swift.Int, Swift.DecodingError.Context(codingPath: [Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: "Index 0", intValue: Optional(0)), Foundation.(_JSONKey in _12768CA107A31EF2DCE034FD75B541C9)(stringValue: "Index 0", intValue: Optional(0))], debugDescription: "Expected Int but found null instead.", underlyingError: nil))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@norio-nomura Thanks for letting me know! I'll look into it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@norio-nomura What compiler version/Xcode version are you using? I see a different error on my end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, nevermind — I was on the wrong Swift version myself. I get the same result as you do. I'll have to hunt down where we append the key to the coding path twice

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key is appended at the line that I commented on.
Same codes exist on each decode(_:) methods of _JSONUnkeyedDecodingContainer and _PlistUnkeyedDecodingContainer.

@jrose-apple
Copy link
Contributor

If you wanted to keep this with thing you could have the callback take an inout Encoder and then use $0 inside. But this works too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants