Skip to content

Ambiguous use of 'remove(at:)' error when using Array.remove in nested closure with @discardableResult #86233

@Kyle-Ye

Description

@Kyle-Ye

Description

The Swift compiler incorrectly reports an "ambiguous use of 'remove(at:)'" error when calling Array.remove(at:) inside a nested closure structure (GCD dispatch + function with generic return type like withAnimation), even though the call is unambiguous.

Reproduction

Create a function with a generic Result return type that accepts a throwing closure (similar to SwiftUI's withAnimation)
Nest this function inside a GCD DispatchQueue.asyncAfter closure
Call Array.remove(at:) as a single statement without using the return value

import Dispatch

func withAnimation<Result>(_ body: () throws -> Result) rethrows -> Result {
    try body()
}

func test() {
    var opacities: [Double] = [0, 0.5, 1.0]
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        withAnimation {
            opacities.remove(at: 2) // ❌ Ambiguous use of 'remove(at:)'
        }
    }
}

Expected behavior

The code should compile successfully. The call to opacities.remove(at: 2) is unambiguous - it should resolve to Array.remove(at:) since opacities is of type [Double] and 2 is an Int.

Actual behavior

The compiler reports an ambiguous use error:

error: ambiguous use of 'remove(at:)'
   |             opacities.remove(at: 2) // ❌ Ambiguous use of 'remove(at:)'
   |                       `- error: ambiguous use of 'remove(at:)'

Swift.Array.remove:3:35: note: found this candidate in module 'Swift'
  |   @inlinable public mutating func remove(at index: Int) -> Element}
  |                                   `- note: found this candidate in module 'Swift'

Swift.RangeReplaceableCollection.remove:3:35: note: found this candidate in module 'Swift'
  |   @inlinable public mutating func remove(at position: Self.Index) -> Self.Element}
  |                                   `- note: found this candidate in module 'Swift'

Environment

Tested under Xcode 16.4 & Xcode 26.2

Xcode 16.4

swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
Target: arm64-apple-macosx15.0

Xcode 26.2

swift-driver version: 1.127.14.1 Apple Swift version 6.2.3 (swiftlang-6.2.3.3.21 clang-1700.6.3.2)
Target: arm64-apple-macosx15.0

Additional information

Workarounds

  1. Explicitly discard the return value:
withAnimation {
    _ = opacities.remove(at: 2) // ✅ Works
}
  1. Use multiple statements:
withAnimation {
    opacities.remove(at: 2) // ✅ Works
    opacities.remove(at: 2) // ✅ Works
}

Forums discussion

https://forums.swift.org/t/ambiguous-use-of-remove-at-for-array-remove/83875

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions