swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.58k stars 10.36k forks source link

[SR-15807] Associated Type Inference fails across module boundaries (Xcode 13.3 RC) #58084

Open robinkunde opened 2 years ago

robinkunde commented 2 years ago
Previous ID SR-15807
Radar None
Original Reporter @robinkunde
Type Bug
Environment Xcode 13.3 b1 (13E5086k) Apple Swift version 5.6 (swiftlang-5.6.0.320.8 clang-1316.0.18.8) Xcode 13.3 b2 (13E5095k) Xcode 13.3 b3 (13E5104i) Xcode 13.3 RC (13E113)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 5 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 6d760c772c45a460ba5f2eba3f9ee36b

Issue Description:

The following code compiles in Xcode 13.2 but not 13.3b1, 13.3b2, 13.3b3 nor 13.3 RC.

Module A will compile, but Module B will not and fail with the following error:

ModuleB.swift:11:8: type 'ModuleBFoo' does not conform to protocol 'DefaultsSerializable'

ModuleA.swift:12:20: ambiguous inference of associated type 'Bridge': 'BridgeStructSerializable<ModuleBFoo>' vs. 'BridgeStructRawRepresentable<ModuleBFoo>'

ModuleA.swift:18:16: matching requirement '_defaults' to this declaration inferred associated type to 'BridgeStructSerializable<ModuleBFoo>'

ModuleA.swift:22:16: matching requirement '_defaults' to this declaration inferred associated type to 'BridgeStructRawRepresentable<ModuleBFoo>'

Note that `ModuleAFoo` does not run into the same issue.

(Also, Xcode will fail to surface the underlying issue in the Issue Navigator most of the time and just show "type 'ModuleBFoo' does not conform to protocol 'DefaultsSerializable'")

Module A

public protocol DefaultsBridge {
    associatedtype T
}

public struct BridgeStructSerializable<T: Codable>: DefaultsBridge {
}

public struct BridgeStructRawRepresentable<T: RawRepresentable>: DefaultsBridge {
}

public protocol DefaultsSerializable {
    associatedtype Bridge: DefaultsBridge

    static var _defaults: Bridge { get }
}

public extension DefaultsSerializable where Self: Codable {
    static var _defaults: BridgeStructSerializable<Self> { return BridgeStructSerializable() }
}

public extension DefaultsSerializable where Self: RawRepresentable {
    static var _defaults: BridgeStructRawRepresentable<Self> { return BridgeStructRawRepresentable() }
}

public struct ModuleAFoo: Codable, DefaultsSerializable {
}

Module B

import Foundation
import MyLibrary

struct ModuleBFoo: Codable, DefaultsSerializable {
}
swift-ci commented 2 years ago

Comment by Alfons (JIRA)

I also see this error appear in Xcode 13.2.1 (type Xyz does not conform to protocol Zyx), but only for "release" builds, i.e. suggestive of compiler settings; hard to reproduce right now.
(And only for protocols that define `static` property requirements).

sindresorhus commented 2 years ago

We're also seeing this issue: https://github.com/sindresorhus/Defaults/issues/93

sindresorhus commented 2 years ago

Still reproducible in Xcode 13.3 final...

swift-ci commented 2 years ago

Comment by Weston Mitchell (JIRA)

I am also seeing this error in Xcode 13.3 final

swift-ci commented 2 years ago

Comment by JB Parrett (JIRA)

Ditto above comments - seeing the error on Xcode 13.3. Have reverted back to 13.2.1 in order to continue development.

swift-ci commented 2 years ago

Comment by Ky (JIRA)

I'm still having this issue in Xcode Version 13.3.1 (13E500a)

Mordil commented 2 years ago

This still reproduces w/ Swift 5.7 and Xcode 14.0.1 (14A400)