RangeExpression Frustrations

I am having some difficulty with RangeExpressions and Generic structures. I think I must be doing something wrong but for the life of me I cannot figure it out. I have simplified my code down to the following Playground which illustrates the problem.


Warning: For me this crashes XCode so please make sure you turn off autoplay before pasting this into a playground. Otherwise, you will be kinda stuck.


Anyways, here is the code in question (again, this doesn't do anything useful but it illustrates my issue):

import Foundation

struct TestStruct<T> {
  var data: [T] = []
}

extension TestStruct {
  subscript(n: Int) -> T {
    get {
      return(data[n])
    }
    set(newValue) {
      data[n] = newValue
    }
  }

  subscript<U: RangeExpression>(ns: U) -> TestStruct<T> where U.Bound == Int {
    get {
      let nRange = ns.relative(to:data)
      var ret = TestStruct<T>()
      for n in (nRange.lowerBound..<nRange.upperBound) {
        ret.data.append(data[n])
      }
      return(ret)
    }
  }
}

// The following 4 lines work beautifully and I'm really excited about
// open ended ranges in Swift 4. 
let arr = [7,6,5,4,3,2,1,0]
let x = arr[1...3]
let y = arr[..<3]
let z = arr[4...]


let a = TestStruct<Double>(data: [0.0, 1.2, 23.2, 42.0])
// This next line causes XCode to hard crash for me.
// I realize that 8 is out of bounds but I figured this would trigger
// a fatalerror when the "data[n]" is executed in the subscript.  Not so ???
// I haven't changed the optimization settings from the default debug options
let b = a[0...8]


Any help with my problem or a workaround if I have discovered a bug somehow would be most appreciated. These forums are extremely helpful and I appreciate all the experts contributing.


-An excited newbie

I tried your code in a test app, and it still crashes, but it logs the following:


Message from debugger: The LLDB RPC server has crashed. The crash log is located in ~/Library/Logs/DiagnosticReports and has a prefix 'lldb-rpc-server'. Please file a bug and attach the most recent crash log.

Fatal error: Index out of range


So the range error was correctly detected, but the debugger failed. This might be a bug in the debugger or in Swift (in generating debug info), but either way you should look in your user Library folder (at the indicated subpath) for the crash log, and submit a bug report.

Looks like a bug, not new....


See https://stackoverflow.com/questions/43635522/what-is-lldb-rpc-server-when-does-it-crash-in-xcode-why-it-crashes


If you take the time to file one, be sure to add your report # to your thread for reference, thanks and good luck.


Ken

I generally test weird edge cases in a macOS command line tool project rather than a playground (or an iOS app). Command line tools are the simplest possible runtime environment.

And lo! if I create a new command line tool project, paste in your code, and run it, I stop at line 22 with the expected error:

Thread 1: Fatal error: Index out of range

This is with Xcode 9.2 on macOS 10.12.6.

If you get other results in other contexts, that’s definitely bugworth IMO.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
RangeExpression Frustrations
 
 
Q