I looks like generics are required for the issue to show up:
// A protocol with an associated type
protocol PAT {
associatedtype U
func f() -> U
}
// ✅ Compiles
struct S1: PAT {
func f() -> String { "Hello" }
}
// ✅ Compiles
struct S2: PAT {
func f() -> some StringProtocol { "Hello" }
}
// ===========
// A protocol with a constrained associated type
protocol ConstrainedPAT {
associatedtype U: StringProtocol
func f() -> U
}
// ✅ Compiles
struct S3: ConstrainedPAT {
func f() -> String { "Hello" }
}
// ✅ Compiles
struct S4: ConstrainedPAT {
func f() -> some StringProtocol { "Hello" }
}
// ===========
// A protocol with an associated type and a generic requirement
protocol GenericPAT {
associatedtype U
func f<T>(t: T) -> U
}
// ✅ Compiles
struct S5: GenericPAT {
func f<T>(t: T) -> String { "Hello" }
}
// ❌ error: type 'S6' does not conform to protocol 'GenericPAT'
// struct S6: GenericPAT {
// ^
// note: protocol requires nested type 'U'; do you want to add it?
// associatedtype U
// ^
struct S6: GenericPAT {
func f<T>(t: T) -> some StringProtocol { "Hello" }
}