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-10045] Codable synthesis fails when instance property and static property share the same name #52448

Closed
swift-ci opened this issue Mar 5, 2019 · 6 comments
Assignees
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

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 5, 2019

Previous ID SR-10045
Radar rdar://problem/48702779
Original Reporter dlbuckley (JIRA User)
Type Bug
Status Closed
Resolution Done
Environment

Originally found on Swift 4.2.2 running on Ubuntu 18.04

Reproduced on Swift 4.2 running on macOS 10.14.3

Reproduced on Swift 5 beta 4 running on macOS 10.14.3

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, Codable
Assignee @beccadax
Priority Medium

md5: 5dcbaff73a6035f1ffe58c76cc81c5b1

Issue Description:

Details

Variation 1

Given the following code:

import Foundation

struct MyStruct: Decodable {
    static let name = "aaa"
    let _id: String?
    let name: PersonNameComponents?
}

let decoded = try JSONDecoder().decode(MyStruct.self, from: "{\"_id\": \"abc123\", \"name\": { \"givenName\": \"John\"}}".data(using: .utf8)!)

print(decoded.name!)

The following error is produced:

Swift5.playground:6:9: note: 'self.name' not initialized
    let name: PersonNameComponents?
        ^

Swift fails to understand that Decodable synthesis should only take into account instance property names and should ignore static property names. By having an instance property and static property with the same name, it confuses the compiler into not adding the Decodable synthesis for that particular property.

If the static property name is changed then Decodable synthesis works correctly.

Variation 2

Given the following code (note the instance property is now a var instead of a let):

import Foundation

struct MyStruct: Decodable {
    static let name = "aaa"
    let _id: String?
    var name: PersonNameComponents?
}


let decoded = try JSONDecoder().decode(MyStruct.self, from: "{\"_id\": \"abc123\", \"name\": { \"givenName\": \"John\"}}".data(using: .utf8)!)


print(decoded.name)

The following output is produced:

nil

This is arguably a worse result as it is not found at compile time and only found at run time. This means that code that should work at first glance and that passes compilation doesn't produce the expected output.

Expected Result

I would expect that the struct conformance to Decodable would be correctly synthesised and the static property name would be ignored. Failing that I would at least expect the decoding of the instance property to throw an error at runtime if it manages to compile, but this isn't the case.

The most desirable result would for Decodable synthesis to be correctly applied.

Workaround

To circumvent the issue, add in manual support of Decodable.

required init(decoder: Decoder) throws { ... }
@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Mar 8, 2019

+ @itaiferber

@beccadax
Copy link
Contributor

#23252

@beccadax
Copy link
Contributor

Fix is in master and, barring unforeseen circumstances, should be in Swift 5.1.

@swift-ci
Copy link
Collaborator Author

Comment by Dale Buckley (JIRA)

Amazing brentdax (JIRA User), great work

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Aug 9, 2019

dlbuckley (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 commented Aug 9, 2019

Comment by Dale Buckley (JIRA)

@AnnaZaks I have verified this is fixed in Xcode 11 Beta 5 (11M382q) running Swift 5.1. Thanks for the speedy turn around on this brentdax (JIRA User).

@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
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
Projects
None yet
Development

No branches or pull requests

2 participants