Swift 3 segmentation error 11


(Don Giovanni) #1

The following code generates a segmentation error:

      extension Int
        {
          static prefix func++(i:Int)->Int{
            
            return i+1;
          }
          
          static prefix func--(i:Int)->Int{
            return i-1;
          }
        }
        
        infix operator <-;
        
        class DynamicList<T>: RangeReplaceableCollection, MutableCollection, BidirectionalCollection
        {
          
          var length:Int;
          var arr:Array<T>?;
          var startIndex:Int{
            return 0;
          }
          var endIndex:Int{
          
            return length;
          }
          
          subscript(i:Int)->T{
            get{
              return arr![i];
            }
            set{
              arr![i] = newValue;
            }
          }
          
          func index(after i: Int) -> Int{
            return ++i;
          }
          
          func index(before i: Int) -> Int{
            return --i;
          }
          
          required init(){
            length = 0;
            
          }
          
          static func <- (left: inout DynamicList<T>, right: DynamicList<T>){
            
          }
          
          /* func replaceSubrange<C>(_ subrange: Range<Self.Index>, with newElements: C) where C : Collection,
            C.Iterator.Element == Iterator.Element */
          
          func replaceSubrange<C>(_ subrange: Range<DynamicList.Index>, with c: C)
          where C : Collection, C.Iterator.Element == DynamicList.Iterator.Element{
            
          }
          
        }

My intent here is to have the generic class `DynamicList<T>`

adopt three protocols:

`class DynamicList<T>: RangeReplaceableCollection, MutableCollection, BidirectionalCollection`

as indicated in the code above but it generates segmentation error.

However, when I replace `MutableCollection` with `Collection`, code above complies.

If I remove `BidirectionalCollection`, the code above compiles with no segmentation error.

Is it not possible to adopt all 3:

- RangeReplaceableCollection
- MutableCollection
- BidirectionalCollection

at the same time? Thanks.


(Ben Cohen) #2

Hi – I think you’re hitting a bug that was fixed recently. There was a missing default for this particular combination’s slice type, which then interacted quite unpleasantly with a compiler bug to cause a crash. Can you try it with the latest Xcode beta?

In the mean-time, adding the following to your declaration of your DynamicList ought to do as a workaround:

    typealias SubSequence = MutableRangeReplaceableBidirectionalSlice<DynamicList>

···

On Mar 10, 2017, at 3:32 PM, Don Giovanni via swift-users <swift-users@swift.org> wrote:

The following code generates a segmentation error:

     extension Int
       {
         static prefix func++(i:Int)->Int{
           
           return i+1;
         }
         
         static prefix func--(i:Int)->Int{
           return i-1;
         }
       }

       infix operator <-;

       class DynamicList<T>: RangeReplaceableCollection, MutableCollection, BidirectionalCollection
       {
         
         var length:Int;
         var arr:Array<T>?;
         var startIndex:Int{
           return 0;
         }
         var endIndex:Int{
         
           return length;
         }
         
         subscript(i:Int)->T{
           get{
             return arr![i];
           }
           set{
             arr![i] = newValue;
           }
         }
         
         func index(after i: Int) -> Int{
           return ++i;
         }
         
         func index(before i: Int) -> Int{
           return --i;
         }
         
         required init(){
           length = 0;
           
         }
         
         static func <- (left: inout DynamicList<T>, right: DynamicList<T>){
           
         }
         
         /* func replaceSubrange<C>(_ subrange: Range<Self.Index>, with newElements: C) where C : Collection,
           C.Iterator.Element == Iterator.Element */
         
         func replaceSubrange<C>(_ subrange: Range<DynamicList.Index>, with c: C)
         where C : Collection, C.Iterator.Element == DynamicList.Iterator.Element{
           
         }
         
       }

My intent here is to have the generic class `DynamicList<T>`

adopt three protocols:

`class DynamicList<T>: RangeReplaceableCollection, MutableCollection, BidirectionalCollection`

as indicated in the code above but it generates segmentation error.

However, when I replace `MutableCollection` with `Collection`, code above complies.

If I remove `BidirectionalCollection`, the code above compiles with no segmentation error.

Is it not possible to adopt all 3:

- RangeReplaceableCollection
- MutableCollection
- BidirectionalCollection

at the same time? Thanks.

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users