 /// Ideally instances of this class would be allocated in-line in the
 /// buffers used for Array storage.
 @objc internal final class _SwiftDeferredNSArray
-  : _SwiftNativeNSArray, _NSArrayCoreType {
+  : _SwiftNativeNSArrayWithContiguousStorage {
 
   // This stored property should be stored at offset zero.  We perform atomic
   // operations on it.

    while true
  }

  //
  // NSArray implementation.
  //
  // Do not call any of these methods from the standard library!
  //

  @objc internal var count: Int {
    // Note: calling this function should not trigger bridging of array
    // elements, there is no need in that.
    if let bridgedStorage = _heapBufferBridged {
      return _HeapBuffer(bridgedStorage).value
    }

@transparent
internal func _isValidArraySubscript(index: Int, count: Int) -> Bool {
  return (index >= 0) & (index < count)
}

/// An `NSArray` with Swift-native reference counting and contiguous
/// storage.
class _SwiftNativeNSArrayWithContiguousStorage
  : _SwiftNativeNSArray { // provides NSArray inheritance and native refcounting

  // Operate on our contiguous storage
  internal func withUnsafeBufferOfObjects<R>(
    body: (UnsafeBufferPointer<AnyObject>)->R
  ) -> R {
    _sanityCheckFailure(
      "Must override withUnsafeBufferOfObjects in derived classes")
  }
}

// Implement the APIs required by NSArray 
extension _SwiftNativeNSArrayWithContiguousStorage: _NSArrayCoreType {
  @objc internal var count: Int {
    return withUnsafeBufferOfObjects { $0.count }
  }

  @objc internal final func objectAtIndex(index: Int) -> AnyObject {
    return withUnsafeBufferOfObjects {
      objects in
      _precondition(
        _isValidArraySubscript(index, objects.count),
        "Array index out of range")
      return objects[index]
    }
  }

  @objc internal func getObjects(
    aBuffer: UnsafeMutablePointer<AnyObject>, range: _SwiftNSRange
  ) {
    return withUnsafeBufferOfObjects {
      objects in
      _precondition(
        _isValidArrayIndex(range.location, objects.count),
        "Array index out of range")

      _precondition(
        _isValidArrayIndex(
          range.location + range.length, objects.count),
        "Array index out of range")

      // These objects are "returned" at +0, so treat them as values to
      // avoid retains.
      UnsafeMutablePointer<Word>(aBuffer).initializeFrom(
        UnsafeMutablePointer(objects.baseAddress + range.location),
        count: range.length)
    }
  }

  @objc internal func countByEnumeratingWithState(
    state: UnsafeMutablePointer<_SwiftNSFastEnumerationState>,
    objects: UnsafeMutablePointer<AnyObject>, count: Int
  ) -> Int {
    var enumerationState = state.memory

    if enumerationState.state != 0 {
      return 0
    }

    return withUnsafeBufferOfObjects {
      objects in
      enumerationState.mutationsPtr = _fastEnumerationStorageMutationsPtr
      enumerationState.itemsPtr = unsafeBitCast(
        objects.baseAddress,
        AutoreleasingUnsafeMutablePointer<AnyObject?>.self)
      enumerationState.state = 1
      state.memory = enumerationState
      return objects.count
    }
  }

  @objc internal func copyWithZone(_: _SwiftNSZone) -> AnyObject {
    return self
  }
}

/// An `NSArray` whose contiguous storage is created and filled, upon
