swiftlang / swift

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

"Unsupported recursion for reference to type alias" compiler error when protocol implementation declares array of associatedtype #77640

Open nickasd opened 1 day ago

nickasd commented 1 day ago

Description

When a protocol implementation uses a type that has the same name as the protocol's associatedtype, changing the protocol's definition from var b: B? to var b: [B] suddenly causes a compiler error.

Reproduction

protocol A {
    associatedtype B
//    var b: B? { get set }
    var b: [B] { get set }
}

class AImpl: A { //  error: Type 'AImpl' does not conform to protocol 'A'
//    var b: B?
    var b = [B]() // error: Unsupported recursion for reference to type alias 'B' of type 'AImpl'
}

struct B {
}

Note that this problem wouldn't happen when renaming struct B to something different, like struct BImpl.

Expected behavior

Either no error, or the same error as when changing var b: [B] to var b: B? (see commented out lines).

Environment

swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4) Target: arm64-apple-macosx15.0

Additional information

No response