-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Description
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
- Explicitly discard the return value:
withAnimation {
_ = opacities.remove(at: 2) // ✅ Works
}
- Use multiple statements:
withAnimation {
opacities.remove(at: 2) // ✅ Works
opacities.remove(at: 2) // ✅ Works
}