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-12943] "Associated type references itself" when defining default value. #55389

Closed
swift-ci opened this issue Jun 5, 2020 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 5, 2020

Previous ID SR-12943
Radar None
Original Reporter Lantua (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: a3ae998d37de719ba38b7d2cee24e226

Issue Description:

This fails to compile,

struct Composite<Foo> { }
protocol Chain {
  associatedtype Next: Chain
  associatedtype Foo = Composite<Next.Foo> // Associated type `Foo` references itself.
}

But it shouldn't need to, since,

  1. conforming types can override `Foo`, and
  2. It's not referring directly to `Foo`, but `Next.Foo`.

This is one work around (from @filip-sakel over the Swift's forum):

typealias CompositeWithoutFoo<Value> = Composite<Value.Foo> where Value: Chain

protocol Chain {
  associatedtype Next: Chain
  associatedtype Foo = CompositeWithoutFoo<Next> // ✅ 
}

So either the checking is too restrictive, or the second code block should be illegal. Either way, this looks like a bug.

@jepers
Copy link

jepers commented Jun 5, 2020

Also, the following similar example will make the compiler get stuck in an infinite loop:

struct Composite<Foo> { }
protocol Chain where Foo = Composite<Next.Foo> {
  associatedtype Next: Chain
  associatedtype Foo
}

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@slavapestov
Copy link
Member

Fixed on main, adding a regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.
Projects
None yet
Development

No branches or pull requests

3 participants