Skip to content

[SR-726] Can't access associated types through class-constrained generic parameters #43341

@swift-ci

Description

@swift-ci
Previous ID SR-726
Radar None
Original Reporter jaspa (JIRA User)
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee jaspa (JIRA)
Priority Medium

md5: 28453193dbd999bd7c7c32caa1cb0e83

Issue Description:

Given a protocol P with an associated type A, a class C conforming to P and a function f with an generic parameter T constrained to C, it is not possible to access A through through T (T.A) inside the body of f.
In the function signature though, T.A works as expected (for example as a return type).

Example:

protocol P { associatedtype A }
class C : P { typealias A = Int }
func foo<T : C>(x: T) {
  let a: T.A? = nil
}

Compiler output:

<stdin>:4:12: error: ambiguous type name 'A' in 'T'
  let a: T.A? = nil
         ~ ^
<stdin>:2:25: note: found candidate with type 'A' (aka 'Int')
class C : P { typealias A = Int }
                        ^
<stdin>:1:29: note: found candidate with type '<<error type>>'
protocol P { associatedtype A }
                            ^

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselftype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions