Skip to content

[SR-8686] Compiler should not require restatement of associatedtype fully specified in where clause utilizing new associatedtype #51199

@swift-ci

Description

@swift-ci
Previous ID SR-8686
Radar None
Original Reporter neightchan (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AssociatedTypeInference
Assignee @slavapestov
Priority Medium

md5: 80d1b8504026d92243c04a3f3af13173

Issue Description:

The following code compiles without issue:

protocol B {
    associatedtype A
}

protocol D : B where A == Int {}

enum C : D {}

In particular, the conformance of C to the derived protocol D is not burdened by having to write out the definition of the associatedtype A because it is specified in the where clause. That is, you do not have to write

enum C : D {
    typealias A = Int
}

However, when adding an associatedtype AP to the derived protocol D and constraining the original associatedtype A to be equal to [an expression of] AP, that burden is imposed, despite the fact that, as in the previous case, there is only one possible value. That is, the following does not compile:

protocol B {
    associatedtype A
}

protocol D : B where A == AP {
    associatedtype AP
}

enum C : D {
    typealias AP = Int
}

and we are forced to define C.A despite the fact that there is only one possible definition. That is, we are forced to write:

enum C : D {
    typealias A = Int
    typealias AP = Int
}

Though any other definition for A is illegal (and will cause a compilation error).

That is a particular nuisance when the definition for A is an expression of the new associatedtype AP as in the compilation error raised by following

protocol ZeroProtocol {
    associatedtype CanonicalRepresentation : ZeroProtocol
}

struct Successor<Value : ZeroProtocol> : NumberTypeProtocol {
    typealias CanonicalRepresentation = Successor<
        Value.CanonicalRepresentation
    >

    typealias Predecessor = Value
}

protocol NumberTypeProtocol : ZeroProtocol 
where CanonicalRepresentation == Successor<Predecessor.CanonicalRepresentation> 
{
    associatedtype Predecessor : ZeroProtocol
}

struct Zero : ZeroProtocol {
    typealias CanonicalRepresentation = Zero
}

struct One : NumberTypeProtocol {
    typealias Predecessor = Zero
}

which requires that One specify CanonicalRepresentation to be Successor<Predecessor.CanonicalRepresentation> despite the fact that that is the only definition.

Metadata

Metadata

Assignees

Labels

associated type inferencebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconformancesFeature → protocol: protocol conformancesgeneric constraintsFeature → generics: generic constraintsgenericsFeature: generic declarations and typesswift 5.10type checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions