Skip to content

[SR-898] Unresolvable "ambiguous for type lookup" error when using multiple modules #43510

Closed

Description

@swift-ci
Previous ID SR-898
Radar rdar://problem/19481048
Original Reporter mkadijk (JIRA User)
Type Bug
Environment

Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81) / Xcode Version 7.2.1 (7C1002) / iOS 9.2

Additional Detail from JIRA
Votes 18
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: ea3a4fb46f14a67b99d4cb07142d5803

blocks:

  • SR-14195 Swift emits an invalid module interface when a public type has the same name as a module

Issue Description:

Given two modules with the same type, where one module has a type that has the same name as the module itself with generic parameters it will become impossible to explicitly refer to the type you want.

Example

Module A:

struct NoError: ErrorType {}

Module B:

struct B<T> {}
struct NoError: ErrorType {}

The app:

import A
import B

let ambiguousError: NoError // This correctly errors because it's ambiguous
let errorA: A.NoError // This correctly works since we make the module explicit
let errorB: B.NoError // This _incorrectly_ errors, complaining about required arguments

Expected behaviour

Only ambiguousError should give an error, the other two error constants should work since the module is specified to prevent ambiguous type errors.

What happend instead

The error we get with B.NoError is Reference to generic type 'B' requires arguments in <...>. The compiler incorrectly assumes we are referring to struct B instead of the module B. This makes it impossible to fix the ambiguity.

Workaround

It is possible to work around this by creating a typealias in a seperate file where we only import module B and there typealias BNoError = NoError then we can use the BNoError as the type of let errorB to work around the issue.

Activity

  1. belkadan commented on Mar 8, 2016

    @belkadan
    Contributor

    Needs design. import … as … is one possible answer.

  2. swift-ci commented on Mar 14, 2016

    @swift-ci
    ContributorAuthor

    Comment by Evan Maloney (JIRA)

    I've hit this issue as well, and have discovered that it doesn't require the use of generics to trigger it.

    In my case, I have a package LegacyStore and a package AppleTart, both of which contain a type named User.

    In the case of LegacyStore, User is an Objective-C class, and in the case of AppleTart, User is a Swift struct.

    Normally, I'd be able to refer to one as LegacyStore.User and the other as AppleTart.User, but when using this notation for an extension:

    extension AppleTart.User
    {
        public var legacyUser: LegacyStore.User {
            // implementation
        }
    }

    ...I get a compiler error on the extension declaration line:

    error: 'User' is not a member type of 'AppleTart'

    Note that the LegacyStore.User notation used in the var declaration never triggers a compiler error, so I'm assuming the notation just isn't supported yet in the context of declaring an extension.

    Fortunately, there's a (somewhat cumbersome) work-around, which is to create two separate files, one for each module. Within each file import just that one module, and create a typealias for each clashing name using package-qualified name of the type.

    For example, here's the relevant part of my AppleTartTypeDisambiguation.swift file:

    import AppleTart
    
    public typealias AppleTartUser = User

    And here's my LegacyStoreTypeDisambiguation.swift file:

    import LegacyStore
    
    public typealias LegacyUser = User

    I can then use the typealiases to extend either type. For example, here's how I successfully extended AppleTart.User:

    extension AppleTartUser
    {
        public var legacyUser: LegacyStore.User {
            // implementation
        }
    }

    After circulating the problem above to the Swift-Users mailing list, @belkadan reached out with a (correct) hunch that the underlying problem was that I have a type that has the same name as the module.

    In fact, the AppleTart module contained a type also named AppleTart, which acts as an interface for controlling the framework that vends the module. I renamed the type to AppleTartFramework and am no longer experiencing the problem.

  3. swift-ci commented on Sep 24, 2018

    @swift-ci
    ContributorAuthor

    Comment by Vladimir Shabanov (JIRA)

    Please fix. There are problems with importing custom obj-c error classes (descendants of NSError) even from single custom Obj-C framework to Swift app.

  4. lilyball commented on Feb 4, 2021

    lilyballmannequin
    Mannequin

    Also see SR-14142 where this missing language feature is causing emitted swiftinterface files to fail to compile.

  5. added a commit that references this issue on Jan 13, 2026
  6. added
    multiple modulesFlag: An issue whose reproduction requires multiple modules
    typesFeature: types
    and removed
    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.
    on Jan 27, 2026
  7. 4 remaining items

  8. AnthonyLatsis commented on Jan 28, 2026

    @AnthonyLatsis
    Collaborator
  9. tshortli commented on May 13, 2026

    @tshortli
    Contributor

    This was addressed by #87132.

  10. added
    swift evolution implementedFlag → feature: A feature that was approved through the Swift evolution process and implemented
    and removed
    swift evolution approvedFlag → feature: A feature that was approved through the Swift evolution process
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    compilerThe Swift compiler itselfdisambiguationBug: disambiguationmultiple modulesFlag: An issue whose reproduction requires multiple modulesswift evolution implementedFlag → feature: A feature that was approved through the Swift evolution process and implementedtypesFeature: types

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions