Add subscript with unchecked index to Array

That’s a very good point, and also applies to overflow checks. I’ve seen cases where LLVM has specific logic to recognise particular algorithms and code patterns, but they don’t get optimised in Swift because of overflow checks that aren’t eliminated.

Whenever I experiment with -Ounchecked in my code, I’m not normally concerned with the checks themselves, but how they affect the compiler’s understanding of my code more broadly and which other optimisations suddenly become possible.

1 Like

If this gets added maybe it could still check when compiled in debug mode:

extension Array {
  subscript(unsafeUnchecked index: Index) -> Element {
           "\(index) of bounds [WARNING: this access is not checked in release mode and would have undefined behavior.]")
    self.withUnsafeBufferPointer { $0[index] }

Edit: looks like UnsafeBufferPointer already does this in its subscript, so this is mostly redundant except for the different error message.

1 Like