Never ending compilation involving types and PATs

I have following code that compiles in Swift 4.2, but doesn't in Swift 5.0:

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) }
}

It seems that somehow generic argument T1 in OneOf2 doesn't tie-up to _OneOf2Type.T1.

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);

This is the change that is required for the code to compile in Xcode 10.2 with Swift 5 (a.k.a. Xcode 10.2 toolchain):

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) }
}

Just wondering if this is by design and expected change in Swift 5.

Best!
Eimantas :slight_smile:

This is the sort of thing that's generally best reported as a bug rather than as a forum post. I went ahead and wrote one up for you, please feel free to follow-up there.

Hi Steve,

Thanks for quick response! I didn't file a bug there just in case it wasn't a bug and was an evolutionary feature :slight_smile:

Best,
Eimantas

Source breaks are occasionally intentional, but the default assumption should be that they are bugs =)

FWIW, this seems to produce a semi-useful error on master:

sr-10627.swift:8:18: note: ambiguous inference of associated type 'T1': 'T1' vs. 'T2'

Yep, I got something similar when compiling on Xcode 10.2 with Swift4.2 toolchain. That's how I got to explicit typealiasing workaround.

And noted the source breakage in future releases. I'll report bugs directly.

1 Like
Terms of Service

Privacy Policy

Cookie Policy