Repository: apple/swift
File: stdlib/public/core/NativeDictionary.swift (Swift)

Code before:
    func mapValues<T>(
      _ transform: (Value) throws -> T
    ) rethrows -> _NativeDictionary<Key, T>
  )
  internal func __rethrows_mapValues<T>(
    _ transform: (Value) throws -> T
  ) throws -> _NativeDictionary<Key, T> {
    try mapValues(transform)
  }
#endif

  @_alwaysEmitIntoClient
  internal mutating func merge<S: Sequence, E: Error>(
    _ keysAndValues: __owned S,
    isUnique: Bool,
    uniquingKeysWith combine: (Value, Value) throws(E) -> Value
  ) throws(E) where S.Element == (Key, Value) {
    var isUnique = isUnique
    for (key, value) in keysAndValues {
      let (bucket, found) = mutatingFind(key, isUnique: isUnique)
      isUnique = true
      if found {
        do {
          let newValue = try combine(unsafe uncheckedValue(at: bucket), value)
          unsafe _values[bucket.offset] = newValue
        } catch let error as _MergeError {
          switch error {
          case .keyCollision:
            #if !$Embedded
            fatalError("Duplicate values for key: '\(key)'")
            #else
            fatalError("Duplicate values for a key in a Dictionary")
            #endif
          }
        }
      } else {
        _insert(at: bucket, key: key, value: value)
      }
    }
  }

#if !$Embedded
  // ABI-only entrypoint for the rethrows version of merge, which has been
  // superseded by the typed-throws version. Expressed as "throws", which is
  // ABI-compatible with "rethrows".
  @_spi(SwiftStdlibLegacyABI) @available(swift, obsoleted: 1)
  @usableFromInline
  @abi(
    mutating func merge<S: Sequence>(
      _ keysAndValues: __owned S,
      isUnique: Bool,

Review comment (suggestion):
`combine(_:_:)` throws `E`, so this doesn't seem correct. The `do` statement above should be `do throws(E)`, but then there's nothing to throw `_MergeError`.

Code after:
      _ transform: (Value) throws -> T
    ) throws -> _NativeDictionary<Key, T>
  )
  @usableFromInline
  internal func __rethrows_mapValues<T>(
    _ transform: (Value) throws -> T
  ) throws -> _NativeDictionary<Key, T> {
    try mapValues(transform)
  }
#endif

  @_alwaysEmitIntoClient
  internal mutating func merge<S: Sequence, E: Error>(
    _ keysAndValues: __owned S,
    isUnique: Bool,
    uniquingKeysWith combine: (Value, Value) throws(E) -> Value
  ) throws(E) where S.Element == (Key, Value) {
    var isUnique = isUnique
    for (key, value) in keysAndValues {
      let (bucket, found) = mutatingFind(key, isUnique: isUnique)
      isUnique = true
      if found {
        let newValue = try combine(unsafe uncheckedValue(at: bucket), value)
        unsafe _values[bucket.offset] = newValue
      } else {
        _insert(at: bucket, key: key, value: value)
      }
    }
  }

  @_alwaysEmitIntoClient
  internal mutating func merge<S: Sequence>(
    trappingOnDuplicates keysAndValues: __owned S,
  ) where S.Element == (Key, Value) {
    var isUnique = true
    for (key, value) in keysAndValues {
      let (bucket, found) = mutatingFind(key, isUnique: isUnique)
      isUnique = true
      if found {
        #if !$Embedded
        fatalError("Duplicate values for key: '\(key)'")
        #else
        fatalError("Duplicate values for a key in a Dictionary")
        #endif
      } else {
        _insert(at: bucket, key: key, value: value)
      }
    }
  }

#if !$Embedded