Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11868] Unable to rename protocol associatedtype through inheritance #54282

Open
swift-ci opened this issue Nov 28, 2019 · 2 comments
Open
Labels
compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances generics Feature: generic declarations and types improvement type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11868
Radar rdar://problem/57549603
Original Reporter ral (JIRA User)
Type Bug
Environment

MacOS 10.15, XCode 11.2.

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, AssociatedTypeInference
Assignee None
Priority Medium

md5: 5c2da9f9df69e7952aa7afdd24d2008a

Issue Description:

If I have protocol Old with associatedtype OldType, I may want to define protocol New with associatedtype NewType that inherits Old and provides NewType for OldType. My first attempt at this fails unexpectedly:

protocol Old {
    associatedtype OldType
}
protocol New: Old {
    associatedtype NewType
    typealias OldType = NewType // Warning
}
class Impl: New { // Error
    typealias NewType = String
}

There's both a warning:

Typealias overriding associated type 'OldType' from protocol 'Old' is better expressed as same-type constraint on the protocol

and Impl fails to compile with

Type 'Impl' does not conform to protocol 'New' Type 'Impl' does not conform to protocol 'Old'

This appears to work:

protocol Old {
    associatedtype OldType
}
protocol New: Old where OldType == NewType {
    associatedtype NewType
}
extension New {
    typealias OldType = NewType // Warning
}
class Impl: New {
    typealias NewType = String
}

except there is the same warning as above in the extension and the extension itself should be unnecessary. That makes me think the first approach should work, and the warning is incorrect since there is already a where clause on the protocol.

@beccadax
Copy link
Contributor

beccadax commented Dec 2, 2019

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added conformances Feature → protocol: protocol conformances type checker Area → compiler: Semantic analysis generics Feature: generic declarations and types improvement and removed associated type inference bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. labels Jun 6, 2022
@AnthonyLatsis
Copy link
Collaborator

The conformance failure is gone, and we already have tests for

  • The introduction of a same-type constraint for a protocol typealias that overrides an associated type, here
  • Associated type inference through same-type constraints in simple scenarios like the one shown above, here

The warning could be upgraded to point out the redundancy when the constraint is already in place, so I am leaving the issue open and labeling it as an improvement.

@AnthonyLatsis AnthonyLatsis added the compiler The Swift compiler in itself label Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself conformances Feature → protocol: protocol conformances generics Feature: generic declarations and types improvement type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants