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-10627] Source-breaking change in associated type inference #53027

Open
stephentyrone opened this issue May 6, 2019 · 3 comments
Open

[SR-10627] Source-breaking change in associated type inference #53027

stephentyrone opened this issue May 6, 2019 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression source compatibility swift 5.0

Comments

@stephentyrone
Copy link
Member

Previous ID SR-10627
Radar None
Original Reporter @stephentyrone
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 5.0Regression, SourceCompatibility
Assignee None
Priority Medium

md5: a973e6979fc1e019488712b770c5752f

Issue Description:

(Copied from https://forums.swift.org/t/never-ending-compilation-involving-types-and-pats/24200)

public enum OneOf2<T1, T2> {
    case t1(T1), t2(T2)
}

// MARK: - Protocols

public protocol _OneOf2Type {
    associatedtype T1; associatedtype T2
    static func value(_ v: T1) -> Self
    static func value(_ v: T2) -> Self
}

// MARK: - _OneOf_Type

extension OneOf2: _OneOf2Type {
    public static func value(_ v: T1) -> OneOf2<T1, T2> { return .t1(v) }
    public static func value(_ v: T2) -> OneOf2<T1, T2> { return .t2(v) }
}

The weird thing is that these are the following compilation results (in order that I tried them):

  • On Xcode 10.1 using Xcode 10.1 toolchain: code compiles nicely;

  • On Xcode 10.2 using Xcode 10.2 toolchain: code never finishes compiling (and spent few hours trying to minimize, compile and recompile the code);

  • On Xcode 10.2 using Xcode 10.1 toolchain: code doesn't compile and I get compilation errors (which actually led me to a solution below);

Using explicit typealiases resolves the problem:

extension OneOf2: _OneOf2Type {
    typealias T1 = T1
    typealias T2 = T2
    public static func value(_ v: T1) -> OneOf2<T1, T2> { return .t1(v) }
    public static func value(_ v: T2) -> OneOf2<T1, T2> { return .t2(v) }
}
@stephentyrone
Copy link
Member Author

On master, I observe that this produces an error:

sr-10627.swift:15:1: error: type 'OneOf2<T1, T2>' does not conform to protocol '_OneOf2Type'
extension OneOf2: _OneOf2Type {
^
sr-10627.swift:8:18: note: ambiguous inference of associated type 'T1': 'T1' vs. 'T2'
  associatedtype T1; associatedtype T2
                 ^

@belkadan
Copy link
Contributor

belkadan commented May 6, 2019

I'm inclined to say master is correct here.

@stephentyrone
Copy link
Member Author

I agree, but we should be clear that it's a decision we're making.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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. compiler The Swift compiler in itself regression source compatibility swift 5.0
Projects
None yet
Development

No branches or pull requests

3 participants