Compiler crashes when using protocols with associated type

compiler
associatedtype
(Vito Matarazzo) #1

Hello everyone,
I have a project in which I created several protocols that have an associatedType, implemented by some generic classes. In particular, there is a protocol with a method requirement and an associatedType, and this type conforms to another protocol that has another associatedType. When I try to build the project, the compiler crashes, giving a Segmentation fault error when trying to build the method I mentioned before but without explaining the reason.
Of course the code is much more complex than this, but I managed to reproduce the issue in a short playground:

protocol C {
  associatedtype Type1
}

protocol B {
  associatedtype Type2: C
  
  func register()
}

extension B {
  func register() {
    print("Hello")
  }
}

class MyClass: C {
  typealias Type1 = Int
}

class MyGeneric<T>: B where T: C {
  typealias Type2 = T
}

let c = MyGeneric<MyClass>()

c.register()

I don't know what should be the issue in this code, but at least I think the compiler should report it.

(Thomas Krajacic) #2

The compiler can't report it because it crashes. So you have uncovered a bug in the compiler. :tada:

In that case please always file a bug at https://bugs.swift.org/.
Especially since you have done a fantastic job of distilling the problem down! That'll help really a lot.

1 Like
(Vito Matarazzo) #3

Thanks, I'll report the bug.

1 Like