[swift-users] Crash in generic struct

Ryan Lovelett swift-dev at ryan.lovelett.me
Wed May 4 10:02:41 CDT 2016


On Wed, May 4, 2016, at 10:41 AM, Jan E. Schotsman via swift-users
wrote:
> Hello,
> 
> This code causes a segmentation fault:
> 
> struct MyHeapElement<T:Comparable>
> 	{
> 	var index:Int
> 	var	key:T
> 	}
> 
> struct MyHeap<T:Comparable>
> 	{
> 	var elements = [MyHeapElement<T>]()
> 	}
> 
> extension MyHeap
> 	{
> 	init( withElements elements:[MyHeapElement<T>] )
> 		{
> 		self.elements = elements
> 		}
> 	}
> 
> Do I have a syntax error somewhere or is this a compiler bug (apart  
> from that ideally the compiler should never crash)?
> 
> Swift 2.1-2.2
> 
> Jan E.
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

Drop the extension and move the initializer to the MyHeap struct and it
compiles for me on Swift 3.0. To me that indicates the answer to your
first question: I would think you do not have a syntax error. That is:

struct MyHeapElement <T:Comparable> {
  var index: Int
  var key: T
}

struct MyHeap <T:Comparable> {
  var elements = [MyHeapElement<T>]()

  init(withElements elements: [MyHeapElement<T>]) {
    self.elements = elements
  }
}

Regardless of whether it is or not you should file the bug at
bugs.swift.org. As you say the compiler should never crash.


More information about the swift-users mailing list