Skip to content

Type of expression is ambiguous without a type annotation when using Combine publisher #81023

Closed
@theblixguy

Description

@theblixguy

Description

I get a Type of expression is ambiguous without a type annotation error when using the following code:

Reproduction

(Originally written for Combine, but have isolated it into an example that works without needing Combine. If you want to test with Combine, just remove my custom protocols and structs and change to AnyPublisher and Just)

protocol MyPublisher {
  associatedtype Output
  associatedtype Failure: Error
  func eraseToAnyPublisher() -> MyAnyPublisher<Output, Failure>
}

extension MyPublisher {
  func eraseToAnyPublisher() -> MyAnyPublisher<Output, Failure> {
    fatalError()
  }
}

struct MyAnyPublisher<Output, Failure: Error>: MyPublisher {}
struct MyJust<Output>: MyPublisher {
  typealias Failure = Never
  init(_ value: Output) {}
}

extension MyPublisher where Output == (any Collection)? {
  func mapCount() -> MyAnyPublisher<Int, Failure> { fatalError() }
}

let myPub: MyAnyPublisher<[Int]?, Never> = MyJust([1, 2, 3] as [Int]?)
  .eraseToAnyPublisher()

myPub
  .mapCount() // Type of expression is ambiguous without a type annotation

This compiles though:

let myPub = MyJust([1, 2, 3] as [Int]?)
  .eraseToAnyPublisher()
  .mapCount()

Expected behavior

The code either compiles or gives a descriptive error message.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Xcode: 16.3 (16E140)
macOS: 15.4 (24E248)

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.existentialsFeature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased valuesextensionFeature → declarations: `extension` declarationsgeneric where clausesFeature → generics: generic where clausestype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions