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-5960] Decoding a struct with a RawRepresentable field fails when whole module optimization is enabled #4413

Open
swift-ci opened this issue Sep 22, 2017 · 2 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-5960
Radar None
Original Reporter broadway_lamb (JIRA User)
Type Bug
Environment

Apple Swift version 4.0 (swiftlang-900.0.65 clang-900.0.37)

Target: x86_64-apple-macosx10.9

Xcode: 9.0 (9A235)

macOS: 10.12.6 (16G29)

Additional Detail from JIRA
Votes 0
Component/s Compiler, Foundation
Labels Bug
Assignee None
Priority Medium

md5: ec21a6810f42adb139989ac6b511601d

relates to:

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

Issue Description:

Deserializing an object that has a field of a RawRepresentable type while the rawValue of that type is accessed in a function that is never called fails when whole module optimization is enabled.

I have two files in a module:

// Container.swift

public struct SomeRawRepresentable: RawRepresentable, Decodable {
    
    public let rawValue: String
    
    public init(rawValue: String) { self.rawValue = rawValue }
}

public struct Container: Decodable {
    public let identifier: SomeRawRepresentable
}
// NeverCalled.swift
private func neverCalled(someRawRepresentable: SomeRawRepresentable) {
    print(someRawRepresentable.rawValue)
}

As you can see, the neverCalled function is never called from inside the module, nor it is from outside. But moving it into the first file or removing it at all fixes the thing.

If I run the follwing with -O0, it passes. However, when I turn WMO on, it fails.

func testDecode() {
    
    let json = """
        {
            "Container": {
                "identifier": "IDENTIFIER",
            }
        }
        """.data(using: .utf8)!
    
    let decoder = JSONDecoder()
    
    do {
        let container = try decoder.decode([String : Container].self, from: json)["Container"]!
        
        XCTAssertEqual(container, "IDENTIFIER")
        
    } catch {
        // This path is executed only when WMO is enabled
        XCTFail("Couldn't decode:\n\n\(error)\n\n")
    }
}

I have created a test project that can be used to reproduce the issue:
Codable-WMO-bug

@belkadan
Copy link

Looks a lot like SR-5965 (that's what I get for reading these newest-to-oldest). @itaiferber?

@itaiferber
Copy link
Contributor

@belkadan Looking at these now, thanks!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
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

3 participants